Declarative DataBinding in Asp.Net


You can bind any of the List controls to a data source. The List controls support both
declarative databinding and programmatic databinding

Example:

Suppose you have a movies table in your database. It has two columns named Id and Title.
<asp:DropDownList
            ID="ddlMovies"
            DataSourceID="srcMovies"
            DataTextField="Title"
            DataValueField="Id"
            runat="server" />

        <asp:SqlDataSource
            ID="srcMovies"
            SelectCommand="SELECT Id, Title FROM Movies"
            ConnectionString="yourConnectionString"
            runat="server" />

The DropDownList control’s DataSourceID property points to the ID of the SqlDataSource control. The SqlDataSource control retrieves the records from the Movies database table. The DropDownList control grabs these records from the SqlDataSource control and creates a ListItem control for each data item. 

The DropDownList control has both its DataTextField and DataValueField properties set. When the DropDownList control creates each of its list items, it uses the values of the DataTextField and DataValueField properties to set the Text and Value properties of each list item.

Read About Programmatic DataBinding :  ProgrammaticDataBinding in Asp.Net

No comments:

Post a Comment