2013-01-17 88 views
1

我試圖阻止多次提交,當用戶多次點擊鏈接按鈕,但不想使用jQuery。理想情況下,我希望在代碼背後做到這一點。防止在沒有jQuery的ASP.NET中多次提交按鈕點擊

我目前的解決方案是基於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(&quot;ctl00$cntMain$btnSubmit&quot;, &quot;&quot;, true, &quot;&quot;, &quot;&quot;, false, true))">Add Note</a> 

這很好地工作在Firefox &鉻。然而它只在IE中禁用了按鈕,並沒有提交。

爲了讓提交工作在IE我不得不添加

currentPage.ClientScript.GetPostBackEventReference(new PostBackOptions(button)) 

進入的JavaScript,這使得根據需要在IE瀏覽它的工作,但現在提交關於在Firefox & Chrome的每一次點擊。

任何人都可以提出修復或替代?

+0

爲什麼你做服務器端?你可以在簡單的javascript和沒有jQuery中做同樣的事情。 – LoSTxMiND

+0

我想將其添加到可供多個Web應用程序使用的庫 – Yetiish

回答

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; 
} 
相關問題