Monday, July 23, 2012

Introduction to Data binding


Data binding helps to develop applications where controls can be bound to the values stored in a data source.

Web server controls can be bounded to data source such as an array list, database table dynamically.

The properties of a server control are set to a specific data source and when the application executes, it fetches data from the data source and displays it.

Data bind by using the following syntax:

    <asp:TextBox ID="txtName" Text="Enter Name" runat="server">Enter Name</asp:TextBox>
    <asp:Label ID="lblName" Text="<% #txtName.Text%>" runat="server"/>

The sever control will display the data when the DataBind() method is called on that control.

protected void Page_Load(object sender, EventArgs e)
        {
if (IsPostBack)
{
lblName.DataBind();
}

}

Types of Data binding:

There are two types of data binding available in ASP.NET
  •  Single-value data binding
    •  Displays the single line of data value at a time from the data source Eg: TextBox and Label Controls
  • Multiple values data binding 
    • Displays the multi line of data values at a time from the data source Eg: DropDownlist, ArrayList, GridView, ListView etc.


 Eg: Multiple Databinding

protected void Page_Load(object sender, EventArgs e)
        {

            ArrayList arry = new ArrayList();
            arry.Add("First");
            arry.Add("Second");
            arry.Add("Third");

            DropDown1.DataSource = arry;
            DropDown1.DataBind();
       }

No comments:

Post a Comment