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.
Example 39 SimpleBinding.aspx |
<Script Runat="Server">
Sub Page_Load
lbMessage.DataBind()
End Sub
Function GetTime() As String
Return DateTime.Now.ToString( "T" )
End Function
</Script>
<html>
<head><title>SimpleBinding.aspx</title></head>
<body>
<form Runat="Server">
<asp:Label ID="lbMessage" Text='<%# GetTime() %>'
Runat="Server" />
</form>
</body>
</html> |
| |
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.
s

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: -
| Example 40 RepeaterExample.aspx |
<%@ Import Namespace="System.Data.SqlClient" %>
<Script Runat="Server">
Sub Page_Load
Dim conNorthwind As SqlConnection
Dim cmdSelect As SqlCommand
Dim dtrEmployees As SqlDataReader
Retrieve records from database
conNorthwind = New SqlConnection( "Server=localhost;UID=sa;PWD=secret;Database=Northwind" )
cmdSelect = New SqlCommand( "Select * From Employees", conNorthwind )
conNorthwind. Open()
DtrEmployees = cmdSelect.ExecuteReader()
Bind to Repeater
rptEmployees.DataSource = dtrEmployees
rptEmployees.DataBind()
dtrEmployees.Close()
conNorthwind.Close()
End Sub
</Script>
<html>
<head><title>RepeaterExample.aspx</title></head>
<body>
<form Runat="Server">
<asp:Repeater
ID="rptEmployees"
Runat="Server">
<ItemTemplate>
<%# Container.DataItem( "FirstName" ) %>
</ItemTemplate>
</asp:Repeater>
</form>
</body>
</html> |
| |
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.
|