Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 600
user53885 Avatar asked Apr 07 '09 23:04

user53885


People also ask

What is postback trigger in UpdatePanel?

AsyncPostBackTrigger - use these triggers to specify a control within or outside of the UpdatePanel that, when clicked, should trigger a partial page postback. PostBackTrigger - use these triggers to have a control within the UpdatePanel cause a full page postback rather than a partial page postback.

What happens when a button placed in the UpdatePanel control is clicked?

The UpdatePanel control contains a Button control that refreshes the content inside the panel when you click it.

What is UpdateMode conditional in UpdatePanel?

If the UpdateMode property is set to Always, the UpdatePanel control's content is updated on every postback that originates from anywhere on the page. This includes asynchronous postbacks from controls inside other UpdatePanel controls and postbacks from controls that are not inside UpdatePanel controls.

What is trigger in UpdatePanel?

Triggers for a given UpdatePanel, by default, automatically include any child controls that invoke a postback, including (for example) TextBox controls that have their AutoPostBack property set to true.


2 Answers

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>
like image 115
Michael Kniskern Avatar answered Oct 14 '22 15:10

Michael Kniskern


I'm not seeing Label2 in your code sample above. If Label2 is located outside the UpdatePanel, a full page refresh will occur because that is what is required for the page to properly update Label2.

By default, UpdatePanels will only dynamically refresh the content within them, when triggered by controls within them. If you need to do some fancier updates, say a button outside of the panel causing the refresh or a label in a different panel to be updated, then you need to set the Conditional attribute on your UpdatePanel(s) and make some manual Update calls in your code.

like image 22
Dillie-O Avatar answered Oct 14 '22 16:10

Dillie-O