ASP.NET AJAX Extensions Update Panel and Triggers

So far we have been using Update Panels with controls inside the Update Panel which initiate the partial page refresh.

What if we want to refresh an Update Panel based on an external event like a Button which is not necessarily a part of the UpdatePanel's content template?  Wait ! Dont jump into ugly ways of refreshing the whole page using F5 !

Triggers is your friend in this case.  A trigger can be defined within the Update Panel to specify which control needs to trigger a partial refresh for that section.

For that matter, the only 2 tags you can directly place within an UpdatePanel are ContentTemplate and Triggers.  If you directly try placing HTML or Web Controls within the UpdatePanel, it will start throwing compilation errors.

Ok, let us jump into code to understand more.  As usual copy paste the following HTML Source and Code behind to your page.

<form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
    <div>
        Label outside the Update Panel Refreshed at:
        <asp:Label ID="Label2" runat="Server"></asp:Label>
        <br />
        <br />
        <asp:Button ID="Button1" runat="Server" Text="Refresh" OnClick="Button1_Click" />
        <br />
        <br />
        <asp:UpdatePanel ID="UpdatePanel1" runat="Server" UpdateMode="conditional">
        <ContentTemplate>
            Label within the Update Panel Refreshed at:
            <asp:Label ID="Label1" runat="server"></asp:Label>
       
        </ContentTemplate>
        <Triggers>
            <%--<asp:PostBackTrigger ControlID="Button1" />--%>
            <asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />
        </Triggers>
      
        </asp:UpdatePanel>
       
   
    </div>
    </form>

Code behind

 protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = System.DateTime.Now.ToString();
        Label2.Text = System.DateTime.Now.ToString();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
       
    }

Now, when you run this page, you will notice that when you click on the "Refresh" button you will see that the label below the Refresh button gets updated whereas the label above remains with old value.

Couple of interesting things to notice:-

The Button control is defined outside the UpdatePanel unlike our previous samples.

The Triggers contains something called as AsyncPostBackTrigger for which the ControlID is specified as Button1 and EventID is specified as Click.  For now, forget the commented PostBackTrigger definition.

The AsyncPostBackTrigger initiates an async call back and the button associated doesnt do a refresh or postback explicitly to execute the code.  In fact, the code you place within the Button1_Click event will throw error, if you want to do something like Response.Write.

Though the Button is a normal asp:Button, it doesnt invoke a postback but refreshes the contents of the Update Panel.

Now, if you comment the AsyncPostBackTrigger line and uncomment the PostBackTrigger line, and run the page, you will notice that the whole page refreshes upon clicking on the "Refresh" Button.  This works like a normal button although it does update the contents of the UpdatePanel.

In normal cases you would use AsyncPostBackTrigger since it implies the partial page refresh mechanism.

Cheers !!!

ASP.NET 2.0 AJAX Extensions Update Panel - Nested Update Panel

If you have been following my articles, you would have realised how easy it is to implement partial page refreshing in ASP.NET AJAX where we explored a single UpdatePanel and multiple Update Panel examples.

Let us now consider a more complex scenario where we want to nest Update Panels.

Page

       UpdatePanel 1 (Parent)

            UpdatePanel 2 (Child)

The thumb rule with nested update panels is as follows:-

Parent Update Panel refreshes all the contents including Child Update Panel's contents even if the Child Update Panel's update mode is set to Conditional

Child Update Panel refreshes only its contents and doesnt refresh that of the Parent Update Panel unless, the update mode for the parent update panel is not set to Conditional

Kind of confusing eh?  Let us see it in action.  Copy paste the following HTML and the subsequent source code to see it in action.

 <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <div>
            <table width="80%" border="3">
                <tr>
                    <td>
                        Label outside all the Update Panels
                        <asp:Label ID="Label3" runat="Server" Font-Bold="true"></asp:Label>
                        <br />
                        <br />
                        <asp:Button ID="Button3" runat="Server" Text="Refresh" />
                        <br />
                        <br />
                    </td>
                </tr>
                <tr>
                    <td>
                        <table width="65%" border="2">
                            <tr>
                                <td>
                                    <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
                                        <ContentTemplate>
                                            Label within the Parent Update Panel
                                            <asp:Label ID="Label1" runat="server" Font-Bold="true"></asp:Label>
                                            <br />
                                            <br />
                                            <asp:Button ID="Button1" runat="server" Text="Refresh" />
                                            <table width="40%" border="1">
                                                        <tr>
                                                        <td>
                                                       
                                                       
                                                        <asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
                                                <ContentTemplate>
                                                    <br />
                                                    <br />
                                                   
                                                    Label within the Child Update Panel
                                                    <asp:Label ID="Label2" runat="server" Font-Bold="True"></asp:Label>
                                                    <br />
                                                    <br />
                                                    <asp:Button ID="Button2" runat="server" Text="Refresh" />
                                                </ContentTemplate>
                                            </asp:UpdatePanel>
                                                        </td>
                                                       
                                                        </tr>
                                                   
                                                    </table>
                                           
                                        </ContentTemplate>
                                    </asp:UpdatePanel>
                                </td>
                            </tr>
                        </table>
                    </td>
                </tr>
            </table>
        </div>
    </form>

