Search This Blog

Sunday, April 3, 2011

Click event stopped firing after ajax postback

I had a user control on a SharePoint master page for one our client portal which was taking longer to load than other modules on the pages. 

Deciding to load this control asynchronously to speed up the page load I achieved this by adding button control inside the Ajax update panel. This button remains hidden from users view. I made the button click event to fire using JavaScript function by pushing it to body onload event using SharePoint's _spBodyOnLoadFunctionNames function.

The control worked as expected, causing the button click event to fire and processing the server side logic. However, the other buttons, link buttons and other server on other modules lost their events. Now nothing happens whenever I click on any server side button controls.

We tried various different options like calling the JavaScript function right into the script tag, mentioning it on the master page’s body onload event etc,. The final one which worked was ScriptManager's registerstartupscript function i.e. ScriptManager.RegisterStartupScript on the server side page load event. I registered the JavaScript function call using the RegisterStartupScript function provided by ScriptManager object. Now the page worked perfectly without any issue. Buttons, hyperlink buttons all worked smoothly.

I am still not sure what caused events to break by calling the function right into the page load. Your suggestions and advice are welcome.

2 comments:

Diya said...

Hi Rizwan,
Thanks for your Post , but can you elaborate more on technical side
my issue is I have created sharepoint Application page on MOSS 2007with ajax functionality, making all the required changed to web.config

Required functionality works fine as expected, without postback, on button click.

However, from second time onwards, it doesn't work.

i added following script on my page
POSTBACK_SCRIPT =
@"_spBodyOnLoadFunctionNames.push(""_initFormActionAjax"");
function _initFormActionAjax()
{
if (_spEscapedFormAction == document.forms[0].action)
{
document.forms[0]._initialAction = document.forms[0].action;
}
}
var RestoreToOriginalFormActionCore = RestoreToOriginalFormAction;
RestoreToOriginalFormAction = function()
{
if (_spOriginalFormAction != null)
{
RestoreToOriginalFormActionCore();
document.forms[0]._initialAction = document.forms[0].action;
}
}";
ScriptManager.RegisterStartupScript(this,
typeof(RotomotiveCREForm), "UpdatePanelFixup",
POSTBACK_SCRIPT, true);
but it is not helping me can you please guide me i already spent 1 day it's urgent
Thanks in Advance
Divya

Diya said...

Hi Rizwan, Please help me to resolve the similar issue you solved
have created sharepoint webpart on MOSS 2007with ajax functionality, making all the required changed to web.config
Required functionality works fine as expected, without postback, on button click.
However, from second time onwards, it doesn't work.
please have a look on the script added on the page
script = @"
var ITEMLISTINGBUTTONID;
with(Sys.WebForms.PageRequestManager.getInstance()){
add_beginRequest(onBeginRequest);
add_endRequest(onEndRequest);
}
function onBeginRequest(sender, args){
ITEMLISTINGBUTTONID = args.get_postBackElement().id;
$get(ITEMLISTINGBUTTONID).parentElement.style.display = 'none';
}
function onEndRequest(sender, args){
$get(ITEMLISTINGBUTTONID).parentElement.style.display = '';
}
";

Thanks in Advance