Ajax two solutions to prevent users from submitting repeatedly

Time:2021-9-6

Methods that can be loaded on the server side:

Copy codeThe code is as follows:
private void OnLoading()
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append(” function pageLoad(){var manager = Sys.WebForms.PageRequestManager.getInstance(); “);
sb.Append(” manager.add_beginRequest(OnBeginRequest); manager.add_endRequest(OnEndRequest)} var lcPostbackElementID;”);
sb.Append(” function OnBeginRequest(sender, args){ lcPostbackElementID = args.get_ postBackElement().id.toLowerCase(); if (lcPostbackElementID === \”btnadwords\”){$get(\”btnAdWords\”).disabled = true; $ Get (\ “btnadwords \”). Value = ‘submitting…’;}} ‘);
sb.Append(” function OnEndRequest(sender, args){if (lcPostbackElementID === \”btnadwords\”){ $get(\”btnAdWords\”).disabled = false; $ Get (\ “btnadwords \”). Value = ‘OK’;}} ‘);

ScriptManager.RegisterStartupScript(Page, Page.GetType(), “btnAdWords”, sb.ToString(), true);
}

It can also be written directly in the page script:

Copy codeThe code is as follows:
<script>
function pageLoad()
{
var manager = Sys.WebForms.PageRequestManager.getInstance();
manager.add_beginRequest(OnBeginRequest);
manager.add_endRequest(OnEndRequest)
}
var lcPostbackElementID;
function OnBeginRequest(sender, args)
{
lcPostbackElementID = args.get_postBackElement().id.toLowerCase();
if (lcPostbackElementID === ‘btnadwords’)
{
$get(‘btnAdWords’).disabled = true;
$get (‘btnadwords’). Value = ‘submitting…’;
}
}
function OnEndRequest(sender, args)
{
if (lcPostbackElementID === \”btnadwords\”)
{
$get(‘btnAdWords’).disabled = false;
$get (‘btnadwords’). Value = ‘OK’;
}
}
< /script>

For more information, please refer to: http://msdn.microsoft.com/zh-cn/magazine/cc163319.aspx