Results 1 to 3 of 3

Thread: Binding to a DropDownList

  1. #1
    Join Date
    Feb 2005
    Location
    Sydney
    Posts
    17

    Default Binding to a DropDownList

    I was wondering if it was possible to bind both the selected item / value and the item.

    Would this require two [binding] attributes?

    I was planning on binding the drop down list to an IList of Business Objects. Just abit unsure how to do it.

    If there is any work that needs to be done here, then I'd be happy to do it.

    cheers,

    Paul J

  2. #2
    Join Date
    Sep 2004
    Location
    Belgrade, Serbia
    Posts
    613

    Default

    It's possible to specify multiple binding for the same control, but in this case it won't work as expected.

    Problem is that DataBinder has no way of knowing when to call DataBind method on your dropdown, so it never calls it directly. You can handle OnDataBound event and call it manually from there, but that seems to reset selected value.

    What worked best for me is to use Spring data binding for selected value and to use standard .Net data binding for the items. That was actually the reason I added OnInitializeDataModel and OnInitializeControls methods to the page and user control lifecycle.

    So, in order to do what you want you would load your list of business objects in OnInitializeDataModel method and bind it to a dropdown in OnInitializeControls:

    Code:
    protected override void OnInitializeControls(object sender, EventArgs e)
    {
        if (!IsPostback)
        {
            myDropdown.DataSource = myListOfBusinessObjects;
            myDropdown.DataTextField = "nameOfTheBOPropertyToDisplay";
            myDropdown.DataValueField = "nameOfTheBOPropertyToReturnWhenSelected";
            myDropdown.DataBind();
        }
    }
    Hope this helps,

    Aleks

  3. #3
    Join Date
    Mar 2005
    Posts
    78

    Default

    Right now, we're using an entirely different approach to binding.

    Our data-access layer uses IDictionaries, and ASP.NET uses ControlCollection.

    We name our ASP.NET controls to match the field names our DAO layer uses.

    We can then pass the data-access IDictionary and ControlCollection to a utility method that matches the names. We have one set of utilties for reading and another for binding.

    For dropdown lists, the utilities accept a standard suffix, like "_list". When reading or binding a list control, the utilities drop the "_list", and use that to bind the default value to read the selected value. So a dropdown list named "user_key_list" maps to "user_key".

    The bottom line is that we can add controls to the ASP.NET page without any additional configuration. The trade-off is that we need to make sure everything is named consistently. (Though, we think that's a good thing!)

    I can post the code, if anyone is interested.

    -Ted.

Similar Threads

  1. Replies: 1
    Last Post: 05-22-2006, 08:03 PM
  2. Replies: 28
    Last Post: 02-23-2006, 03:50 AM
  3. Replies: 9
    Last Post: 11-30-2005, 12:26 AM
  4. Data Binding - Formatting the output
    By stephenro in forum Web
    Replies: 2
    Last Post: 08-16-2005, 12:09 AM
  5. Replies: 1
    Last Post: 05-16-2005, 10:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •