以GridView控件的RowUpdating事件说明如何退出事件
2008-09-09 22:07
295 查看
单击某一行的“更新”按钮以后,在 GridView 控件对该行进行更新之前,将引发 RowUpdating 事件。这使您可以提供一个这样的事件处理方法,即每次发生此事件时执行一个自定义例程(如取消更新操作)。
GridViewUpdateEventArgs 对象将被传递给事件处理方法,以便您可以确定当前行的索引,还可以指示应取消更新操作。若要取消更新操作,请将 GridViewUpdateEventArgs 对象的 Cancel 属性设置为 true。即[/b]e.Cancel = true; [/b]此方法也适用于删除、编辑等事件。[/b][/b]
具体用法:将事件参数的Cancel属性设置为True,表示退出该事件:
例:
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
TextBox txtpro=(TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0];
try
{
if(txtpro==null)
{
Response.Write(bc.messagbox("无法获取专业名称,请与管理员联系!"));
e.Cancel = true; //退出更新事件
return;
}
else if(txtpro.Text=="")
{
e.Cancel = true; //退出更新事件
Response.Write(bc.messagbox("专业名称不能为空!"));
return;
}
SqlDataSource1.UpdateParameters[0].DefaultValue = GridView1.DataKeys[e.RowIndex].Value.ToString();
SqlDataSource1.UpdateParameters[1].DefaultValue = txtpro.Text.Trim();
SqlDataSource1.UpdateParameters[2].DefaultValue = DateTime.Now.ToShortDateString().ToString();
SqlDataSource1.Update();
}
catch (Exception ex)
{
Response.Write(bc.messagbox(ex.Message));
throw new Exception(ex.Message);
}
}
GridViewUpdateEventArgs 对象将被传递给事件处理方法,以便您可以确定当前行的索引,还可以指示应取消更新操作。若要取消更新操作,请将 GridViewUpdateEventArgs 对象的 Cancel 属性设置为 true。即[/b]e.Cancel = true; [/b]此方法也适用于删除、编辑等事件。[/b][/b]
具体用法:将事件参数的Cancel属性设置为True,表示退出该事件:
例:
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
TextBox txtpro=(TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0];
try
{
if(txtpro==null)
{
Response.Write(bc.messagbox("无法获取专业名称,请与管理员联系!"));
e.Cancel = true; //退出更新事件
return;
}
else if(txtpro.Text=="")
{
e.Cancel = true; //退出更新事件
Response.Write(bc.messagbox("专业名称不能为空!"));
return;
}
SqlDataSource1.UpdateParameters[0].DefaultValue = GridView1.DataKeys[e.RowIndex].Value.ToString();
SqlDataSource1.UpdateParameters[1].DefaultValue = txtpro.Text.Trim();
SqlDataSource1.UpdateParameters[2].DefaultValue = DateTime.Now.ToShortDateString().ToString();
SqlDataSource1.Update();
}
catch (Exception ex)
{
Response.Write(bc.messagbox(ex.Message));
throw new Exception(ex.Message);
}
}
相关文章推荐
- ASP.NET2.0中的GRIDVIEW控件在使用TemplateField中的LinkButton时如何在RowCommand事件中找到当前行index的方法
- 如何:响应 GridView 控件中的按钮事件
- ASP.NET2.0中的GRIDVIEW控件在使用TemplateField中的LinkButton时如何在RowCommand事件中找到当前行index的方法
- ASP.NET2.0中的GRIDVIEW控件在使用TemplateField中的LinkButton时如何在RowCommand事件中找到当前行index的方法
- C#.NET GridView 中如何获取脚中的控件ID并添加客户端事件
- 如何:响应 GridView 控件中的按钮事件
- 如何:响应 GridView 控件中的按钮事件
- 如何在GridView的OnRowCommand事件 用FindControl找到模版控件?
- 菜鸟学Asp.Net——GridView控件之RowDataBind事件中如何获取已绑定的数据
- 如何使用 RowDataBound 事件在数据源中的字段值显示在 GridView 控件中之前修改该值
- 如何判断GridView控件的某一行是否被选中
- [原创]GridView各事件获取处理行索引及该行控件方法
- C#.NET常见问题(FAQ)-如何捕捉窗体关闭的事件,弹窗确认是否退出
- 在GridView中添加按钮后,如何触发按钮的各种事件? [转帖]
- GridView控件点击单元格如何获取该列的列标题
- [VB.NET]如何捕捉datagridview控件中combox内容改变的事件?
- GridView控件RowDataBound事件中获取列字段
- 关于如何反射出GridView中控件的问题(如GridView中的Button)???
- 编写一个浮动按钮控件(如何给控件添加MouseOut事件)
- 鼠标在某个控件上按下,然后离开后弹起,如何捕获这个鼠标弹起事件