简体   繁体   English

ASP.NET UpdatePanel不能与Page_Load中的Response.Write一起使用

[英]ASP.NET UpdatePanel dosn't work with Response.Write in Page_Load

I have that example: 我有那个例子:

<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();
}

This works fine. 这很好。 When I click in the Button the Label updates without refreshing the page, but if I have Response.Write in the Page_Load event, when I click the button the UpdatePanel doesn't work, and I need the Page_Load . 当我单击ButtonLabel更新而不刷新页面,但是如果我在Page_Load事件中具有Response.Write ,则当我单击按钮时, UpdatePanel不起作用,并且需要Page_Load

How I can solve my problem? 我该如何解决我的问题? Thanks. 谢谢。

Don't use Response.Write() in your Page_Load function, as it will break the UpdatePanel . 不要在Page_Load函数中使用Response.Write() ,因为它将破坏UpdatePanel

In fact, using Response.Write() is often a bad idea, because you don't control where in the DOM the written content will end up. 实际上,使用Response.Write()通常不是一个好主意,因为您无法控制DOM写入内容的最终位置。 Instead, if you need some simple debug output, then use other means such as System.Diagnostics.Debug.WriteLine() . 相反,如果您需要一些简单的调试输出,请使用其他方法,例如System.Diagnostics.Debug.WriteLine() Or if you need to add something to the DOM, add a control such as Label and manipulate that. 或者,如果您需要向DOM中添加内容,则添加诸如Label的控件并对其进行操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM