簡體   English   中英

單擊按鈕重新填充下拉菜單

[英]Repopulate Dropdown on Button Click

我有一個DataBound DropDown控件(因此由設計器中指定的查詢填充),並且我希望單擊按鈕時可以重新填充此查詢。 DropDown的定義如下:

<asp:DropDownList ID="JobRelPhase_DropDown" runat="server" 
     DataSourceID="SqlDataSourceMYDATASOURCE" DataTextField="JobRelPhase" 
     DataValueField="id" OnSelectedIndexChanged="my_DropDown_SelectedIndexChanged" 
     AutoPostBack="True" style="text-align: center" 
     Width="684px" Font-Bold="True" AppendDataBoundItems="true" BackColor="White" 
     ForeColor="Black">
         <asp:ListItem Value="SELECT AN ITEM" disabled="disabled"></asp:ListItem>
</asp:DropDownList>`

並且查詢在SqlDataSourceMYDATASOURCE定義。

編輯:這是單擊按鈕時調用的函數:

protected void GenerateButton_Click(object sender, EventArgs e)
{
    if (JobRelPhase_DropDown.SelectedIndex != -1)
    {
        if (JobActive())
        {
            SetButton(GenerateButton, false);
            //JobRelPhase_DropDown.SelectedIndex = -1; //to set back to the top of the list
            JobRelPhase_DropDown.DataBind();
        }
    }
}

要在設置DropDownID時重新填充下拉列表,只需在其上調用DataBind() ,它將被反彈。 您必須在按鈕單擊處理程序中調用它,如下所示:

protected void Button_Click(..)
{
    //Since you have AppendDataBoundItems="true", have to clear list to reset
    JobRelPhase_DropDown.Items.Clear();
    JobRelPhase_DropDown.DataBind();
}

您應該首先從下拉列表中清除舊值,然后重新綁定。

JobRelPhase_DropDown.Items.Clear();

即:

 protected void GenerateButton_Click(object sender, EventArgs e)
    {
        if (JobRelPhase_DropDown.SelectedIndex != -1)
        {
            if (JobActive())
            {
                SetButton(GenerateButton, false);
                //JobRelPhase_DropDown.SelectedIndex = -1; //to set back to the top of the list
                 JobRelPhase_DropDown.Items.Clear();

                JobRelPhase_DropDown.DataBind();
JobRelPhase_DropDown.Items.Insert(0, new ListItem("SELECT AN ITEM"));

            }
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM