Nội dung Text: Binding Simple Data to Web Forms Controls
[ Team LiB ]
Recipe 7.1 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 source—in this case the GetCompanyName( ) method—to the
companyNameTextBox server control.
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
// . . .
private void Page_Load(object sender, System.EventArgs e)
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);
// Execute the command.
companyName = cmd.ExecuteScalar().ToString( );
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 bound—for
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 . 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:
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
• 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:
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
The DataBind( ) method of the Page class can be called to data-bind all controls on the
[ Team LiB ]