You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET GridView控件RowUpdating事件未触发如何解决

GridView 行更新事件不触发排查方案

触发条件校验

  • 行内更新按钮的CommandName属性必须设为固定值Update,自定义其他值只会触发RowCommand,不会触发RowUpdating事件。
  • 确认GridView控件已正确声明OnRowUpdating绑定,且后台方法签名与事件要求一致,参数无错误、无重名。

回发逻辑排查

  • Page_Load中绑定GridView必须加!IsPostBack判断,每次回发都重新绑定会清空事件队列,导致RowUpdating事件被拦截。正确绑定逻辑参考:
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        // 仅首次页面加载时绑定数据
        GridView1.DataSource = 数据源;
        GridView1.DataBind();
    }
}
  • 检查RowCommand事件中是否执行了重绑定GridView、页面跳转、e.Cancel=true等操作,这类操作会中断后续事件执行流程。

控件属性校验

  • 确认GridView的EnableViewState属性为true,关闭视图状态会导致事件绑定丢失,无法触发RowUpdating。
  • 确认TemplateField中的所有服务端控件都添加了runat="server"标识,且ID无重复冲突。

自定义触发方案

如果需要使用自定义CommandName实现更新,可以在RowCommand中手动调用RowUpdating逻辑:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if(e.CommandName == "你的自定义更新命令名")
    {
        int rowIndex = Convert.ToInt32(e.CommandArgument);
        GridViewUpdateEventArgs updateArgs = new GridViewUpdateEventArgs(rowIndex);
        GridView1_RowUpdating(sender, updateArgs);
    }
}

内容的提问来源于stack exchange,提问作者user3899176

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.15 03:36:04