下拉框链式过滤器无法正常工作?

时间:2012-08-09 20:23:24

标签: c# asp.net .net

我正在尝试从一个下拉框到另一个下拉框实现一个简单的过滤器。

当我从第一个下拉列表中选择一个项目时,第二个下拉框不会填充(包含任何项目)。 我不确定我错过了什么。 请指教。

这是ascx代码:

  <div id="SubmitSection" style="width:auto; height:auto;" class="SubmitSectionStyle">
        <div id="DropdownSection" style="text-align:center;">
        <asp:DropDownList ID="DropDown1" runat="server" AppendDataBoundItems="true"
               onselectedindexchanged="Type_SelectedIndexChanged" ToolTip="Select Category">
              <asp:ListItem Text="--Select Category--" Value="" />
              <asp:ListItem Value="1">Department</asp:ListItem>
              <asp:ListItem Value="2">Safety</asp:ListItem>
        </asp:DropDownList>&nbsp;

        <asp:DropDownList ID="DropDown2" runat="server">
        <asp:ListItem Text="--Select One--" Value="" />
        </asp:DropDownList>

      </div>

这是我背后的代码:

protected void Type_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DropDown1.SelectedValue == "1")
        {
            DropDown2.Items.Clear();
            DropDown2.Items.Add("DeptTest");
            DropDown2.DataBind();
        }
        else if (DropDown1.SelectedValue == "2")
        {
            DropDown2.Items.Clear();
            DropDown2.Items.Add("SafetyTest");
            DropDown2.DataBind();
        }
    }

3 个答案:

答案 0 :(得分:1)

在第一个下拉列表中,设置AutoPostBack="True"

答案 1 :(得分:1)

AutoPostBack = "true" // AutoPostBack attribute is missing in DropDown1 due to which the event does not fire

// change your dropdown1 code as
<asp:DropDownList ID="DropDown1" AutoPostBack = "true" runat="server" AppendDataBoundItems="true"
           onselectedindexchanged="Type_SelectedIndexChanged" ToolTip="Select Category">
          <asp:ListItem Text="--Select Category--" Value="" />
          <asp:ListItem Value="1">Department</asp:ListItem>
          <asp:ListItem Value="2">Safety</asp:ListItem>
    </asp:DropDownList>

答案 2 :(得分:1)

如果您希望在项目更改时更新页面,则需要将自动回发设置为true。

<asp:DropDownList ID="DropDown1" AutoPostBack="True" runat="server" AppendDataBoundItems="true" onselectedindexchanged="Type_SelectedIndexChanged" ToolTip="Select Category" >
              <asp:ListItem Text="--Select Category--" Value="" />
              <asp:ListItem Value="1">Department</asp:ListItem>
              <asp:ListItem Value="2">Safety</asp:ListItem>
        </asp:DropDownList>

您可能还想考虑在更新面板中包装这些DropDownList控件,这样每次用户更改选择时都不会刷新整个页面。

相关问题