Binding Data to a Web Forms DataList
Số trang: 9
Loại file: pdf
Dung lượng: 29.10 KB
Lượt xem: 10
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.3 Binding Data to a Web Forms DataList Problem You need to bind the result set from a query to a DataList control. Solution Set the DataLists advanced properties as demonstrated by this solution. The schema of table TBL0703 that is used in the solution is shown in Table 7-2. Table 7-2.
Nội dung trích xuất từ tài liệu:
Binding Data to a Web Forms DataList [ Team LiB ]Recipe 7.3 Binding Data to a Web Forms DataListProblemYou need to bind the result set from a query to a DataList control.SolutionSet the DataLists advanced properties as demonstrated by this solution.The schema of table TBL0703 that is used in the solution is shown in Table 7-2. Table 7-2. TBL0703 schema Column name Data type Length Allow nulls?Id int 4 NoIntField int 4 YesStringField nvarchar 50 YesThe Web Forms page sample code defines the DataList control and the three templates—SelectedItemTemplate, ItemTemplate, and EditItemTemplate—which control the displayof data for selected items, unselected items, and items being edited. The static Eval( )method of the DataBinder class is used to fill the field values in each template.Container.DataItem specifies the container argument for the method which when used ina list in a template resolves to DataListItem.DataItem. The code for the Web Forms pageis shown Example 7-5.Example 7-5. File: ADOCookbookCS0703.aspx ; ; ; ; ID: IntField: StringField: The code-behind contains six event handlers and three methods:Page.Load Calls the CreateDataSource( ) method and binds data to the Web Forms DataList, if the page is being loaded for the first time.CreateDataSource( ) This method fills a DataTable with the TBL0703 table and stores the DataTable to a Session variable to cache the data source for the DataList.UpdateDataSource( ) This method creates a DataAdapter and uses it together with updating logic generated by a CommandBuilder to update the data source with changes made to the cached DataTable. The updated DataTable is stored to the Session variable, which is used to cache the data source for the DataList.BindDataList( ) This method gets the cached data from the Session variable and binds its default view to the DataList.DataList.CancelCommand Sets the index of the item being edited to -1 to cancel any current editing and calls BindDataList( ) to refresh the list.DataList.DeleteCommand Finds and deletes the specified row from the data cached in the Session variable and calls the UpdateDataSource( ) method to persist the change back to the data source. BindDataList( ) is called to refresh the list.DataList.EditCommand Sets the index of the selected item to -1 to cancel its selection. The index of the item being edited is then set to the index of the row corresponding to the Edit button putting that row into edit mode. BindDataList( ) is called to refresh the list.DataList.ItemCommand Checks if the Select button was pressed. If it was, the index of the item being edited is set to -1 to cancel its editing. The index of the selected item is then set to the index of the row corresponding to the Select button to put that row into select mode. Finally, BindDataList( ) is called to refresh the list.DataList.UpdateCommand Finds and updates the specified row in the data cached in the Session variable and calls the UpdateDataSource( ) method to persist the change back to the data source. BindDataList( ) is called to refresh the list.The C# code for the code-behind is shown in Example 7-6.Example 7-6. File: ADOCookbookCS0703.aspx.cs// Namespaces, variables, and constantsusing System;using System.Configuration;using System.Web.UI.WebControls;using System.Data;using System.Data.SqlClient;private const String TABLENAME = TBL0703;// . . .private void Page_Load(object sender, System.EventArgs e){ if(!Page.IsPostBack) { dataList.DataSource = CreateDataSource( ); dataList.DataKeyField = Id; dataList.DataBind( ); }}private DataTable CreateDataSource( ){ DataTable dt = new DataTable(TABLENAME); // Create the DataAdapter and fill the table using it. SqlDataAdapter da = new SqlDataAdapter(SELECT * FROM + TABLENAME + ORDER BY Id, ConfigurationSettings.AppSettings[DataConnectString]); da.Fill(dt); da.FillSchema(dt, SchemaType.Source); // Store data in session variable to store data between // posts to server. Session[DataSource] = dt; return dt;}private DataTable UpdateDataSource(DataTable dt){ // Create a DataAdapter for the update. SqlDataAdapter da = new SqlDataAdapter(SELECT * FROM + TABLENAME + ORDER BY Id, ConfigurationSettings.AppSettings[DataConnectString]); // Create a CommandBuilder to generate update logic. SqlCommandBuilder cb = new SqlCommandBuilder(da); // Update the data source with changes to the table. da.Update(dt); // Store updated data in session variable to store data between // posts to server. Session[DataSource] = dt; return dt;}private void BindDataList( ){ // Get the data from the session variable. DataView dv = ((DataTable)Session[DataSource]).DefaultView; // Bind the data view to the data list. dataList.DataSource = dv; dataList.DataBind( );}private void dataList_CancelCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e){ // Set the index of the item being edited out of range. dataList.EditItemIndex = -1; BindDataList( );}private void dataList_DeleteCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e){ // Get the data from the session variable. DataTable dt = (DataTable)Session[Data ...
Nội dung trích xuất từ tài liệu:
Binding Data to a Web Forms DataList [ Team LiB ]Recipe 7.3 Binding Data to a Web Forms DataListProblemYou need to bind the result set from a query to a DataList control.SolutionSet the DataLists advanced properties as demonstrated by this solution.The schema of table TBL0703 that is used in the solution is shown in Table 7-2. Table 7-2. TBL0703 schema Column name Data type Length Allow nulls?Id int 4 NoIntField int 4 YesStringField nvarchar 50 YesThe Web Forms page sample code defines the DataList control and the three templates—SelectedItemTemplate, ItemTemplate, and EditItemTemplate—which control the displayof data for selected items, unselected items, and items being edited. The static Eval( )method of the DataBinder class is used to fill the field values in each template.Container.DataItem specifies the container argument for the method which when used ina list in a template resolves to DataListItem.DataItem. The code for the Web Forms pageis shown Example 7-5.Example 7-5. File: ADOCookbookCS0703.aspx ; ; ; ; ID: IntField: StringField: The code-behind contains six event handlers and three methods:Page.Load Calls the CreateDataSource( ) method and binds data to the Web Forms DataList, if the page is being loaded for the first time.CreateDataSource( ) This method fills a DataTable with the TBL0703 table and stores the DataTable to a Session variable to cache the data source for the DataList.UpdateDataSource( ) This method creates a DataAdapter and uses it together with updating logic generated by a CommandBuilder to update the data source with changes made to the cached DataTable. The updated DataTable is stored to the Session variable, which is used to cache the data source for the DataList.BindDataList( ) This method gets the cached data from the Session variable and binds its default view to the DataList.DataList.CancelCommand Sets the index of the item being edited to -1 to cancel any current editing and calls BindDataList( ) to refresh the list.DataList.DeleteCommand Finds and deletes the specified row from the data cached in the Session variable and calls the UpdateDataSource( ) method to persist the change back to the data source. BindDataList( ) is called to refresh the list.DataList.EditCommand Sets the index of the selected item to -1 to cancel its selection. The index of the item being edited is then set to the index of the row corresponding to the Edit button putting that row into edit mode. BindDataList( ) is called to refresh the list.DataList.ItemCommand Checks if the Select button was pressed. If it was, the index of the item being edited is set to -1 to cancel its editing. The index of the selected item is then set to the index of the row corresponding to the Select button to put that row into select mode. Finally, BindDataList( ) is called to refresh the list.DataList.UpdateCommand Finds and updates the specified row in the data cached in the Session variable and calls the UpdateDataSource( ) method to persist the change back to the data source. BindDataList( ) is called to refresh the list.The C# code for the code-behind is shown in Example 7-6.Example 7-6. File: ADOCookbookCS0703.aspx.cs// Namespaces, variables, and constantsusing System;using System.Configuration;using System.Web.UI.WebControls;using System.Data;using System.Data.SqlClient;private const String TABLENAME = TBL0703;// . . .private void Page_Load(object sender, System.EventArgs e){ if(!Page.IsPostBack) { dataList.DataSource = CreateDataSource( ); dataList.DataKeyField = Id; dataList.DataBind( ); }}private DataTable CreateDataSource( ){ DataTable dt = new DataTable(TABLENAME); // Create the DataAdapter and fill the table using it. SqlDataAdapter da = new SqlDataAdapter(SELECT * FROM + TABLENAME + ORDER BY Id, ConfigurationSettings.AppSettings[DataConnectString]); da.Fill(dt); da.FillSchema(dt, SchemaType.Source); // Store data in session variable to store data between // posts to server. Session[DataSource] = dt; return dt;}private DataTable UpdateDataSource(DataTable dt){ // Create a DataAdapter for the update. SqlDataAdapter da = new SqlDataAdapter(SELECT * FROM + TABLENAME + ORDER BY Id, ConfigurationSettings.AppSettings[DataConnectString]); // Create a CommandBuilder to generate update logic. SqlCommandBuilder cb = new SqlCommandBuilder(da); // Update the data source with changes to the table. da.Update(dt); // Store updated data in session variable to store data between // posts to server. Session[DataSource] = dt; return dt;}private void BindDataList( ){ // Get the data from the session variable. DataView dv = ((DataTable)Session[DataSource]).DefaultView; // Bind the data view to the data list. dataList.DataSource = dv; dataList.DataBind( );}private void dataList_CancelCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e){ // Set the index of the item being edited out of range. dataList.EditItemIndex = -1; BindDataList( );}private void dataList_DeleteCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e){ // Get the data from the session variable. DataTable dt = (DataTable)Session[Data ...
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 Data to a Web Forms DataListTài liệu có liên quan:
-
52 trang 468 1 0
-
Top 10 mẹo 'đơn giản nhưng hữu ích' trong nhiếp ảnh
11 trang 367 0 0 -
96 trang 334 0 0
-
74 trang 329 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 321 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 321 1 0 -
Kỹ thuật lập trình trên Visual Basic 2005
148 trang 310 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 304 0 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 303 0 0 -
EBay - Internet và câu chuyện thần kỳ: Phần 1
143 trang 297 0 0