Binding Simple Data to Web Forms Controls

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 | Team LiB Recipe 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 CustomerlD 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 asp TextBox id companyNameTextBox style Z-INDEX 103 LEFT 136px POSITION absolute TOP 128px runat server ReadOnly True Width 280px Text GetCompanyName asp TextBox 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. 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 Namespaces variables and constants using System using using using . . . private void Page_Load object sender e public String GetCompanyName String customerld String companyName Not found. if Create a command to retrieve the company name for the user-specified customer ID. String sqlText SELECT CompanyName FROM Customers WHERE CustomerID . SqlConnection conn new SqlConnection DataConnectString SqlCommand cmd new SqlCommand sqlText conn Execute the command. companyName .ToString return companyName Discussion Simple data binding binds an 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

Bấm vào đây để xem trước nội dung
TÀI LIỆU MỚI ĐĂNG
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.