Ramani Sandeep's Blog

DotNetting – Fast , Easy Way of Developing Applications

AsyncPostBackTrigger vs PostBackTrigger

Posted by Ramani Sandeep on April 7, 2009


In the <triggers> template in an update panel, there are the options of an AsyncPostBackTrigger or a PostBackTrigger.

By default, controls outside of an update panel will trigger a normal synchronous post back.  The AsyncPostBackTrigger “wires” up these controls to trigger an asynchronous post back.  Conversely, controls declared inside an update panel will trigger an asynchronous call by default.  The PostBackTrigger short circuits this, and forces the control to do a synchronous post back.

Utilizing a simple “time” example:

 <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server" />
        <div>
            Page Generated @
            <asp:Label runat="server" ID="uiPageTime" />
            <p />
            <asp:UpdatePanel runat="server" ID="update" UpdateMode="Conditional">
                <ContentTemplate>
                    <asp:Label runat="server" ID="uiTime" />
                    <asp:Button runat="server" ID="uiInternalButton" Text="Click" />
                </ContentTemplate>
                <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="uiAsynch" EventName="click" />
                    <asp:PostBackTrigger ControlID="uiInternalButton" />
                </Triggers>
            </asp:UpdatePanel>
            <asp:Button runat="server" ID="uiPostback" Text="Click" />
            <asp:Button runat="server" ID="uiAsynch" Text="Asynch" />
        </div>
    </form>

And the code behind file.

public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Init(object sender, EventArgs e)
        {
            uiAsynch.Click += uiAsynch_Click;
            uiPostback.Click += uiPostback_Click;
            uiInternalButton.Click += uiInternalButton_Click;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            uiPageTime.Text = DateTime.Now.ToLongTimeString();

            if (!IsPostBack)
            {
                uiTime.Text = DateTime.Now.ToLongTimeString();
            }
        }

        private void uiInternalButton_Click(object sender, EventArgs e)
        {
            uiTime.Text = "Internal @ " + DateTime.Now.ToLongTimeString();
        }

        private void uiPostback_Click(object sender, EventArgs e)
        {
            uiTime.Text = "Postback click @ " + DateTime.Now.ToLongTimeString();
            update.Update();
        }

        private void uiAsynch_Click(object sender, EventArgs e)
        {
            uiTime.Text = "Asych click @ " + DateTime.Now.ToLongTimeString();
            update.Update();
        }
    }
About these ads

18 Responses to “AsyncPostBackTrigger vs PostBackTrigger”

  1. Gnosis said

    very helpfull, thank you very much, i will use it on my site

  2. Abhijit said

    Good example …..
    It is help to better understand the AsyncPostBackTrigger and PostBackTrigger..

    Thanks…
    Abhijit

  3. Denny said

    Very very useful. Thanks a lottttttttttttt

  4. trr said

    rtytr

  5. Hena said

    Its really gud….

  6. Padmanabha said

    Its really very nice example…..

  7. sandra said

    BcnRVe

  8. bishnu said

    Good example …..

  9. Yasmeen said

    good example

  10. krishna said

    hello i want (“http://weblogs.asp.net/jstengel/archive/2008/04/25/flash-file-upload-server-control.aspx”) this type of progress bar here provide dounload opetion , i was dounload this code n run but i m not enable to run this code some Dll error eccur plz solve it n return me

  11. krishna said

    i want asp.net ajax file upload progress bar ….
    plz help me…. for that

  12. HKJHJKH said

    HJKHJK

  13. Neeraj Matta said

    Very well explained!

    Neeraj

  14. pgscannell said

    I was able to implement your code example completely. However, when I applied the same principal to a piece of my own code, I couldn’t get it to work. Is there something special you have to do if the control you are using is a dropdownlist? I have the list inside the updatepanel so no trigger should be needed as it will be work like the internal button in your wexample. I do have an event handles on the selectedindex change of the dropdownlist, but it isn’t called.

  15. sivakumar said

    It is working even though i remove

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

Join 124 other followers

%d bloggers like this: