Skip to main content
 首页 » 编程设计

c#之ASP.NET UpdatePanel 不适用于 Page_Load 中的 Response.Write

2024年02月06日19emanlee

我有那个例子:

<asp:ScriptManager ID="ScriptManager1" runat="server"> 
    </asp:ScriptManager> 
    <asp:UpdatePanel ID="UpdatePanel1" runat="server"> 
        <ContentTemplate> 
            <fieldset> 
                <legend>UpdatePanel</legend> 
                <asp:Label ID="Label1" runat="server" Text="Panel created."></asp:Label><br /> 
            </fieldset> 
        </ContentTemplate> 
        <Triggers> 
            <asp:AsyncPostBackTrigger ControlID="Button1" /> 
        </Triggers> 
    </asp:UpdatePanel> 
    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /></div> 

protected void Page_Load(object sender, EventArgs e) 
{ 
    // doesn work with: 
    Response.Write("eg.");     
} 
 
protected void Button1_Click(object sender, EventArgs e) 
{ 
    Label1.Text = "Refreshed at " + DateTime.Now.ToString(); 
} 

这很好用。当我点击 Button Label更新而不刷新页面,但如果我有 Response.Write在Page_Load事件,当我点击按钮 UpdatePanel不起作用,我需要 Page_Load .

我该如何解决我的问题?谢谢。

请您参考如下方法:

不要在 Page_Load 函数中使用 Response.Write(),因为它会破坏 UpdatePanel。

事实上,使用 Response.Write() 通常不是一个好主意,因为您无法控制写入内容在 DOM 中的最终位置。相反,如果您需要一些简单的调试输出,则使用其他方法,例如 System.Diagnostics.Debug.WriteLine()。或者,如果您需要向 DOM 添加一些内容,请添加一个控件,例如 Label 并对其进行操作。