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:DropDownList>
                        <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:DropDownList>
                        <asp:SqlDataSource ID="PositionSqlDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:OJTConnectionString %>" SelectCommand="SELECT [PositionID], [JobTitle] FROM [Position] WHERE ([EmployerID] = @EmployerID) ORDER BY [JobTitle]">
                            <SelectParameters>
                                <asp:ControlParameter ControlID="ddlEmployer" Name="EmployerID" PropertyName="SelectedValue" />
                            </SelectParameters>
                        </asp:SqlDataSource>

Open in new window

LVL 1
megninAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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:

ddlPositions.Items.Clear()
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.
0
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.
0
megninAuthor Commented:
I did this:

        ddlPosition.Items.Clear()
        ddlPosition.Items.Add("")

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>
0
megninAuthor Commented:
Thanks a lot.  After also adding ddlPosition.Items.Add("") it's working perfectly now.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.