Intercepting DropDownList Selected Value when not in list of Items

W

WJB

I'm using a FormView with a number of DropDownList controls that are bound to
SqlDataSources. Sometimes, the current contents of the bound fields are
invalid (i.e. not in the list of items retrieved from the data source used as
the DataSourceID for the dropdown list). Trapping the OnDataBinding event is
too early since the list items and selected index/text have not been
populated yet. Trapping the OnDataBound event is too late as the exception
has already been thrown. The rules I'm working with make such that I cannot
add the invalid item to the list's data source.

Am I understanding this correctly? Is there a way to try..catch an exception
that seems to occur between two events firing? Or there a better way to
accomplish this short of doing it the hard way and replacing the
SqlDataSources with ObjectDataSources and coding the data access myself?

Thanks
 
P

Phil H

I'm using a FormView with a number of DropDownList controls that are bound to
SqlDataSources. Sometimes, the current contents of the bound fields are
invalid (i.e. not in the list of items retrieved from the data source used as
the DataSourceID for the dropdown list). Trapping the OnDataBinding event is
too early since the list items and selected index/text have not been
populated yet. Trapping the OnDataBound event is too late as the exception
has already been thrown. The rules I'm working with make such that I cannot
add the invalid item to the list's data source.

Am I understanding this correctly? Is there a way to try..catch an exception
that seems to occur between two events firing? Or there a better way to
accomplish this short of doing it the hard way and replacing the
SqlDataSources with ObjectDataSources and coding the data access myself?

Thanks

Neither an SqlDataSource or an Object DataSource can be used for
individual controls contained within a FormView control separately
from the one that is attached to the FormView control via its
DataSourceID property. You can populate the DDL controls individually
with code in the DataBinding event but you will have to use objects
such as System.Data.SqlClient.SqlCommand +
System.Data.SqlClient.SqlDataReader to do it and write explicit code.
SqlDataSource objects and the associated databind events cannot be
nested as you appear to be trying to do.

Phil Hall
 
W

WJB

Thank you, Phil. I wan't necessarily trying to nest databind events. Rather,
I wanted to try to prevent the DataBound event from firing as the exception
has already occurred. But on the event preceding that, namely the DataBinding
event, the fields have not been populated yet so I cannot test whether the
entered content in the control is valid or not. However, if I explicitly
write the data access code myself as you suggest, I should be able to fully
control the process.

Thanks again for your response.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top