Binding Simple Data to Web Forms Controls
Số trang: 3
Loại file: pdf
Dung lượng: 24.10 KB
Lượt xem: 9
Lượt tải: 0
Xem trước 2 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
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
Nội dung trích xuất từ tài liệu:
Binding Simple Data to Web Forms Controls[ Team LiB ]Recipe 7.1 Binding Simple Data to Web Forms ControlsProblemYou need to bind a field of data to a server-side control.SolutionUse the DataBind( ) method.The Web Forms page sample code displays the company name for the CustomerIDspecified by assigning the method GetCompanyName( ), which is defined in the code-behind file, to the Text property of TextBox control companyNameTextBox. The codefor the Web Forms page is shown in Example 7-1.Example 7-1. File: ADOCookbookCS0701.aspxThe 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 constantsusing 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( ConfigurationSettings.AppSettings[DataConnectString]); SqlCommand cmd = new SqlCommand(sqlText, conn); conn.Open( ); // Execute the command. companyName = cmd.ExecuteScalar().ToString( ); conn.Close( ); } return companyName;}DiscussionSimple data binding binds an ASP.NET web control property to a single value in a datasource. The values can be determined at runtime. Although most commonly used to setcontrol properties to display data, any property of the control can be bound—forexample, the background color or size of the control.The Visual Studio .NET Properties window provides a tool to create data-bindingexpressions. 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 thatresolves to a single value. The data-binding expression is delimited with . Formore information about data-binding expressions, see the MSDN Library.In the solution, the Text property of the TextBox control is bound to a CompanyNamefield in the data source:Text=This sets the Text property to the value returned by the GetCompanyName( ) method inthe code-behind page.Instead of using an expression as previously shown, the static Eval( ) method of theDataBinder class can be used to simplify data binding when the value to bind is derivedfrom a data source. The DataBinder class helps to extract data from a data source andmakes 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 theEval( ) 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 thecontrols bind. This can be done explicitly by calling the DataBind( ) method of thecontrol.companyNameTextBox.DataBind( );The DataBind( ) method of the Page class can be called to data-bind all controls on theform.[ Team LiB ]
Nội dung trích xuất từ tài liệu:
Binding Simple Data to Web Forms Controls[ Team LiB ]Recipe 7.1 Binding Simple Data to Web Forms ControlsProblemYou need to bind a field of data to a server-side control.SolutionUse the DataBind( ) method.The Web Forms page sample code displays the company name for the CustomerIDspecified by assigning the method GetCompanyName( ), which is defined in the code-behind file, to the Text property of TextBox control companyNameTextBox. The codefor the Web Forms page is shown in Example 7-1.Example 7-1. File: ADOCookbookCS0701.aspxThe 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 constantsusing 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( ConfigurationSettings.AppSettings[DataConnectString]); SqlCommand cmd = new SqlCommand(sqlText, conn); conn.Open( ); // Execute the command. companyName = cmd.ExecuteScalar().ToString( ); conn.Close( ); } return companyName;}DiscussionSimple data binding binds an ASP.NET web control property to a single value in a datasource. The values can be determined at runtime. Although most commonly used to setcontrol properties to display data, any property of the control can be bound—forexample, the background color or size of the control.The Visual Studio .NET Properties window provides a tool to create data-bindingexpressions. 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 thatresolves to a single value. The data-binding expression is delimited with . Formore information about data-binding expressions, see the MSDN Library.In the solution, the Text property of the TextBox control is bound to a CompanyNamefield in the data source:Text=This sets the Text property to the value returned by the GetCompanyName( ) method inthe code-behind page.Instead of using an expression as previously shown, the static Eval( ) method of theDataBinder class can be used to simplify data binding when the value to bind is derivedfrom a data source. The DataBinder class helps to extract data from a data source andmakes 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 theEval( ) 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 thecontrols bind. This can be done explicitly by calling the DataBind( ) method of thecontrol.companyNameTextBox.DataBind( );The DataBind( ) method of the Page class can be called to data-bind all controls on theform.[ Team LiB ]
Tìm kiếm theo từ khóa liên quan:
công nghệ thông tin kỹ thuật lập trình Oreilly Ado Dot Net Cookbook Ebook-Lib Binding Simple Data to Web Forms ControlsGợi ý tài liệu liên quan:
-
52 trang 430 1 0
-
Top 10 mẹo 'đơn giản nhưng hữu ích' trong nhiếp ảnh
11 trang 314 0 0 -
74 trang 300 0 0
-
96 trang 293 0 0
-
Báo cáo thực tập thực tế: Nghiên cứu và xây dựng website bằng Wordpress
24 trang 289 0 0 -
Đồ án tốt nghiệp: Xây dựng ứng dụng di động android quản lý khách hàng cắt tóc
81 trang 281 0 0 -
EBay - Internet và câu chuyện thần kỳ: Phần 1
143 trang 275 0 0 -
Tài liệu dạy học môn Tin học trong chương trình đào tạo trình độ cao đẳng
348 trang 269 1 0 -
Tài liệu hướng dẫn sử dụng thư điện tử tài nguyên và môi trường
72 trang 265 0 0 -
Kỹ thuật lập trình trên Visual Basic 2005
148 trang 265 0 0