Binding Simple Data to Web Forms Controls


You need to bind a field of data to a server-side control.


Use the DataBind( ) method.

The Web Forms page sample code displays the company name for the CustomerID specified by assigning the method GetCompanyName( ) , which is defined in the code-behind file, to the Text property of TextBox control companyNameTextBox . The code for the Web Forms page is shown in Example 7-1.

Example 7-1. File: ADOCookbookCS0701.aspx


The code-behind contains one event and one method:


Binds data from the sourcein this case the GetCompanyName( ) methodto the companyNameTextBox server control.

GetCompanyName( )

This method retrieves and returns the company name for a specified customer ID.

The C# code for the code-behind is shown in Example 7-2.

Example 7-2. File: ADOCookbookCS0701.aspx.cs

// Namespaces, variables, and constants
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

// . . . 

private void Page_Load(object sender, System.EventArgs e)
 companyNameTextBox.DataBind( );

public String GetCompanyName(String customerId)
 String companyName = "Not found.";

 if (customerIdTextBox.Text != "")
 // Create a command to retrieve the company name for the
 // user-specified customer ID.
 String sqlText =
 "SELECT CompanyName FROM Customers WHERE CustomerID='" +
 customerIdTextBox.Text + "'";
 SqlConnection conn = new SqlConnection(
 SqlCommand cmd = new SqlCommand(sqlText, conn);
 conn.Open( );
 // Execute the command.
 companyName = cmd.ExecuteScalar().ToString( );

 conn.Close( );

 return companyName;


Simple data binding binds an ASP.NET web control property to a single value in a data source. The values can be determined at runtime. Although most commonly used to set control properties to display data, any property of the control can be boundfor example, the background color or size of the control.

The Visual Studio .NET Properties window provides a tool to create data-binding expressions. It is accessed by clicking the ellipsis (...) in the (DataBindings) property.

To simple-bind a control, set the property of the control to a data-binding expression that resolves to a single value. The data-binding expression is delimited with <%# and #> . For more information about data-binding expressions, see the MSDN Library.

In the solution, the Text property of the TextBox control is bound to a CompanyName field in the data source:

Text="<%# GetCompanyName(customerIdTextBox.Text) %>

This sets the Text property to the value returned by the GetCompanyName( ) method in the code-behind page.

Instead of using an expression as previously shown, the static Eval( ) method of the DataBinder class can be used to simplify data binding when the value to bind is derived from a data source. The DataBinder class helps to extract data from a data source and makes it available to a control property. The Eval( ) method takes two arguments:

  • A reference to the data source object. This is usually a DataSet , DataTable , or DataView .
  • A string specifying the navigation path to the specific value in the data source. This usually references a row and a column in that row.

The syntax to retrieve the company name from the first row in a DataTable using the Eval( ) method instead of using a data-binding expression might be:

Text="<%# DataBinder.Eval(companyDataTable, "[0].CompanyName") %>

For more information about the DataBinder class and the syntax of the Eval( ) method, see the topic "Data-Binding Expressions for Web Forms Pages" in the MSDN Library.

Data-binding expressions must be resolved at runtime to provide the values to which the controls bind. This can be done explicitly by calling the DataBind( ) method of the control.

companyNameTextBox.DataBind( );

The DataBind( ) method of the Page class can be called to data-bind all controls on the form.

Connecting to Data

Retrieving and Managing Data

Searching and Analyzing Data

Adding and Modifying Data

Copying and Transferring Data

Maintaining Database Integrity

Binding Data to .NET User Interfaces

Working with XML

Optimizing .NET Data Access

Enumerating and Maintaining Database Objects

Appendix A. Converting from C# to VB Syntax

ADO. NET Cookbook
ADO.NET 3.5 Cookbook (Cookbooks (OReilly))
ISBN: 0596101406
EAN: 2147483647
Year: 2002
Pages: 222
Authors: Bill Hamilton © 2008-2020.
If you may any questions please contact us: