单击某一行的“更新”按钮以后,在 GridView 控件对该行进行更新之前,将引发 RowUpdating 事件。这使您可以提供一个这样的事件处理方法,即每次发生此事件时执行一个自定义例程(如取消更新操作)。
GridViewUpdateEventArgs 对象将被传递给事件处理方法,以便您可以确定当前行的索引,还可以指示应取消更新操作。若要取消更新操作,请将 GridViewUpdateEventArgs 对象的 Cancel 属性设置为 true。即e.Cancel = true; 此方法也适用于删除、编辑等事件。
具体用法:将事件参数的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);
}
}