Intercepting DropDownList Selected Value when not in list of Items

Discussion in 'ASP .Net Web Controls' started by WJB, Oct 31, 2007.

  1. WJB

    WJB Guest

    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
     
    WJB, Oct 31, 2007
    #1
    1. Advertising

  2. WJB

    Phil H Guest

    On 31 Oct, 00:22, WJB <> wrote:
    > 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
     
    Phil H, Nov 7, 2007
    #2
    1. Advertising

  3. WJB

    WJB Guest

    Re: Intercepting DropDownList Selected Value when not in list of I

    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.

    "Phil H" wrote:

    > On 31 Oct, 00:22, WJB <> wrote:
    > > 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
    >
    >
     
    WJB, Nov 8, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bart Van Hemelen
    Replies:
    2
    Views:
    4,693
    Bart Van Hemelen
    Sep 4, 2006
  2. Iain
    Replies:
    3
    Views:
    951
  3. Nathan Sokalski
    Replies:
    0
    Views:
    635
    Nathan Sokalski
    Oct 5, 2008
  4. mldardy
    Replies:
    1
    Views:
    762
    mldardy
    Dec 10, 2008
  5. Nathan Sokalski

    Selected ListItem in DropDownList does not appear selected

    Nathan Sokalski, Oct 5, 2008, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    301
    Nathan Sokalski
    Oct 5, 2008
Loading...

Share This Page