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();
}
}
Gnosis said
very helpfull, thank you very much, i will use it on my site
Abhijit said
Good example …..
It is help to better understand the AsyncPostBackTrigger and PostBackTrigger..
Thanks…
Abhijit
Denny said
Very very useful. Thanks a lottttttttttttt
trr said
rtytr
Ramani Sandeep said
what is this rtytr means??
Hena said
Its really gud….
Padmanabha said
Its really very nice example…..
sandra said
BcnRVe
bishnu said
Good example …..
Yasmeen said
good example
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
krishna said
i want asp.net ajax file upload progress bar ….
plz help me…. for that
Ramani Sandeep said
What error you are facing..??
HKJHJKH said
HJKHJK
Neeraj Matta said
Very well explained!
Neeraj
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.
sivakumar said
It is working even though i remove
Ramani Sandeep said
Than good for you to post the same thing on MSDN so that other people get to know about this.