bobbellows
asked on
re-binding dropdownlist
I'm constructing a little page that allows a teacher to assign a student in a class to a group. To select the student I use 2 dropdownlists. The first one is for the last name. The second one is for the first name. This way if there are 2 students with the same last name I can choose which one I want. To test this out I have two students in the database, BooBoo Bear and Bam Bam Rubble.
This is what happens. If I choose "Bear" for the last name after the page loads Boo Boo is the only choice for first name. This is what I expected. If I change my mind and select "Rubble" for the last name then both Boo Boo and Bam Bam are displayed. If I change my mind again and select "Bear" again the First Name dropdownlist then shows Boo Boo, Bam Bam, and then Boo Boo again. Auto Postback is set to true. Below is the applicable code. I did try to re-bind the FirstName dropdownlist but that didn't work.
HTML:
<div id="divLastName" style="position:absolute; top:120px; left:25px; width:150px; text-align: left;">
<asp:Label ID="lblLastName" runat="server" Text="Last Name"></asp:Label>
<asp:DropDownList ID="ddlLastName" runat="server" DataSourceID="sdsLastName" AutoPostBack="True" DataTextField="LastName" DataValueField="LastName" AppendDataBoundItems="true " OnSelectedIndexChanged="dd lLastName_ SelectedIn dexChanged ">
<asp:ListItem Value="0"><--Select Name--></asp:ListItem>< /asp:DropD ownList>
</div>
<div id="divFirstName" style="position:absolute; top:120px; left:185px; width:150px; text-align: left;">
<asp:Label ID="lblFirstName" runat="server" Text="First Name"></asp:Label>
<asp:DropDownList ID="ddlFirstName" runat="server" DataSourceID="sdsFirstName " DataTextField="FirstName" DataValueField="FirstName" AutoPostBack="True" AppendDataBoundItems="true ">
<asp:ListItem Value="0"><--Select Name--></asp:ListItem>< /asp:DropD ownList>
</div>
<asp:SqlDataSource ID="sdsLastName" runat="server" ConnectionString="<%$ ConnectionStrings:SchoolDi rectory %>" ProviderName="<%$ ConnectionStrings:SchoolDi rectory.Pr oviderName %>" SelectCommand="usp_GetLast NameByClas sID_byRB" SelectCommandType="StoredP rocedure">
<SelectParameters>
<asp:ControlParameter ControlID="ddlClassList" Name="p_ClassID" PropertyName="SelectedValu e" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="sdsFirstName" runat="server" ConnectionString="<%$ ConnectionStrings:SchoolDi rectory %>" ProviderName="<%$ ConnectionStrings:SchoolDi rectory.Pr oviderName %>" SelectCommand="usp_GetFirs tNameByLas tName_byRB " SelectCommandType="StoredP rocedure">
<SelectParameters>
<asp:ControlParameter ControlID="ddlClassList" Name="p_ClassID" PropertyName="SelectedValu e" Type="String" />
<asp:ControlParameter ControlID="ddlLastName" Name="p_LastName" PropertyName="SelectedValu e" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
asp.net C#
protected void ddlLastName_SelectedIndexC hanged(obj ect sender, EventArgs e)
{
ddlFirstName.ClearSelectio n();
ddlFirstName.DataBind();
}
This is what happens. If I choose "Bear" for the last name after the page loads Boo Boo is the only choice for first name. This is what I expected. If I change my mind and select "Rubble" for the last name then both Boo Boo and Bam Bam are displayed. If I change my mind again and select "Bear" again the First Name dropdownlist then shows Boo Boo, Bam Bam, and then Boo Boo again. Auto Postback is set to true. Below is the applicable code. I did try to re-bind the FirstName dropdownlist but that didn't work.
HTML:
<div id="divLastName" style="position:absolute; top:120px; left:25px; width:150px; text-align: left;">
<asp:Label ID="lblLastName" runat="server" Text="Last Name"></asp:Label>
<asp:DropDownList ID="ddlLastName" runat="server" DataSourceID="sdsLastName"
<asp:ListItem Value="0"><--Select Name--></asp:ListItem><
</div>
<div id="divFirstName" style="position:absolute; top:120px; left:185px; width:150px; text-align: left;">
<asp:Label ID="lblFirstName" runat="server" Text="First Name"></asp:Label>
<asp:DropDownList ID="ddlFirstName" runat="server" DataSourceID="sdsFirstName
<asp:ListItem Value="0"><--Select Name--></asp:ListItem><
</div>
<asp:SqlDataSource ID="sdsLastName" runat="server" ConnectionString="<%$ ConnectionStrings:SchoolDi
<SelectParameters>
<asp:ControlParameter ControlID="ddlClassList" Name="p_ClassID" PropertyName="SelectedValu
</SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="sdsFirstName" runat="server" ConnectionString="<%$ ConnectionStrings:SchoolDi
<SelectParameters>
<asp:ControlParameter ControlID="ddlClassList" Name="p_ClassID" PropertyName="SelectedValu
<asp:ControlParameter ControlID="ddlLastName" Name="p_LastName" PropertyName="SelectedValu
</SelectParameters>
</asp:SqlDataSource>
asp.net C#
protected void ddlLastName_SelectedIndexC
{
ddlFirstName.ClearSelectio
ddlFirstName.DataBind();
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
If you can point me in a direction for the other dataset, I'll start working on that. Again. Thanks.