Link to home
Start Free TrialLog in
Avatar of bobbellows
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="ddlLastName_SelectedIndexChanged">
            <asp:ListItem Value="0">&lt;--Select Name--&gt;</asp:ListItem></asp:DropDownList>
    </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">&lt;--Select Name--&gt;</asp:ListItem></asp:DropDownList>
    </div>


    <asp:SqlDataSource ID="sdsLastName" runat="server" ConnectionString="<%$ ConnectionStrings:SchoolDirectory %>" ProviderName="<%$ ConnectionStrings:SchoolDirectory.ProviderName %>" SelectCommand="usp_GetLastNameByClassID_byRB" SelectCommandType="StoredProcedure">
        <SelectParameters>
            <asp:ControlParameter ControlID="ddlClassList" Name="p_ClassID" PropertyName="SelectedValue" Type="Int32" />
        </SelectParameters>
    </asp:SqlDataSource>
    <asp:SqlDataSource ID="sdsFirstName" runat="server" ConnectionString="<%$ ConnectionStrings:SchoolDirectory %>" ProviderName="<%$ ConnectionStrings:SchoolDirectory.ProviderName %>" SelectCommand="usp_GetFirstNameByLastName_byRB" SelectCommandType="StoredProcedure">
        <SelectParameters>
            <asp:ControlParameter ControlID="ddlClassList" Name="p_ClassID" PropertyName="SelectedValue" Type="String" />
            <asp:ControlParameter ControlID="ddlLastName" Name="p_LastName" PropertyName="SelectedValue" Type="String" />
        </SelectParameters>
    </asp:SqlDataSource>


asp.net C#
    protected void ddlLastName_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlFirstName.ClearSelection();
        ddlFirstName.DataBind();
    }
ASKER CERTIFIED SOLUTION
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bobbellows
bobbellows

ASKER

I see what you're saying. In fact it might even increase the chance of a mistake. The names are only stored as FirstName and LastName. I'm somewhat new to c# programming and only a little more experienced with asp.net. I'm not sure how to build a dataset of FirstName+LastName. BTW the append = False worked. Thanks. Sorry for the delay. My day job kept me from getting back to this till now.

If you can point me in a direction for the other dataset, I'll start working on that. Again. Thanks.