I have a asp.net 2.0 web site with numerous asp:DropDownList controls. The DropDownList control contains the standard info city, state, county etc… info. In addition to the standard codes the site also has custom codes that the users can configure themselves. For example a animal dropdown may contain the values Dog, Cat, Fish, ect…
I am popluating the DropDownList from a SQL 2005 table that I created e.g. tblCodes
Everything works great and users are able to add orders using the numerous DropDownList controls to choose items from the list.
The problem occurrs if a user wants to change one of their custom dropdowns. For example a user would like to change the verbage on a animal type control from Dog to K9. This is where the problem starts.
For all new orders the drop down works fine. When the user retrieved an old order I get the following error in the C# codebehind ”DropDownList1′ has a SelectedValue which is invalid because it does not exist in the list of items.’
What’s happening is the old order has a database field value of Dog and the DropDownList no longer has Dog in its list since the user changed it to K9.
Any ideas on a workaround?
Is there a way to make the asp:DropDownList accept items not seeded in its list? Is there another control I could use?
I’m not sure it’s the same issue, but I had a similar sounding issue with trying to bind a DropDownList that I wanted to contain in a GridView. When I looked around I found a lot of people asking similar questions, but no robust solutions. I did read conflicting reports about whether you could intercept databinding, etc events. I tried most of them but I couldn’f find a way of intercepting or pre-empting the error.
I ended up creating a subclass of the ddl, intercepting the error from there hacking a fix.
Not tidy but it worked for my needs. I put the code up on my blog in case it’s of help. link text