Javascript - Event Handling

This code will help us to write any code before and after any event. BeforeAjaxRequest method will be executed before any event and pageLoadedHandler method will be executed at the end of any event.

Sample Syntax:
var pageMgr = Sys.WebForms.PageRequestManager.getInstance();
pageMgr.add_beginRequest(BeforeAjaxRequest);
pageMgr.add_pageLoaded(pageLoadedHandler);
var postbackElement;

function BeforeAjaxRequest(sender, args)
{
postbackElement=args.get_postBackElement();
if (postbackElement.id.indexOf("{ID of the Control}") != "-1")
{
/* Write your Code Here */}
}

function pageLoadedHandler(sender, args)
{
if (typeof(postbackElement) == "undefined") { return;}
else if (postbackElement.id.indexOf("
{ID of the Control}") != "-1")
{
/* Write your Code Here */}
}


For Example: You are having a asp link button with id lnkTest now according to this your above code should be:
function BeforeAjaxRequest(sender, args)
{
postbackElement=args.get_postBackElement();
if (postbackElement.id.indexOf("
lnkTest") != "-1")
{
alert("Before Ajax Request");}
}

function pageLoadedHandler(sender, args)
{
if (typeof(postbackElement) == "undefined") { return;}
else if (postbackElement.id.indexOf("
lnkTest") != "-1")
{
alert("After Ajax the Request");}
}

0 comments: