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

ASP.NET Web应用关闭模态子页面后父页面GridView不刷新问题

问题根因

  1. 父页面GridView的绑定逻辑仅放在if (!IsPostBack)块中,只有页面首次加载时会执行,后续页面刷新/回发时不会重新查询最新数据完成绑定
  2. 子页面中Modal.Close执行优先级高于你注册的RefreshParentPage脚本,模态框关闭后脚本大概率未触发执行
  3. DayPilot模态框的closed回调中仅处理了日历控件的刷新,未触发GridView所在UpdatePanel的更新

修复步骤

1. 父页面抽离GridView绑定公共方法

将Parent.aspx.cs中原有绑定逻辑抽为可复用方法,同时补充回发时的绑定触发逻辑:

// 抽离GridView绑定逻辑
private void BindDeptGrid()
{
    string sqlquery = "";// 此处保留你原有的查询语句
    SqlDataAdapter da = new SqlDataAdapter(sqlquery);
    DataTable dt = new DataTable();
    da.Fill(dt);
    gv_dept.DataSource = dt;
    gv_dept.DataBind();  
    update_gv.Update();
}

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        BindDeptGrid();
    }
    else
    {
        // 判断是否为GridView更新触发的回发,是则刷新数据
        string eventTarget = Request.Params["__EVENTTARGET"];
        if (!string.IsNullOrEmpty(eventTarget) && eventTarget == update_gv.UniqueID)
        {
            BindDeptGrid();
        }
    }
}

2. 移除子页面冗余的刷新脚本

子页面Modal.Close已经可以给父页面回调传参,不需要额外注册刷新脚本,修改Child.aspx.cs代码:

protected async void ButtonOK_Click(object sender, EventArgs e)
{
    // 此处保留你原有的数据插入逻辑
    Hashtable ht = new Hashtable();
    ht["refresh"] = "yes";
    ht["message"] = "Created.";
    Modal.Close(this, ht);
    // 删除下方原有注册脚本的代码
    // Page.ClientScript.RegisterStartupScript(this.GetType(), "RefreshParentPage", "<script language='javascript'>RefreshParentPage();</script>");
}

3. 修改父页面模态框关闭回调,触发GridView刷新

修改父页面JS的createModal方法,在关闭回调中补充UpdatePanel刷新逻辑:

function createModal() {
    var modal = new DayPilot.Modal();
    modal.closed = function () {
        if (this.result && this.result.refresh) {
            dp_day.commandCallBack("refresh", { message: this.result.message });
            dp_week.commandCallBack("refresh", { message: this.result.message });
            dp_month.commandCallBack("refresh", { message: this.result.message });
            // 新增:触发UpdatePanel局部回发刷新GridView
            __doPostBack('<%= update_gv.UniqueID %>', '');
            
            // 可选方案:不需要局部刷新的话可以直接整页强制刷新,替换上面一行即可
            // window.location.reload(true);
        }
        dp_day.clearSelection();
        dp_week.clearSelection();
        dp_month.clearSelection();
    };

    return modal;
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.08 03:57:01