Some Insights About the Update Panel

I was using the UpdatePanel and was noticing some strange behavior. After some debugging I found some cool stuff about the UpdatePanel. The first thing that you need to know is that the UpdatePanel is rendered as a DIV element. I guess that was pretty obvious. The next thing is if you put a Button inside the UpdatePanel then the Button will refresh all the UpdatePanels. Check out the code below:

  <atlas:UpdatePanel Mode="Always" RenderMode="Block" ID="up1" runat="server"> 
        <ContentTemplate>
         
        <asp:Button ID="Button1" runat="server" Text="Press Me!" OnClick="Button1_Click" />
        </ContentTemplate>
        
        </atlas:UpdatePanel>
        

And here are some more UpdatePanels:

<asp:Label ID="lblError" runat="server"></asp:Label>

  <atlas:UpdatePanel ID="up3" runat="server">
    <ContentTemplate>
    <asp:Label ID="VisibleLabelText" runat="server"></asp:Label>
   </ContentTemplate>
   </atlas:UpdatePanel>
   
    <atlas:UpdatePanel Mode="always" ID="up2" runat="server">
    <ContentTemplate>
    <asp:Label ID="Label1" runat="server"></asp:Label>
    <asp:Label ID="Label2" runat="server"></asp:Label>
    <asp:Label ID="Label3" runat="server"></asp:Label>
     
    </ContentTemplate>
    </atlas:UpdatePanel>

And here is the Button click code:

 protected void Button1_Click(object sender, EventArgs e)
    {
        Label1.Text = "Button1 is clicked";
        Label2.Text = "Button1 is clicked";
        Label3.Text = "Button3 is clicked";

        lblError.Text = "This will not update";

        VisibleLabelText.Text = "This is Visible also";
        
    }

Now, since every Label control is placed inside the UpdatePanel this will work fine. But try putting a Label outside the UpdatePanel and change its text inside the Button click event. It will not work, the reason is that since only the UpdatePanels are refreshed and not the whole page.

If you don't put the Button inside the UpdatePanel then it will fire a server postback.

Print | posted @ Monday, April 17, 2006 8:09 PM

Twitter