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

comments powered by Disqus

3 Comments

Comments have been disabled for this content.