AppendDataBoundItems stacks multiple Items. Old selections are not removed when a new parent selection is made.

I have two DropDownLists on an ASP.NET 4.0 web form.

ddlEmployers is the parent and gets it's items from the Employers MS SQL table.

ddlPositions gets it's items from the Positions table based on the EmployerID selected in ddlEmployers.

With AppendDataboundItems="True" the single empty string item is listed first then the items pulled from the database, just as they should be.  But when I change the selection in ddlEmployers, the new Position Items are added to the previous ones instead of replacing them.  

That actually makes sense with AppendDataboundItems="True" but it is not desired, of course.  I want the positions for each newly selected employer to be the only items listed in ddlPositions.

If I set AppendDataboundItems="False" then new items replace old items as desired, BUT there is no emptystring item at the top to provide an empty space so that a single item can be selected and fire the ddlPositions.SelectedIndexChanged.  Without that blank item at the top, you can't select the first or only item in the list because it won't fire the SelectedIndexChanged.

How do I fix my cascading DropDownLists to work right?

                        <asp:DropDownList ID="ddlEmployer" runat="server" AppendDataBoundItems="True" AutoPostBack="True" DataSourceID="ddlEmployerSqlDataSource" DataTextField="CompanyName" DataValueField="EmployerID">
                            <asp:ListItem Value="0" Text=""/>
                        <asp:SqlDataSource ID="ddlEmployerSqlDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:OJTConnectionString %>" SelectCommand="SELECT [EmployerID], LTRIM(RTRIM(F1CompanyName)) AS CompanyName FROM [Employer] ORDER BY [CompanyName]"></asp:SqlDataSource>

Open in new window

                        <asp:DropDownList ID="ddlPosition" runat="server" AppendDataBoundItems="True" AutoPostBack="True" DataSourceID="PositionSqlDataSource" DataTextField="JobTitle" DataValueField="PositionID" >
                            <asp:ListItem Value="0" Text=""/>
                        <asp:SqlDataSource ID="PositionSqlDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:OJTConnectionString %>" SelectCommand="SELECT [PositionID], [JobTitle] FROM [Position] WHERE ([EmployerID] = @EmployerID) ORDER BY [JobTitle]">
                                <asp:ControlParameter ControlID="ddlEmployer" Name="EmployerID" PropertyName="SelectedValue" />

Open in new window

Who is Participating?
Paul JacksonSoftware EngineerCommented:
Leave  AppendDataboundItems="True" and then in the code-behind in the SelectedIndexChanged event for ddlEmployers dropdown clear the items from the ddlPositions dropdown and then when the page is reloaded the single blank entry is added and then the new data items are added to the dropdown from the sqldatasource.

To clear the items from the dropdown list:

megninAuthor Commented:
Oh, bless you!  I was hoping for simple elegant solution like this.

I has also tried putting EnableViewState="false" on the ddlPosition DropDownList, but that caused issues with data not being inserted or updated.  The form just didn't work with EnableViewState="false".

I'll give this a try real quick and be right back.
megninAuthor Commented:
No joy.  ddlPositions.Items.Clear() in the ddlEmployers SelectedIndexChanged event has the same effect as AppendDataboundItems="False", whether I place it at the very beginning of the event or at the very end.
megninAuthor Commented:
I did this:


And, since I'm doing this:
 If ddlPosition.SelectedIndex > 0 Then

Instead of this:
If ddlPosition.SelectedValue = "0" Then

It seems to be okay.  I'll test the form for full functionality and see how it goes.  <crosses fingers>
megninAuthor Commented:
Thanks a lot.  After also adding ddlPosition.Items.Add("") it's working perfectly now.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.