DataGrid中的公共事件有:
对 DataGrid 控件中的某个项单击 Cancel 按钮时发生。 | |
DataBinding(从 Control 继承) | 当服务器控件绑定到数据源时发生。 |
对 DataGrid 控件中的某个项单击 Delete 按钮时发生。 | |
Disposed(从 Control 继承) | 当从内存释放服务器控件时发生,这是请求 ASP.NET 页时服务器控件生存期的最后阶段。 |
对 DataGrid 控件中的某个项单击 Edit 按钮时发生。 | |
Init(从 Control 继承) | 当服务器控件初始化时发生;初始化是控件生存期的第一步。 |
当单击 DataGrid 控件中的任一按钮时发生。 | |
当在 DataGrid 控件中创建项时在服务器上发生。 | |
在项被数据绑定到 DataGrid 控件后发生。 | |
Load(从 Control 继承) | 当服务器控件加载到 Page 对象中时发生。 |
当单击页选择元素之一时发生。 | |
PreRender(从 Control 继承) | 当服务器控件将要呈现给其包含的 Page 对象时发生。 |
SelectedIndexChanged(从 BaseDataList 继承) | 在两次服务器发送之间,在数据列表控件中选择了不同的项时发生。 |
对列进行排序时发生。 | |
Unload(从 Control 继承) | 当服务器控件从内存中卸载时发生。 |
对 DataGrid 控件中的某个项单击 Update 按钮时发生。 |
其中,Control 类定义由所有 ASP.NET 服务器控件共享的属性、方法和事件。
一、DataGrid编辑事件的整合
对数据进行编辑操作,包括更新、删除、取消、编辑等,有两种方式响应按钮事件:
1、对于每一种按钮分别引发事件,取消、删除、编辑、更新分别对应CancelCommand、DeleteCommand、EditCommand、UpdateCommand事件。
2、当单击 DataGrid 控件中的任一按钮时都引发 ItemCommand 事件。此事件常用于处理在 DataGrid 控件中具有自定义 CommandName 值(如 Add)的按钮控件。
现有一DataGrid,对其中的数据进行编辑、更新、取消、删除操作,采用方式一如下:
private void DataGridCounsellor_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) { this.DataGridCounsellor.EditItemIndex = -1; this.BindGrid(counsellorDataTable); }
private void DataGridCounsellor_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) { this.DataGridCounsellor.EditItemIndex = e.Item.ItemIndex; this.BindGrid(counsellorDataTable); }
private void DataGridCounsellor_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) { Counsellor counsellor = new Counsellor(); counsellor.CounsellorID = e.Item.Cells[0].Text.Trim(); counsellor.CounsellorEmail = ((TextBox)e.Item.FindControl("TextBoxUpdateEmail")).Text.Trim(); counsellor.UpdateCounsellor(); this.DataGridCounsellor.EditItemIndex = -1; this.BindGrid(counsellorDataTable); }
private void DataGridCounsellor_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) { Counsellor counsellor = new Counsellor(); counsellor.CounsellorID = e.Item.Cells[0].Text; counsellor.DeleteCounsellor(); this.DataGridCounsellor.EditItemIndex = -1; this.BindGrid(counsellorDataTable); } |
采用方式二如下:
private void DataGridCounsellor_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) { switch(e.CommandName) { case "Edit": this.DataGridCounsellor.EditItemIndex = e.Item.ItemIndex; this.BindGrid(counsellorDataTable); break;
case "Cancel": this.DataGridCounsellor.EditItemIndex = -1; this.BindGrid(counsellorDataTable); break;
case "Update": Counsellor counsellor = new Counsellor(); counsellor.CounsellorID = e.Item.Cells[0].Text.Trim(); counsellor.CounsellorEmail = ((TextBox)e.Item.FindControl("TextBoxUpdateEmail")).Text.Trim(); counsellor.UpdateCounsellor(); this.DataGridCounsellor.EditItemIndex = -1; this.BindGrid(counsellorDataTable); break;
case "Delete": Counsellor counsellor = new Counsellor(); counsellor.CounsellorID = e.Item.Cells[0].Text; counsellor.DeleteCounsellor(); this.DataGridCounsellor.EditItemIndex = -1; this.BindGrid(counsellorDataTable); break; } } |
对比代码,发现方式二更简洁清晰,便于阅读和修改。
应用:以后运用DataGrid中的数据进行编辑操作时,一般情况下均用方式二,除非特殊情况如只有删除功能则采用方式一。
二、DataGrid数据绑定过程各事件的执行顺序
了解各事件的执行顺序及事件执行的含义,便于在以后的编程中灵活运用。
以页面初始化绑定DataGrid为例来跟踪得出DataGrid数据绑定及初始化时各公共事件的执行顺序,如下图所示:
|