Reputation: 796
I am trying to implement a simple filter from one dropdown box to another.
The second dropdown box deos not populate (with any item) when I select an item from the first dropdown. I am not sure what I am missing. Please advise.
Here is the ascx code:
<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>
<asp:DropDownList ID="DropDown2" runat="server">
<asp:ListItem Text="--Select One--" Value="" />
</asp:DropDownList>
</div>
And here is my code behind:
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();
}
}
Upvotes: 0
Views: 319
Reputation: 17724
You need to set auto postback to true if you want it to update the page on item change.
<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>
You might also want to consider wrapping both these DropDownList controls in an update panel, so that you don't refresh the whole page every time a user changes a selection.
Upvotes: 1
Reputation: 6123
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>
Upvotes: 1