Development – Leet 4 Life

Software Development Code Samples and More

Skip to: Content | Sidebar | Footer

Firing JavaScript only when inside an ASP.NET Ajax Postback

29 June, 2012 | ASP.NET, C#. NET, jQuery | By: Michael Merrell

There are times where you need to fire a JavaScript event only when you are posting inside of an ASP.NET AJAX post back event. In my example below I am needing to fire a function used for a document ready jQuery event. This event only needs to be fired on AJAX post backs because on normal posts back it will already be handled by jQuery, however it will not be fired when executed inside of an AJAX post back unless told to.

The following JavaScript is a pretty standard script for firing a set of functionality when a page has loaded.

$(document).ready(document_Ready);

function document_Ready() { 
   //Page Load Code Here
}

You will need to have the following code inside of your page load or post back event to register that the event should fire when you are posting back due to an AJAX post back.

 
if (this.Page.IsPostBack && ScriptManager.GetCurrent(this.Page).IsInAsyncPostBack)
                ScriptManager.RegisterStartupScript(this, this.GetType(), "document_Ready", "document_Ready();", true);

You can view more information and my situation around this event by viewing my Firing jQuery Document.Ready inside an ASP.NET Ajax Postback post.

Comments

Comment from Marilu
Time October 21, 2012 at 1:25 pm

That’s a knowing answer to a difficult question

Write a comment