当用户多次单击链接按钮但不想使用 jQuery 时,我试图防止多次提交。理想情况下,我想在后面的代码中做到这一点。
我当前的解决方案基于此代码项目文章
目前我有这个:
private const string _disableButtonScript = "return disableButton(this.id);";
/// <summary>
/// Prevents a button being clicked multiple times on the client
/// </summary>
/// <param name="currentPage">The page the button resides on</param>
/// <param name="button">The button to prevent the multiple clicks on</param>
public void PreventMultipleClientClickPostbacks(Page currentPage, LinkButton button)
{
if (currentPage.IsPostBack || currentPage.IsCallback)
{
// Already added script
return;
}
AddDisableScriptToPageHeader(currentPage, button);
string currentOnClick = string.Empty;
// If onclick already exist we need to append to it
if (button.Attributes["onclick"] != null)
{
currentOnClick = button.Attributes["onclick"];
// Remove current onclick
button.Attributes.Remove("onclick");
if (!currentOnClick.EndsWith(";"))
{
// add semi colon suffix
currentOnClick = currentOnClick + ";";
}
}
// add client onclick handler
button.Attributes.Add("onclick", string.Format("{0}{1}", currentOnClick, _disableButtonScript));
}
/// <summary>
/// Adds the javascript disable function to the page header
/// </summary>
/// <param name="currentPage">The page to add the header script to</param>
/// <param name="button">The button to add the script to</param>
private static void AddDisableScriptToPageHeader(Page currentPage, Control button)
{
// Generate disable script
StringBuilder scriptLinkHtml = new StringBuilder();
scriptLinkHtml.AppendLine("<script type=\"text/javascript\">");
scriptLinkHtml.AppendLine("//<![CDATA[");
scriptLinkHtml.AppendLine("var callCount = 0;");
scriptLinkHtml.AppendLine("function disableButton(btnId) {");
scriptLinkHtml.AppendLine("var ret = true;");
scriptLinkHtml.AppendLine("var ele = document.getElementById(btnId);");
scriptLinkHtml.AppendLine("if (ele != null && !ele.disabled)");
scriptLinkHtml.AppendLine("ret = true;");
scriptLinkHtml.AppendLine("else");
scriptLinkHtml.AppendLine("ret = false;");
scriptLinkHtml.AppendLine("if (ele != null)");
scriptLinkHtml.AppendLine("ele.disabled = true;");
scriptLinkHtml.AppendLine("return ret;");
scriptLinkHtml.AppendLine("}");
scriptLinkHtml.AppendLine("//]]>");
scriptLinkHtml.AppendLine("</script>");
// Add script link to page header
LiteralControl scriptLink = new LiteralControl(scriptLinkHtml.ToString());
scriptLink.ID = "disableButtonScript";
currentPage.Header.Controls.Add(scriptLink);
}
这在客户端上呈现如下:
<script type="text/javascript">
function disableButton(btnId) {
var ret = true;
var ele = document.getElementById(btnId);
if (ele != null && !ele.disabled)
ret = true;
else
ret = false;
if (ele != null)
ele.disabled = true;
return ret;
}
</script>
<a onclick="javascript:return disableButton(this.id);" id="cntMain_btnSubmit" class="but-yellow frcr" href="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$cntMain$btnSubmit", "", true, "", "", false, true))">Add Note</a>
这在 FireFox 和 Chrome 中运行良好。但是它只禁用了 IE 中的按钮,根本没有提交。
为了让提交在 IE 中工作,我必须添加
currentPage.ClientScript.GetPostBackEventReference(new PostBackOptions(button))
进入 javascript,使其在 IE 中按需要工作,但现在在 Firefox 和 Chrome 中每次点击时都会提交。
任何人都可以提出解决方案或替代方案吗?