Binding Complex Data to Web Forms Controls
Số trang: 3
Loại file: pdf
Dung lượng: 15.05 KB
Lượt xem: 12
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:
[ Team LiB ] Recipe 7.2 Binding Complex Data to Web Forms Controls Problem You want to bind multiple columns in multiple records to an ASP.NET control. Solution Set the controls advanced properties (see Table 7-1) before calling DataBind( ).
Nội dung trích xuất từ tài liệu:
Binding Complex Data to Web Forms Controls[ Team LiB ]Recipe 7.2 Binding Complex Data to Web Forms ControlsProblemYou want to bind multiple columns in multiple records to an ASP.NET control.SolutionSet the controls advanced properties (see Table 7-1) before calling DataBind( ).The Web Forms page sample code defines the ListBox that is populated with data by thecode-behind page logic. The code for the Web Forms page is shown in Example 7-3.Example 7-3. File: ADOCookbookCS0702.aspxThe code-behind contains one event handler:Page.Load Fills a DataTable with the CategoryID and CategoryName from the Categories table in the Northwind sample database. The ListBox server control is bound to the DataTable.The C# code for the code-behind is shown in Example 7-4.Example 7-4. File: ADOCookbookCS0702.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){ // Create a DataAdapter and use it to retrieve ID and Name // for all categories. String sqlText = SELECT CategoryID, CategoryName FROM Categories + ORDER BY CategoryName; SqlDataAdapter da = new SqlDataAdapter(sqlText, ConfigurationSettings.AppSettings[DataConnectString]); DataTable table = new DataTable(Categories); da.Fill(table); // Bind the table to the list box control. categoriesListBox.DataSource = table.DefaultView; categoriesListBox.DataValueField = CategoryID; categoriesListBox.DataTextField = CategoryName; categoriesListBox.DataBind( );}DiscussionComplex data binding describes binding a multi-record control to multiple records in adata source. The DataGrid, DataList, and ListBox controls are examples of controls thatsupport complex data binding.Each control that supports complex data binding exposes a set of properties, which areslightly different for each control, that control the binding. These properties are describedin Table 7-1. Table 7-1. Complex data-binding properties Property Description Gets or sets the data source that the control is displaying data for. Valid data sources include DataTable, DataView, DataSet,DataSource DataViewManager, or any object that implements the IEnumerable interface. Gets or sets the table in the data source to bind to the control. You canDataMember use this property if the data source contains more than one table—a DataSet, for example. Gets or sets the key field in the data source. This allows the key fieldDataKeyField for a listing control to be stored and later accessed without displaying it in the control.DataValueField Gets or sets the field in the data source that provides the value for the control when an item is selected. Gets or sets the field in the data source that provides the display valueDataTextField for the control when an item is selected.After the properties appropriate to the control are set, call the DataBind( ) method of thecontrol or of the Page to bind the data source to the server control.[ Team LiB ]
Nội dung trích xuất từ tài liệu:
Binding Complex Data to Web Forms Controls[ Team LiB ]Recipe 7.2 Binding Complex Data to Web Forms ControlsProblemYou want to bind multiple columns in multiple records to an ASP.NET control.SolutionSet the controls advanced properties (see Table 7-1) before calling DataBind( ).The Web Forms page sample code defines the ListBox that is populated with data by thecode-behind page logic. The code for the Web Forms page is shown in Example 7-3.Example 7-3. File: ADOCookbookCS0702.aspxThe code-behind contains one event handler:Page.Load Fills a DataTable with the CategoryID and CategoryName from the Categories table in the Northwind sample database. The ListBox server control is bound to the DataTable.The C# code for the code-behind is shown in Example 7-4.Example 7-4. File: ADOCookbookCS0702.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){ // Create a DataAdapter and use it to retrieve ID and Name // for all categories. String sqlText = SELECT CategoryID, CategoryName FROM Categories + ORDER BY CategoryName; SqlDataAdapter da = new SqlDataAdapter(sqlText, ConfigurationSettings.AppSettings[DataConnectString]); DataTable table = new DataTable(Categories); da.Fill(table); // Bind the table to the list box control. categoriesListBox.DataSource = table.DefaultView; categoriesListBox.DataValueField = CategoryID; categoriesListBox.DataTextField = CategoryName; categoriesListBox.DataBind( );}DiscussionComplex data binding describes binding a multi-record control to multiple records in adata source. The DataGrid, DataList, and ListBox controls are examples of controls thatsupport complex data binding.Each control that supports complex data binding exposes a set of properties, which areslightly different for each control, that control the binding. These properties are describedin Table 7-1. Table 7-1. Complex data-binding properties Property Description Gets or sets the data source that the control is displaying data for. Valid data sources include DataTable, DataView, DataSet,DataSource DataViewManager, or any object that implements the IEnumerable interface. Gets or sets the table in the data source to bind to the control. You canDataMember use this property if the data source contains more than one table—a DataSet, for example. Gets or sets the key field in the data source. This allows the key fieldDataKeyField for a listing control to be stored and later accessed without displaying it in the control.DataValueField Gets or sets the field in the data source that provides the value for the control when an item is selected. Gets or sets the field in the data source that provides the display valueDataTextField for the control when an item is selected.After the properties appropriate to the control are set, call the DataBind( ) method of thecontrol or of the Page to bind the data source to the server control.[ 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 Complex 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