Reputation: 1070
I have several DropDownList in my website, and they are filtering each other.
So, I have a school, based on that I have classes, and inside of that classes I have students. Each one has its own table on the DB, and it's generated from a table that has all the IDs.
I dont know why, but I can filter the classes from the school, but the students DropdownList wouldn't be affected by the filter.
This is my code :
<li>School </li>
<li>
<asp:DropDownList ID="SchoolBox" runat="server" AutoPostBack="True"
DataSourceID="DropDownlistSchool" DataTextField="SchoolName"
DataValueField="ID">
</asp:DropDownList>
<asp:SqlDataSource ID="DropDownlistSchool" runat="server"
ConnectionString="<%$ ConnectionStrings:DanielConnectionString %>"
SelectCommand="SELECT [SchoolName], [ID] FROM [Schools]">
</asp:SqlDataSource>
</li>
<li>Class</li>
<li>
<asp:DropDownList ID="ClassBox" runat="server" AutoPostBack="True"
DataSourceID="Class2" DataTextField="ClassName" DataValueField="ID">
</asp:DropDownList>
<asp:SqlDataSource ID="Class2" runat="server"
ConnectionString="<%$ ConnectionStrings:DanielConnectionString %>"
SelectCommand="SELECT * FROM [Class] WHERE ([SchoolID] = @SchoolID)">
<SelectParameters>
<asp:ControlParameter ControlID="SchoolBox" Name="SchoolID"
PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
</li>
<li>Student</li>
<li>
<asp:DropDownList ID="StudentBox" runat="server" AutoPostBack="True"
DataSourceID="Student" DataTextField="Username" DataValueField="ID">
</asp:DropDownList>
<asp:SqlDataSource ID="Student" runat="server"
ConnectionString="<%$ ConnectionStrings:DanielConnectionString %>"
SelectCommand="SELECT * FROM [Users] WHERE (([ClassID] = @ClassID) AND ([SchoolID] = @SchoolID))">
<SelectParameters>
<asp:ControlParameter ControlID="ClassBox" Name="ClassID"
PropertyName="SelectedValue" Type="Int32" />
<asp:ControlParameter ControlID="SchoolBox" Name="SchoolID"
PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
Upvotes: 2
Views: 728
Reputation: 4165
I had a similar form with cascading DropDownLists, each one filtering the next. I found that using the methods mentioned above, it would only work the first time a value was selected in a DropDownList, but after that I couldn't get a list's values to change again. In other words, when I first loaded the form and chose a value for first DropDownList, the second would populate...when I chose a value for second, the third would populate...but if I then changed the value of the second, I couldn't get the third to re-populate again. It always got stuck with the first set of values.
So I built a small function called RebuildDropDown that I call from the OnSelectedIndexChanged handler. In the handler for a given DropDownList, I call RebuildDropDown for each dependent list, like this:
protected void SchoolBox_SelectedIndexChanged(object sender, EventArgs e)
{
RebuildDropDown(ClassBox);
RebuildDropDown(StudentBox);
}
protected void ClassBox_SelectedIndexChanged(object sender, EventArgs e)
{
RebuildDropDown(StudentBox);
}
protected void RebuildDropDown(DropDownList ddl)
{
ddl.Items.Clear();
// OPTIONAL: add "ALL" option as first option
ListItem li = new ListItem("All", "ALL");
ddl.Items.Add(li);
// now add databound items
ddl.DataBind();
}
Upvotes: 0
Reputation: 11433
You need to add a databound event to your "ClassBox" DropDownList
:
<asp:DropDownList ID="ClassBox" runat="server" AutoPostBack="True"
DataSourceID="Class2" DataTextField="ClassName" DataValueField="ID"
OnDataBound="ClassBox_DataBound" >
</asp:DropDownList>
Then, in your code behind, you need to do this:
protected void ClassBox_DataBound(object sender, EventArgs e)
{
// Bind the SQLDataSource
Student.DataBind();
// Re-bind the associated DropDownList
StudentBox.DataBind();
}
When you select a school, the AutoPostBack occurs, and the "ClassBox" gets updated based on the SelectedValue of the "SchoolBox". At this point, the "ClassBox" does not have a SelectedValue yet, so the "StudentBox" data source doesn't have parameters. Once the "ClassBox" is databound though, it's safe to redo the databinding of the "StudentBox" (thus getting the new information in there).
Upvotes: 1
Reputation: 1110
In the class & student drop down you've set:
DataValueField="ID"
Should this be set as :
DataValueField="ClassId", DataValueField="StudentId"
respectively.
That is unless you've also an Id field in the table?
Upvotes: 0