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.

Upvotes: 1

Views: 1806

Answers (1)

mason
mason

Reputation: 32704

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.

Upvotes: 4

Related Questions