简体   繁体   中英

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 .

How I can solve my problem? Thanks.

Don't use Response.Write() in your Page_Load function, as it will break the 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. Instead, if you need some simple debug output, then use other means such as System.Diagnostics.Debug.WriteLine() . Or if you need to add something to the DOM, add a control such as Label and manipulate that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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