Monday, July 28, 2008

ASP .NET AJAX Timer control : Processing client script on Tick event

Timer Control is a very handy ASP.NET AJAX Extension control used to 'Performs asynchronous or synchronous Web page postbacks at a defined interval'.

This control has good set of features associated with it. But still i feel there is no option to process the client scripts on the Timer's Tick event.

The below given script explains a workaround to the above issue.


// Access the Timer control.
var TestTimer = $find('<%= TestTimer.ClientID %>');
/* Override the '_doPostback' (method called to fire the server Tick event)
method with your own function. */
TestTimer._doPostback = TimerTick;

// Overridden Timer Tick Function
function TimerTick()
{
// Here this is called on every timer tick event..
// So do your client processing here..
alert('Timer Client Click');
// Finally do the async postback..
var ClientTimerId = '<%= tAutoSave.ClientID %>';
// Now call the Asyncpost back method..
// Replace the client id's with the Unique Id..
__doAsyncPostBack(TestTimer, ClientTimerId.replace(/\_/g, '$'),'');
}

// Method to do async post back..
function __doAsyncPostBack(sourceElement, eventTarget, eventArgument)
{
// Here we need to do an async post back...
// Get the Form
var form = Sys.WebForms.PageRequestManager.getInstance()._form;
// Set Event target and the event arguments..
form.__EVENTTARGET.value = eventTarget;
form.__EVENTARGUMENT.value = eventArgument;
// Set the async post back as true..
Sys.WebForms.PageRequestManager.getInstance()._postBackSettings.async = true;
// Set source element..
Sys.WebForms.PageRequestManager.getInstance()._postBackSettings.sourceElement = sourceElement;
// Call the form submit..
Sys.WebForms.PageRequestManager.getInstance()._onFormSubmit();
}

2 comments:

Unknown said...

excellent anna iragatisavu code petti form load lo RenderJSArrayWithCliendIds(controls) add chesi run kodite abbo super anna hats of mine is kalyan c# developer from vijayawada pls feel free contact me kalshant@yahoo.com and at the same time pls give a mail with ur id pls its my humble request

nancydotcom said...

Rajganesh, have you tried using this to execute client side script after the asyncpostback? My javascript will change based on the results of the postback. I've tried using ClientScriptManager with no luck.