防止在ASP中多次点击提交按钮.. NET没有jQuery
本文关键字:提交 按钮 NET jQuery 没有 ASP | 更新日期: 2023-09-27 17:54:20
当用户多次单击链接按钮时,我试图防止多次提交,但不想使用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中工作得很好&铬。但是,它只是禁用了IE中的按钮,根本没有提交。
要让提交在IE中工作,我必须添加
currentPage.ClientScript.GetPostBackEventReference(new PostBackOptions(button))
到javascript中,这使得它在IE中按预期工作,但现在在Firefox中每次点击都提交。Chrome .
谁能建议一个修复或替代方案?
我会尝试使用布尔标志,它应该像这样工作:
private bool _clicked;
protected void Page_Load(object sender, EventArgs e) {
_clicked = false;
}
protected void Button_Click(object sender, EventArgs e) {
if(!_clicked){
_clicked = true;
}