DataGrid中的公共事件
来源:互联网 发布:dnf玩着玩着网络中断 编辑:程序博客网 时间:2024/04/26 07:57
DataGrid中的公共事件有:
对 DataGrid 控件中的某个项单击 Cancel 按钮时发生。
当服务器控件绑定到数据源时发生。
对 DataGrid 控件中的某个项单击 Delete 按钮时发生。
当从内存释放服务器控件时发生,这是请求 ASP.NET 页时服务器控件生存期的最后阶段。
对 DataGrid 控件中的某个项单击 Edit 按钮时发生。
当服务器控件初始化时发生;初始化是控件生存期的第一步。
当单击 DataGrid 控件中的任一按钮时发生。
当在 DataGrid 控件中创建项时在服务器上发生。
在项被数据绑定到 DataGrid 控件后发生。
当服务器控件加载到 Page 对象中时发生。
当单击页选择元素之一时发生。
当服务器控件将要呈现给其包含的 Page 对象时发生。
在两次服务器发送之间,在数据列表控件中选择了不同的项时发生。
对列进行排序时发生。
当服务器控件从内存中卸载时发生。
对 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数据绑定及初始化时各公共事件的执行顺序,如下图所示:
- DataGrid中的公共事件
- 处理在DataGrid中的DropDownList的事件
- 处理在DataGrid中的DropDownList的事件
- datagrid某列中的textbox触发事件
- DataGrid中的每一行,绑定onmouseover、onmousemove、onmouseout事件
- 在 winform 中的 datagrid 中实现单元格双击事件
- datagrid触发javascript事件
- DataGrid鼠标事件处理
- DataGrid鼠标事件方法
- DataGrid双击事件演示
- DataGrid添加事件句柄
- datagrid添加事件
- DataGrid鼠标事件处理
- DataGrid鼠标事件处理
- Datagrid事件响应(二)
- DataGrid鼠标事件方法
- DataGrid相关事件代码
- DataGrid 的ItemCreated事件
- 关于控制反转IoC--Inversion of Control Containers and the Dependency Injection pattern
- 文件的加锁解锁
- 转:Virtual Earth and AJAX--Part one
- DES加密算法详解
- Windows下JAVA环境变量的设置详解
- DataGrid中的公共事件
- 不用一行代码实现DBGRIDEh的标题按钮排序!
- MSVCR80D.dll not found 没有找到MSVCR80D.dll
- 【代码】一个简单的ATM提款机的DOS操作系统
- 转:Virtual Earth and AJAX--Part Two
- 通杀网页加密源代码
- 数据库权限
- 三峡流量计现场初步安装结束
- 水果阿生话说王开源