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

仅通过C#实现关闭ModalPopupExtender后刷新父页面是否可行?

Refresh Parent Page After Closing ModalPopupExtender (C# Only Approach)

Hey there! I totally get the frustration of sifting through JavaScript solutions when you're more comfortable working with C#. Let's walk through how you can handle this using mostly C#—no need to write custom JS from scratch.

First, a quick reality check: Since page refreshes happen on the client side, we can't avoid sending a tiny bit of JavaScript to the browser, but we can inject it directly from C# so you don't have to mess with separate scripts or inline JS. Here's how:

Method 1: Inject Refresh Script via C#

Configure your modal's close button to trigger a server-side click event. In that event handler, run your server-side logic (like saving form data) and then use ScriptManager to send a refresh command to the client:

protected void btnCloseModal_Click(object sender, EventArgs e)
{
    // Do your server-side work here (e.g., save data from the modal)
    
    // Inject the refresh script using C#
    ScriptManager.RegisterStartupScript(
        this, 
        GetType(), 
        "refreshParentPage", 
        "window.location.reload();", 
        true
    );
}

This method lets you handle everything from your C# codebehind—you're just using C# to send the necessary client-side command.

Method 2: Refresh a Parent UpdatePanel (Partial Rendering)

If your parent page uses an UpdatePanel for partial updates, you can force that panel to refresh after closing the modal instead of reloading the entire page. This is more efficient for most cases:

  1. Make sure your modal's close button is set to trigger a postback (add OnClick="btnCloseModal_Click" to the button).
  2. In your C# handler, update the parent UpdatePanel:
protected void btnCloseModal_Click(object sender, EventArgs e)
{
    // Server-side logic goes here
    
    // Refresh the parent UpdatePanel
    UpdatePanelParent.Update();
}
  1. Ensure the close button is added as a trigger for the parent UpdatePanel (either in the markup or codebehind):
<asp:UpdatePanel ID="UpdatePanelParent" runat="server">
    <Triggers>
        <asp:PostBackTrigger ControlID="btnCloseModal" />
    </Triggers>
    <!-- Parent page content here -->
</asp:UpdatePanel>

Method 3: Full Page Postback (No Injected JS)

If you want to avoid even the tiny bit of JS from Method 1, you can set up the close button to trigger a full page postback. Just make sure the button is not inside an UpdatePanel—clicking it will automatically reload the entire parent page once the server-side logic completes.

This is straightforward but less efficient if you only need to refresh part of the page.


Hope these options work for you! They all let you stay within your C# workflow without having to dive into complex JavaScript.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 07:20:34