DropDownList - adding missing items on the fly to avoid ArgumentOutOfRangeException
I have a dropdownlist with a list of options which is subject to change (e.g. 'active users'). Another data source may include historical data, but I wish to use the 'active users' dropdownlist against it. This means an ArgumentOutOfRangeException will be thrown so I want to catch that, and insert the required entry into the dropdownlist on the fly.
I've seen a few suggestions that point towards overriding the ondatabinding event within the dropdownlist, I've managed to do this insofar as I can create a static listitem for example "Value does not exist" and bypass the exception.
However I wish rather than "Value does not exist" so show the actual value which we were attempting to find in the ddl.
ondatabinding is passed a standard EventArgs item which doesn't offer much to go on, I can see there is a DataListItemEventArgs class - is this in any way associated with a dropdownlist? I tried directcast on the EventArgs but that didn't work...
So is there any way - within 开发者_如何学Condatabinding - of finding out the value we're attempting to set on the dll, which causes the ArgumentOutOfRangeException?
Cheers!
ps it's similar to this issue I think: https://stackoverflow.com/questions/3671556/dynamically-add-item-to-dropdownlist
If you select your record that has the inactive user in it, you could recreate your SQL (if that is what you are using) to select all active users plus any that have the id(s) you pass in. If you are using some other persistance technology, there are other code based was to fix it. All in all, its probably substantially easier (within the ASP.NET model) to merge the data into a single collection before binding the grid then it is to do it after (outside of some trivial tasks like adding '<-- Select One -->' to the beginning of the list as an example).
I think the easiest thing to do is to change the data source of the drop down to include both current and historical users.
精彩评论