How to avoid double click on the button inside UpdatePanel


This explains how to avoid double click on the button inside Update panel. If we press the button on the UpdatePanel twice, it makes the call to the server twice which is not needed and also it degrades the performance.

Consider that we have a button on the UpdatePanel which is an AJAX Control. All the AJAX control makes a call to the server to process the java script coded for that control. If we press the button on the UpdatePanel twice, it makes the call to the server twice which is not needed and also it degrades the performance.

The following script disables the post back operation for the button after pressing once but just before it makes the call to the server.

Put the following script on the page after the ScriptManager.



<script type="text/javascript">
var postbackControl = null;
var parm = Sys.WebForms.PageRequestManager.getInstance();
parm.add_beginRequest(BeginRequestHandler);
parm.add_endRequest(EndRequestHandler);

function BeginRequestHandler(sender, args)
{
postbackControl = args.get_postBackElement();
postbackControl.disabled = true;
}
function EndRequestHandler(sender, args)
{
postbackControl.disabled = false;
postbackControl = null;
}
</script>



Once the post back call is made to the server by pressing button once, then the post back control is disabled using the above code to avoid double click on the button inside UpdatePanel.


Comments

Author: keyur soni14 Mar 2012 Member Level: Silver   Points : 0

Hi,
This is very good way to avoit post back.
thanks for share.....

Author: sandy16 May 2012 Member Level: Gold   Points : 0

HI ,
Thanks for sharing the code

Guest Author: spike01 Nov 2012

This works well to disable the button but I still get a Javascript error when you double click, even though the postback isn't made twice.

It complains about a null object in dynamic javascript.

Any thoughts?



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: