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 !!!

posted @ Wednesday, May 16, 2007 8:15 AM

Print

Comments on this entry:

# re: ASP.NET AJAX Extensions Update Panel and Triggers

Left by Abul Kalam at 1/6/2008 2:26 PM
Gravatar
Hi,
Thanks for the this good article. But I want to know wheather it is possible to do following.

On clicking a button will update a ajax panel and do some work outside the ajax panel.

If so how? waht I have to do for that?

# re: ASP.NET AJAX Extensions Update Panel and Triggers

Left by Asim at 2/5/2008 9:13 PM
Gravatar
Hi ! there is a mistake in the code ...i copied the code above and when i click the refresh button, the value in the label 1 and label 2 both change...and the page is going for postback....does ur code has error or i did mistake..??

# re: ASP.NET AJAX Extensions Update Panel and Triggers

Left by asim at 2/5/2008 11:32 PM
Gravatar
sorry the code worked well ...actually the mistake i made is that i forgot to choose AJAXenabledwebsite while creating the website
thanks

# re: ASP.NET AJAX Extensions Update Panel and Triggers

Left by balouta at 2/13/2008 3:12 AM
Gravatar
try this link

# re: ASP.NET AJAX Extensions Update Panel and Triggers

Left by Abdul Saboor at 7/16/2008 2:18 AM
Gravatar
Thanks it is the nice example
My Question is same as Abul Kalam i want to update the label that is outside the Updatepanel
Was that possible on the click of the refresh button updatepanel update and also the label update that is outside the update panel
???

# re: ASP.NET AJAX Extensions Update Panel and Triggers

Left by jhkjhkhjk at 9/26/2008 12:01 AM
Gravatar
FSFSDFSAFSDFAFDSFSDFASDFSDFSDFSDFASFSFSDFSDF
SFSDFSDFSDFSFSF

# re: ASP.NET AJAX Extensions Update Panel and Triggers

Left by Ihsan at 10/30/2008 3:20 PM
Gravatar
I have the button inside a control and the Label is in the page, how do I go about it, thanks in advance.

# re: ASP.NET AJAX Extensions Update Panel and Triggers

Left by Muddassir Basit at 1/4/2009 5:13 PM
Gravatar
Many thanks for this article. Very short and extremely useful. Learnt a lot in few minutes. I was not sure about the concept of <Triggers> and now I believe I can deliver a lecture on it. Much appreciated. Welldone and keep it up. M Basit.

# re: ASP.NET AJAX Extensions Update Panel and Triggers

Left by Narendra Mohan at 1/9/2009 10:03 AM
Gravatar
Thanks .It is a good article .my application looking very good .

# re: ASP.NET AJAX Extensions Update Panel and Triggers

Left by Mangal Singh at 1/19/2009 11:50 AM
Gravatar
Thanks for such a simple and beneficial notes.

# re: ASP.NET AJAX Extensions Update Panel and Triggers

Left by sid at 2/19/2009 11:07 AM
Gravatar
nice article

# re: ASP.NET AJAX Extensions Update Panel and Triggers

Left by Shilpa at 2/25/2009 2:17 PM
Gravatar
I have a doubt about update panel. It is, if I have 3 dependent Dropdownlists say A,B,C. On selecting A, B gets loaded similarly C.

In normal website on selecting a value in A, page refreshes so it blinks, If update panel is used then wat will happen?

Can we avoid this sort of blinking?

# re: ASP.NET AJAX Extensions Update Panel and Triggers

Left by mayank at 4/2/2009 6:34 PM
Gravatar
i have asp.net page with apdate panel .
i have file uploder control inside update panel and one button that i use to uplode image.
because update panel file uplode control is not work then i used triger but i could't find file uplode
value
can any body help me about this problem

# re: ASP.NET AJAX Extensions Update Panel and Triggers

Left by Prachi at 5/19/2009 4:03 PM
Gravatar
Nice Article!!!!!

# re: ASP.NET AJAX Extensions Update Panel and Triggers

Left by sam at 6/21/2009 7:12 AM
Gravatar
I m using three dropdown list. When first one text changed then second one will load according to selected one, and when second one text changed then third one will load according to second one but first and second dropdown list doesn't changed. Plz help me our with solution.

# re: ASP.NET AJAX Extensions Update Panel and Triggers

Left by Dinesh at 7/10/2009 2:27 PM
Gravatar
Thanks.. It's good article!!!

# re: ASP.NET AJAX Extensions Update Panel and Triggers

Left by hasan at 9/2/2009 3:32 AM
Gravatar
awesome.simply great.

# re: ASP.NET AJAX Extensions Update Panel and Triggers

Left by Rajesh at 9/24/2009 6:54 AM
Gravatar
Article was helpful....I was bit confused with AsynPostBackTrigger...Now i got clear idea about these....

Your comment:



 (will not be displayed)


 
 
 
 
 

Live Comment Preview:

 
«November»
SunMonTueWedThuFriSat
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345