ASP.Net按鈕控制項防止重覆送出的方式

防止使用者將表單重覆送出,目的在保持資料的完整性與一致性,同時也可保護伺服器不被Web Bot類程式的侵擾,解決方案依架構來區分可分為Server端及Client等,Server端的解決方案最簡單的是用Session來記錄每條連線的Submit狀態,讓重覆送出的PostBack不會對程式碼產生重覆執行的效果,方法較為可靠,但也較耗伺服器資源,本文針對使用Client端的客製化屬性將表單按鈕做特別的處理,確保表單可以不受干擾的執行。

public class WebForm1 : System.Web.UI.Page
{
    protected System.Web.UI.WebControls.Button Button1;
    
    private void Page_Load(object sender, System.EventArgs e)
    {
    
    
    }
    #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
        Button1.Attributes.Add("onclick","javascript:" +
                  Button1.ClientID + ".disabled=true;" +
                  this.GetPostBackEventReference(Button1));
        InitializeComponent();
        base.OnInit(e);
    }
    
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.Button1.Click +=
                new System.EventHandler(this.Button1_Click);
        this.Load += new System.EventHandler(this.Page_Load);
    }
    #endregion
    private void Button1_Click(object sender, System.EventArgs e)
    {
        System.Threading.Thread.Sleep(5000);
        //Response.Redirect("http://www.msn.com");
    }
}
引用通告地址: 點擊獲取引用地址
評論: 0 | 引用: 1 | 閱讀: 2393 | 列印
發表評論
暱 稱: 密 碼:
網 址: E - mail:
驗證碼: 驗證碼圖片 選 項:
內 容: