DataGrid中的公共事件

DataGrid中的公共事件有:

CancelCommand

DataGrid 控件中的某个项单击 Cancel 按钮时发生。

DataBinding(从 Control 继承)

当服务器控件绑定到数据源时发生。

DeleteCommand

DataGrid 控件中的某个项单击 Delete 按钮时发生。

Disposed(从 Control 继承)

当从内存释放服务器控件时发生,这是请求 ASP.NET 页时服务器控件生存期的最后阶段。

EditCommand

DataGrid 控件中的某个项单击 Edit 按钮时发生。

Init(从 Control 继承)

当服务器控件初始化时发生;初始化是控件生存期的第一步。

ItemCommand

当单击 DataGrid 控件中的任一按钮时发生。

ItemCreated

当在 DataGrid 控件中创建项时在服务器上发生。

ItemDataBound

在项被数据绑定到 DataGrid 控件后发生。

Load(从 Control 继承)

当服务器控件加载到 Page 对象中时发生。

PageIndexChanged

当单击页选择元素之一时发生。

PreRender(从 Control 继承)

当服务器控件将要呈现给其包含的 Page 对象时发生。

SelectedIndexChanged(从 BaseDataList 继承)

在两次服务器发送之间,在数据列表控件中选择了不同的项时发生。

SortCommand

对列进行排序时发生。

Unload(从 Control 继承)

当服务器控件从内存中卸载时发生。

UpdateCommand

DataGrid 控件中的某个项单击 Update 按钮时发生。

 其中,Control 类定义由所有 ASP.NET 服务器控件共享的属性、方法和事件。

 

一、DataGrid编辑事件的整合

对数据进行编辑操作,包括更新、删除、取消、编辑等,有两种方式响应按钮事件:

1、对于每一种按钮分别引发事件,取消、删除、编辑、更新分别对应CancelCommandDeleteCommandEditCommandUpdateCommand事件。

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数据绑定及初始化时各公共事件的执行顺序,如下图所示:

 

 
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值