Search code examples
c#asp.net.net-3.5updatepanelcommunity-server

How to stop UpdatePanel from causing whole page postback?


I am using .NET 3.5 and building pages inside of the Community Server 2008 framework.

On one of the pages, I am trying to get an UpdatePanel working.

I took a sample straight from ASP.NET website, update a time in an UpdatePanel to current time by clicking a button, but for some reason when I try and perform the function the whole page refreshes.

Here is what I have:

protected void Button1_Click(object sender, EventArgs e)
{
    Label1.Text = "Panel refreshed at " + DateTime.Now.ToString();
    Label2.Text = "Panel refreshed at " + DateTime.Now.ToString();
}
<asp:ScriptManager ID="ScriptManager1" runat="server"/>

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

Whenever I click the button, sure the panel updates - but the whole page posts back! I can see the whole page flashing. What the heck am I doing wrong?

I am inside of a nested Masterpage, but I'm not sure if this is a problem. Could there be something in this Community Server framework that I'm using that causes all events to postback?


Solution

  • Did you try setting Button1 as an AsyncPostBackTrigger in the Triggers section? Set the ChildrenAsTriggers property to true and the UpdateMode property to Conditional.

    protected void Button1_Click(object sender, EventArgs e)
    {    
        Label1.Text = "Panel refreshed at " + DateTime.Now.ToString();    
        UpdatePanel1.Update();
    }    
    
    <asp:ScriptManager ID="ScriptManager1" runat="server"/>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server" ChildrenAsTriggers="true" UpdateMode="Conditional">
        <Triggers>        
            <asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />    
        </Triggers>    
        <ContentTemplate>        
            <fieldset>            
                <legend>UpdatePanel</legend>            
                <asp:Label ID="Label1" runat="server" Text="Panel created."></asp:Label><br />            
                <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />        
            </fieldset>    
        </ContentTemplate>
    </asp:UpdatePanel>