Infolink

 

Search This Blog

Feb 4, 2014

AsyncPostBackTrigger vs PostBackTrigger with example

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();
        }
}


No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...