重新按下按钮上的下拉列表

时间:2014-02-07 15:03:36

标签: c# asp.net

我有一个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();
        }
    }
}

2 个答案:

答案 0 :(得分:3)

要在设置DropDown ID时填充下拉列表,只需在其上调用DataBind(),它就会被反弹。您必须在按钮单击处理程序中调用它,如:

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

答案 1 :(得分:1)

您应首先从下拉列表中清除旧值,然后重新绑定。

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"));

            }
        }
    }
相关问题