[ Team LiB ]Recipe 7.1 Binding Simple Data to Web Forms Controls Problem You need to bind a field of data to a server-side control.. The Web Forms page sample code displays the company
Trang 1[ Team LiB ]
Recipe 7.1 Binding Simple Data to Web Forms Controls
Problem
You need to bind a field of data to a server-side control
Solution
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
<asp:TextBox id="companyNameTextBox" style="Z-INDEX: 103; LEFT: 136px;
POSITION: absolute; TOP: 128px" runat="server" ReadOnly="True"
Width="280px" Text="<%# GetCompanyName(customerIdTextBox.Text) %>">
</asp:TextBox>
The code-behind contains one event and one method:
Page.Load
Binds data from the source—in this case the GetCompanyName( ) method—to 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;
Trang 2using 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(
ConfigurationSettings.AppSettings["DataConnectString"]);
SqlCommand cmd = new SqlCommand(sqlText, conn);
conn.Open( );
// Execute the command
companyName = cmd.ExecuteScalar().ToString( );
conn.Close( );
}
return companyName;
}
Discussion
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
Trang 3expressions 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
[ Team LiB ]