0

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

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

这是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();
        }
    }
4

3 回答 3

1

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

于 2012-08-09T20:27:26.023 回答
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>
于 2012-08-09T20:28:46.880 回答
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 控件包装在一个更新面板中,这样您就不会在每次用户更改选择时刷新整个页面。

于 2012-08-09T20:29:44.740 回答