Binding Data to ASP.NET 2.0 Server Controls

I'm teaching the ASP.NET 2.0 Programming class at Interface Technical Training this week and had a few people ask questions about how to embed controls such as a DropDownList into a DetailsView and use it while in edit mode.  The sample code below demonstrates one way of doing this without resorting to VB.NET or C# code and shows how the <%# Bind("FieldName") %> data binding expression can be used. 

For example, the following code shows how to embed the DropDownList into a DetailsView control's EditItemTemplate and automatically select the proper value by using the Bind expression:

<asp:TemplateField HeaderText="Country"> 
     
<EditItemTemplate>
          
<asp:DropDownList ID="ddl" runat="server" 
            SelectedValue
='<%# Bind("Country") %>'
            DataTextField
="Country" DataValueField="Country" 
            DataSourceID
="sdsCountries" />
     </
EditItemTemplate>
     
<ItemTemplate>
          
<%# Eval("Country") %>
    
</ItemTemplate>
</asp:TemplateField>

The code also demonstrates how to consume an RSS feed with the XmlDataSource control and the fundamentals of creating and using TableAdapters and Strongly-Typed DataSets with the ObjectDataSource control.  Download the code below and change the connection string in web.config to point to your Northwind database to get the demo going.

Data Source Demos Code

Published Wednesday, March 21, 2007 2:13 PM by dwahlin
Filed under: , ,

Comments

# re: Binding Data to ASP.NET 2.0 Server Controls

Thursday, March 22, 2007 3:26 PM by Palermo4

This is a common feature request - Great Blog!

# re: Binding Data to ASP.NET 2.0 Server Controls

Sunday, March 25, 2007 5:15 PM by morteza sahragard

great post, thank you

# re: Binding Data to ASP.NET 2.0 Server Controls

Monday, March 26, 2007 1:32 AM by dwahlin

I'm glad you found it useful.  Thanks for taking the time to comment.