Code behind

  protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = System.DateTime.Now.ToString();
        Label2.Text = System.DateTime.Now.ToString();
        Label3.Text = System.DateTime.Now.ToString();
    }

When you run the above page, you will notice that I have put nice looking (??) tables to clearly indicate the label outside the Update Panels, the parent update panel contents and the child update panel contents.

When you click on the top most button, it refreshes the whole page and subsequently, all the labels get refreshed with the new date time.

However, when you click on the second refresh button which is marked inside the Parent Update Panel you will notice that the top most label doesnt get refreshed but both the Label 2 and Label 3 gets refreshed, although I have marked UpdateMode as conditional for the Child UpdatePanel.  The reason being, the parent updates all the child update panels nested within.

Finally, when you click on the last Refresh button which is marked inside the Child Update Panel, you will notice that only the respective label gets refreshed.

Note that, if I remove the property UpdateMode=Conditional for the UpdatePanel1 (parent), both the labels will get refreshed.

Enough of Update Panels eh?

Cheers !!!

 

 

ASP.NET 2.0 AJAX Extensions Update Panel - Multiple Update Panels

We saw about Update Panel and how it works in its simplest form.  Let us explore more complex scenarios where we have multiple Update Panels, Nested Update Panels, etc.,

One of the main properties of an UpdatePanel is its UpdateMode property.  The UpdateMode property is by default set to "Always".

However, when you have more than a single update panel or when you want to control as to when the Update Panel needs to get refresh, then setting the UpdateMode property to "Condition" is the first step.

Let us consider a scenario where we have 4 Update Panels in a page.  For simplicity I am sticking to the Label - Display Current Date functionality.  The HTML for the same is as below:-

 <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
   
    <div>
        <table width="100%">
            <tr>
            <td style="height: 64px">
            <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
            <ContentTemplate>
            <b><u>Section 1</u></b>
            <br />
            <br />
            <asp:Label ID="Label1" runat="server"></asp:Label>
            <br />
            <br />
            <asp:Button ID="Button1" runat="Server" Text="Refresh" OnClick="Button1_Click" />
           
           
            </ContentTemplate>
           
            </asp:UpdatePanel>
           
            </td>
            <td style="height: 64px">
            <asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
            <ContentTemplate>
            <b><u>Section 2</u></b>
            <br />
            <br />
                        <asp:Label ID="Label2" runat="server"></asp:Label>
            <br />
            <br />
            <asp:Button ID="Button2" runat="Server" Text="Refresh" OnClick="Button2_Click" />
           
           
            </ContentTemplate>
           
            </asp:UpdatePanel>
           
            </td>
            <td style="height: 64px">
            <asp:UpdatePanel ID="UpdatePanel3" runat="server" UpdateMode="Conditional">
            <ContentTemplate>
            <b><u>Section 3</u></b>
            <br />
            <br />
                        <asp:Label ID="Label3" runat="server"></asp:Label>
            <br />
            <br />
            <asp:Button ID="Button3" runat="Server" Text="Refresh" OnClick="Button3_Click" />
           
           
            </ContentTemplate>
           
            </asp:UpdatePanel>
           
            </td>
            <td style="height: 64px">
            <asp:UpdatePanel ID="UpdatePanel4" runat="server" UpdateMode="Conditional">
            <ContentTemplate>
                <b><u>Section 4</u></b>
            <br />
            <br />
                        <asp:Label ID="Label4" runat="server"></asp:Label>
            <br />
            <br />
            <asp:Button ID="Button4" runat="Server" Text="Refresh" OnClick="Button4_Click" />
           
           
            </ContentTemplate>
           
            </asp:UpdatePanel>
            </td>
            </tr>
       
       
        </table>
   
    </div>
    </form>

 Note that for all the UpdatePanels, I have set the UpdateMode to conditional.  The application logic or codebehind for the above is as follows:-

 protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = System.DateTime.Now.ToString();
        Label2.Text = System.DateTime.Now.ToString();
        Label3.Text = System.DateTime.Now.ToString();
        Label4.Text = System.DateTime.Now.ToString();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {

    }
    protected void Button2_Click(object sender, EventArgs e)
    {

    }
    protected void Button3_Click(object sender, EventArgs e)
    {

    }
    protected void Button4_Click(object sender, EventArgs e)
    {

    }

When you run this page, you will notice that there are four Sections - Section 1, 2,3 & 4.  When you click on the Refresh button, the date time value gets refreshed only for the particular section.  The other values doesnt get changed.

If you remove the UpdateMode=Conditional from the above UpdatePanel declarations, you will notice that all the labels get refreshed.

Hence, setting the UpdateMode as Conditional is important if you like to control the way the UpdatePanels get refreshed.

Next, we will explore the Nested UpdatePanel example.

Cheers !!!

TechMela - Whats this new name?

 

 

Tech Mela?

What is special about Tech Mela?

Visit http://www.techmela.com

Keep watching this space for my own information nuggets.

Cheers !!!

«May»
SunMonTueWedThuFriSat
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789