Data binding gives extensive control over data. It refers to the process of assigning a value to a property of a runtime. For example, you can use data binding to bind the properties of a control to a data source such as the contents of a SQL Server database table. Any type of data can be bounded to any control or property of control on an ASP.NET page. Therefore this provides a check over the data movement from the data-store to the page and back again. The data can be displayed to the user, to use it for setting the style properties on a control, and allow the user to modify the data directly and update the data store.
A DataBinding class contains information about a single data-binding expression in an ASP.NET server control, which allows rapid-application development (RAD) designers, such as Visual Studio .NET, to create data-binding expressions at the time of design. This class cannot be inherited.
The following example binds the value of a function to the Text property of a Label control.
Look at the Text property of the label control: -
Text='<%# GetTime () %>'
Special tags <%# and %> are being used here to delimit the data binding expression. Here, the value of the DataBinding expression is the value of the GetTime () function. In the subroutine Page_Load the DataBind () method is called on the Label control named lblMessage. When the DataBind ()method is called, the value of the GetTime () function is assigned to the Text property of the Label Control.
How to Bind a Server Control to a Data Source
This section deals with the use of DataBinding with Repeater, an important ASP.NET server control. The Repeater control does not display anything unless it is bound to a data source. Typically Repeater control is used with database applications.
Let us suppose that you have Employees table in your database and you want to display all the rows from this database table within an ASP.NET page. The following example demonstrates it: -
The output of this example is shown below:
In the above example all the rows are displayed through a SqlDataReader. Next, the SqlDataReader is assigned to the DataSource property of the Repeater control. Finally, the DataBind() method is called and the items from the SqlDataReader are bound to the Repeater control.