以GridView控件的RowUpdating事件说明如何退出事件

单击某一行的更新按钮以后,在 GridView 控件对该行进行更新之前,将引发 RowUpdating 事件。这使您可以提供一个这样的事件处理方法,即每次发生此事件时执行一个自定义例程(如取消更新操作)。

GridViewUpdateEventArgs 对象将被传递给事件处理方法,以便您可以确定当前行的索引,还可以指示应取消更新操作。若要取消更新操作,请将 GridViewUpdateEventArgs 对象的 Cancel 属性设置为 truee.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);

        }

    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值