当用户多次单击链接按钮但我不想使用jQuery时,我试图阻止多次提交。理想情况下,我想在后面的代码中执行此操作。
我目前的解决方案基于This Code Project Article
目前我有这个:
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&amp;铬。但是它只在IE中禁用了按钮,并且根本没有提交。
要让提交在IE中工作,我必须添加
currentPage.ClientScript.GetPostBackEventReference(new PostBackOptions(button))
进入javascript,使其在IE中按需运行,但现在在Firefox和&amp ;;铬。
有人可以建议修复或替代吗?
答案 0 :(得分:0)
我尝试使用布尔标志,它应该像这样工作:
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;
}