Databinding a Drop-DownList within a FormView Control w/ 2-way databinding in ASP.NET 2.0

Mike Pope has a great blog post tonight that walks through a common scenario I've seen asked a lot on http://forums.asp.net lately.  Specifically his post covers how to embed a DropDownList control -- whose ListItem values are databound against one database table -- within a FormView or GridView control, and have the SelectedValue of the DropDownlist be two-way databound against the results from a separate database table.

For example: you could have a FormView control that shows an individual product's details from Northwind, and have an edit view of that Product that allows you to change its category value.  Rather than expose the category as the raw CategoryId number, you want to have the categories be surfaced as a friendly drop-downlist of category names (with each item value being the appropriate categoryid).

Mike shows how to-do this with ASP.NET 2.0 and no code using the new <asp:formview> control, and two-way <%# Bind("columnname") %> databinding syntax.  His sample uses the new <asp:accessdatasource> control for the databinding, but you could use the exact same technique using the Northwinds DAL I built a few weeks ago and the <asp:objectdatasource>

Hope this helps,

Scott

 

5 Comments

  • The main issue with this approach is that the system get quite 'chatty', every single DataTable that is to be used needs a unique method in the DAL (imagine you need to fill 10 different combos). It would be so much more cleaner if the objectdatasource would be able to expose several DataTables, since the dropdownlist (and all other controls) support &quot;DataMember&quot;.

  • In your scenario above... "Rather than expose the category as the raw CategoryId number, you want to have the categories be surfaced as a friendly drop-downlist of category names"

    How could I display the category name in the itemtemplate of the formview? Is this possible? Thanks!

  • Hi Jeff,

    The DropDownList control has both a "DataTextField" and "DataValueField" property. You can set these to separate columns in your datasource - so that the CategoryID is the DataValueField, and the CategoryName is the DataTextField to display.

    Hope this helps,

    Scott

  • How do you handle a 2 way update to an asp:parameter of type 'object'?

    For example - take 2 listbox controls

    i bind each listbox control with the same data type (say class ObscureClass) and set the value & text fields properly. if i have a button to move objects from one list to the other, it would work fine in the ui. however, the updated listbox is unable to post back the new list of data ObscureClass objects to an objectdatasource so that they may post for update.

    how would you go about implementing 2-way databinding with an 'object' to a listbox?

  • Hi Bo,

    To enable two-way binding you'll want to use the syntax. You'll then probably want to bind the "selectedvalue" attribute on the listbox.

    Hope this helps,

    Scott

Comments have been disabled for this content.