intTypePromotion=1
zunia.vn Tuyển sinh 2024 dành cho Gen-Z zunia.vn zunia.vn
ADSENSE

Binding Simple Data to Web Forms Controls

Chia sẻ: Bui Tuan | Ngày: | Loại File: PDF | Số trang:3

100
lượt xem
6
download
 
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

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

Chủ đề:
Lưu

Nội dung Text: Binding Simple Data to Web Forms Controls

  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 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;
  2. 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( 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
  3. 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: 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=" 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 ]
ADSENSE

CÓ THỂ BẠN MUỐN DOWNLOAD

 

Đồng bộ tài khoản
24=>0