LA.NET [EN]

Oct 30

Like the previous CTPs, the current beta version introduces 2 “usable” triggers: AsyncPostBackTrigger and PostBackTrigger. I call them “usable” because they”re the leaves of the hierarchical tree defined by the AJAX extensions on the server side. As you can infer by their names, the AsyncPostBackTrigger is responsible for starting an async postback while the PostBackTrigger manages to get a full postback. In current releases, the framework only gave us async postback triggers. So let”s see what we can do with the new ones…let”s start with the AsyncPostbackTrigger.

You normally use it when you want to configure a control placed inside outside ([Updated: thanks Garbin!]) an UpdatePanel so that a postback started by the control results in partial postback (instead of the usual full postback). Do keep in mind that if you have more than one UpdatePanel on  a page, the number of panels that are refreshed during the partial postback depend on the UpdateMode of each panel (if it”s set to always, then it”ll be refreshed, even though the trigger may not be associated with that panel). Opposite to one might think, there is at least one scenario where you might want to associate an AsynPostBackTrigger to a control placed inside an UpdatePanel: when you set the UpdatePanel”s ChildrenAsTrigger property to false. When this happens, all postbacks started by controls placed inside UpdatePanels do result in full postbacks by default! So, in these situations, you might need to add an AsyncPostBackTrigger to get an async postback when a postback is started by a specific control.

When you use an AsyncPostBackTrigger, you must set two properties: ControlID and EventName. Both these properties work together and influence the behavior of the UpdatePanel during a partial postback (ie,if it is refreshed or not). To understand why,you must know that the PageRequestManager (which is responsible for intercepting postbacks and deciding if you”ll get a partial or a full postback) doesn”t have the concept of trigger. It only knows if a specific control can (or cannot) start an async postback (it maintains an internal array with the IDs of the controls that can do that. When it intercepts a postback, it checks against that list to see if the responsible for the operation is on it: if it is, you”ll get a partial postback – keep in mind that this is just one of several steps envolved in checking if the current postback should be partial or full).

So, when the server side is handling a partial update, ScriptManager ends up calling an auxiliary method (in the PageRequestManager auxiliary class) which is responsible for going through all the UpdatePanels and checking the ones that need to be updated. As you might already have guessed, one of the steps performed during this verification is seeing if any of the associated triggers has been “triggered” (which only happens when the control”s event associated to the trigger has been fired). Note that this is only important if the UpdateMode of the UpdatePanel is set to the Conditional (if it”s set to Always, the UpdatePanel will always be refreshed).

Declaring an AsyncPostBackTrigger is as simple as writing this:

<asp:UpdatePanel runat=”server” ID=”painelEsquerdo” UpdateMode=”Conditional”>
  <ContentTemplate>
       <span>DateTime last partial postback:</span><%= DateTime.Now.ToString() %>
  </ContentTemplate>
   <Triggers>
           <asp:AsyncPostBackTrigger ControlID=”myButton” EventName=”Click” />
    </Triggers>
</asp:UpdatePanel>
<asp:Button runat=”server” ID=”myButton” Text=”Refrescar painel” OnClick=”HandlePartialPosback” />

It”s time for the PostBackTriggers. These triggers only have one interesting property: ControlID. As you may now, you can use these triggers to identify a control placed inside an UpdatePanel which is responsible for starting a full postback. This is important when you have <input type=”file”> controls on your page since a full postback is the only way you can get that info back on the server (there are some workarounds, which envolve creating iframes, but will leave that for another day). Besides keeping an internal list on which controls can start a partial postback, the PageRequestManager also keeps a list of those controls that can start a full postback. When you use this trigger, you”re adding an item to that list. The usage of the trigger is really simple and looks a lot like the previous one (and due to that we won”t waste any time by putting it here). and that”s it for today…

3 comments so far

  1. Rahul Bhansali
    1:30 am - 2-3-2007

    Hi !!

    I don”t believe this ! 10 mins back I had been sitting for about 2 hours trying to understand about what is the Always, Conditional, PostBack Trigger etc and had reached my own conclusions of how certain things are just not possible in AJAX, cursing Microsoft.

    Your blog has cleared it up all. I am amazed at how visiting ths single page when I began my search, I have got my understanding just ”right”. Your analysis,, findings and expalnation is simply ”clear-cut”. Thanks a lot. Will look forward to seeing more ”gyaan” from u. Do let me know, if possible, when u write ur next blog. You already have a student.

    Keep the good work going.

  2. meera
    7:11 am - 7-2-2008

    Is it possible to make a file upload control inside an update panel with an associated updateprogress control work with AsynPostbackTrigger ??

  3. luisabreu
    8:04 am - 7-2-2008

    Hello.

    Well, not likely. What you can do is search my blog for some old code that mimics gmail upload feature. The code was written for an older version of ASP.NET AJAX (I believe it was at CTP at the time I”ve written), so you”ll need to port it to the current release.