Executing ClientScript Before and After an Asynchronous PostBack using ASP.NET AJAX
Posted by: Suprotim Agarwal ,
on 1/13/2009,
in
Category ASP.NET AJAX
Abstract: A user recently mailed me to find out if there is a way to determine, when an asynchronous postback begins and ends in an ASP.NET AJAX page. He wanted to fire some JavaScript code during these events. Here’s how to determine the events.
Executing ClientScript Before and After an Asynchronous PostBack using ASP.NET AJAX
A user recently mailed me to find out if there is a way to determine, when an asynchronous postback begins and ends in an ASP.NET AJAX page. He wanted to fire some JavaScript code during these events. Here’s how to determine the events.
During the AJAX client-side execution cycle of a page containing the ScriptManager control, the Application.init event is raised when the page is requested for the first time. We can use this event to wire up the client events that occur during an async postback. For this purpose, we use the Sys.WebForms.PageRequestManager class that raises client events specific to asynchronous postbacks.
Now the beginRequest and endRequest events of the Sys.WebForms.PageRequestManager class are raised before an async request and after an async response respectively. We will use these events to execute our JavaScript code as demonstrated below.
Note: I am using Visual Studio 2008 and thereby utilizing the ASP.NET AJAX plumbing that comes along with it.
Open VS 2008. Click File > New > Website. Choose ASP.NET Website from the list of installed templates, choose target platform as .NET Framework 3.5, choose the desired language and enter the location where you would like to store the website on your FileSystem. I have created a folder called VS2008 Projects, so the location over here is C:\VS2008 Projects\ ExecuteClientScriptDuringAsyncPostback. After typing the location, click OK.
Open Default.aspx. Switch to the Design mode of Default.aspx. Open the toolbox (Ctrl+Alt+X). Now add a <ScriptManager> control to the page and then an <UpdatePanel>. Inside the <UpdatePanel>, add a label and a button control as shown below
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:Button ID="Button1" runat="server" Text="Button"/>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
Just below the </body> tag, add the following script
<body>
...
</body>
<script type="text/javascript">
function beforeAsyncPostBack() {
var curtime = new Date();
alert('Time before PostBack: ' + curtime);
}
function afterAsyncPostBack() {
var curtime = new Date();
document.getElementById('Label1').innerHTML = 'Time after PostBack: ' + curtime;
}
Sys.Application.add_init(appl_init);
function appl_init() {
var pgRegMgr = Sys.WebForms.PageRequestManager.getInstance();
pgRegMgr.add_beginRequest(BeginHandler);
pgRegMgr.add_endRequest(EndHandler);
}
function BeginHandler() {
beforeAsyncPostBack();
}
function EndHandler() {
afterAsyncPostBack();
}
</script>
</html>
As described above, we use the Application.add_init() to add the beginRequest and endRequest event handlers. To add or remove handlers for events raised by the PageRequestManager classes, use the add_eventname and remove_eventname methods of those classes.
Inside these handlers, we call our JavaScript functions beforeAsyncPostBack and afterAsyncPostBack respectively.
That’s it. Run the page. When you click on the button, just before an Asynchronous request, the beforeAsyncPostBack() gets executed.
Similarly, after an Asynchronous response, the afterAsycPostBack() gets executed.
I hope you liked the article and I thank you for viewing it.
This article has been editorially reviewed by Suprotim Agarwal.
C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.
We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).
Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.
Click here to Explore the Table of Contents or Download Sample Chapters!
Was this article worth reading? Share it with fellow developers too. Thanks!
Suprotim Agarwal, MCSD, MCAD, MCDBA, MCSE, is the founder of
DotNetCurry,
DNC Magazine for Developers,
SQLServerCurry and
DevCurry. He has also authored a couple of books
51 Recipes using jQuery with ASP.NET Controls and
The Absolutely Awesome jQuery CookBook.
Suprotim has received the prestigious Microsoft MVP award for ten consecutive times. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that offers Digital Marketing and Branding services to businesses, both in a start-up and enterprise environment.
Get in touch with him on Twitter @suprotimagarwal or at LinkedIn