I was (and still am) hoping for a better answer. I already have two options but would like some other input. I can solve this problem by using stored procedures (faux-code below).
CREATE PROCEDURE dbo.pr_SomeList
(
@ExistingValue int = -1
)
AS
SELECT Id, TextField, Active
FROM dbo.MyTable
WHERE Active = 1 OR Id = @ExistingValue
That approach works very well but it means I have to create a stored proc for each list and use the MS SQLDataSource controls. (Or do more coding in .NET and use an ObjectDataSource control.)
My second option is to sub-class the dropdownlist control and catch the exception during the PerformDataBinding event. At that point, I can change the SelectedValue to a known existing fallback value.
protected override void PerformDataBinding(System.Collections.IEnumerable dataSource)
{
try
{
base.PerformDataBinding(dataSource);
}
catch (ArgumentOutOfRangeException ex)
{
this.SelectedValue = this.DefaultItemValue;
base.PerformDataBinding(dataSource);
}
}
I'm looking for any other ideas that might be out there.