2

I have the following code that removes the item selected in dropdown1 from dropdown2 when a value in dropdown1 is selected:

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList2.Items.Remove(DropDownList1.SelectedItem);

    }

效果很好。但是我尝试在页面加载时添加相同的代码,但它并没有删除我猜测的值,因为没有选择任何内容我该如何完成这个?

 protected void Page_Load(object sender, EventArgs e)
    {

        DropDownList2.Items.Remove(DropDownList1.SelectedItem);
    }

这是什么行不通,有什么想法吗?

4

2 回答 2

0

我想要做的是在 dropdown2 上不显示当前在 dropdown1 中选择的内容。

您的问题是,在页面加载上,所有内容都“重置”了。所以 DropDownList1.SelectedItem 是列表中的第一个或默认项。如果 DropDownList1.SelectedItem 为“One”,则它将始终从 DropDownList2 中删除“One”。

您需要的是 DropDownList1 的按钮或 OnSelectedIndexChanged 事件。

试试我在下面所做的,它对我有用。

<form id="form1" runat="server">
<div>
    <asp:DropDownList ID="ddlOne" runat="server" OnSelectedIndexChanged="ddlOne_OnSelectedIndexChanged" AutoPostBack="true" />
    <br />
    <br />
    <asp:DropDownList ID="ddlTwo" runat="server" />
    <br />
    <br />
    <asp:Button ID="btRemove" runat="server" Text="Remove" 
        onclick="btRemove_Click" />
</div>
</form>

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            // Bind data
            BindLists();
        }
    }

    /// <summary>
    /// Removes whatever Selected Item from ddlTwo that is 
    /// currently selected in ddlOne.
    /// </summary>
    private void RemoveFromTwoWhatIsSelectedInOne()
    {
        ddlTwo.Items.Remove(ddlOne.SelectedItem);
    }

    /// <summary>
    /// Click handler for btRemove. Calls the method 
    /// RemoveFromTwoWhatIsSelectedInOne()
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btRemove_Click(object sender, EventArgs e)
    {
        RemoveFromTwoWhatIsSelectedInOne();
    }

    /// <summary>
    /// OnSelectedIndexChanged handler for ddlOne. Calls the method 
    /// RemoveFromTwoWhatIsSelectedInOne()
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ddlOne_OnSelectedIndexChanged(object sender, EventArgs e)
    {
        RemoveFromTwoWhatIsSelectedInOne();
    }

    /// <summary>
    /// Binds a list of names the two Drop Down Lists, ddlOne and ddlTwo, by using 
    /// the same Data Source from the Manager.
    /// </summary>
    private void BindLists()
    {
        // Get the data
        Manager theManager = new Manager();
        List<Names> theNames = theManager.GetNames();

        // Assign the data source
        ddlOne.DataSource = theNames;
        ddlTwo.DataSource = theNames;

        // Bind each drop down list
        ddlOne.DataBind();
        ddlTwo.DataBind();
    }
于 2012-05-03T17:47:57.137 回答
0

你试过做吗?

if(!IsPostback)
{
    DropDownList2.Items.Remove(DropDownList1.SelectedItem);
}

在你的Page_Load活动中?另外,你是如何绑定数据的?为什么不这样做Selected_IndexChanged呢?在我看来,这就是这段代码所属的地方。DropdowsListPage_Load

于 2012-05-03T16:50:29.743 回答