ASP.NET 驗證控制項與防止按鈕連按兩次送出
前陣子在網路上看到一篇不錯的文章...
介紹ASP.NET使用驗證控制項時與防止按鈕連按兩次送出的問題
在此分享此篇文章給大家呀....
資料來源:http://www.ithome.com.tw/plog/index.php?op=ViewArticle&articleId=11012&blogId=418
小弟也做了一個實例測試一下...如下:
c#範例
.aspx
01 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="doubleClick.aspx.cs" Inherits="doubleClick" %>
02
03 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
04 <html xmlns="http://www.w3.org/1999/xhtml">
05 <head id="Head1" runat="server">
06 <title>未命名頁面</title>
07 </head>
08 <body>
09 <form id="form1" runat="server">
10 <div>
11 <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><asp:RequiredFieldValidator
12 ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1" ErrorMessage="不可為空白"></asp:RequiredFieldValidator><br />
13 <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="送出" CausesValidation="true" /></div>
14 </form>
15 </body>
16 </html>
17
18 <script type="text/javascript">
19 function check(btn)
20 {
21 if (typeof(Page_ClientValidate) == 'function')
22 {
23 if(Page_ClientValidate('')==true)
24 {
25
26 btn.disabled = true;
27 __doPostBack(btn.id,"");
28 }
29 }
30 }
31 </script>
32
33
02
03 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
04 <html xmlns="http://www.w3.org/1999/xhtml">
05 <head id="Head1" runat="server">
06 <title>未命名頁面</title>
07 </head>
08 <body>
09 <form id="form1" runat="server">
10 <div>
11 <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><asp:RequiredFieldValidator
12 ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1" ErrorMessage="不可為空白"></asp:RequiredFieldValidator><br />
13 <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="送出" CausesValidation="true" /></div>
14 </form>
15 </body>
16 </html>
17
18 <script type="text/javascript">
19 function check(btn)
20 {
21 if (typeof(Page_ClientValidate) == 'function')
22 {
23 if(Page_ClientValidate('')==true)
24 {
25
26 btn.disabled = true;
27 __doPostBack(btn.id,"");
28 }
29 }
30 }
31 </script>
32
33
.cs
01 using System;
02 using System.Data;
03 using System.Configuration;
04 using System.Collections;
05 using System.Web;
06 using System.Web.Security;
07 using System.Web.UI;
08 using System.Web.UI.WebControls;
09 using System.Web.UI.WebControls.WebParts;
10 using System.Web.UI.HtmlControls;
11
12 public partial class doubleClick : System.Web.UI.Page
13 {
14 protected void Page_Load(object sender, EventArgs e)
15 {
16 this.Button1.Attributes.Add("onclick", "check(this);");
17 }
18 protected void Button1_Click(object sender, EventArgs e)
19 {
20 //模擬資料庫儲存,處理時間5秒
21 System.Threading.Thread.Sleep(5000);
22 Response.Write(this.TextBox1.Text);
23 }
24 }
02 using System.Data;
03 using System.Configuration;
04 using System.Collections;
05 using System.Web;
06 using System.Web.Security;
07 using System.Web.UI;
08 using System.Web.UI.WebControls;
09 using System.Web.UI.WebControls.WebParts;
10 using System.Web.UI.HtmlControls;
11
12 public partial class doubleClick : System.Web.UI.Page
13 {
14 protected void Page_Load(object sender, EventArgs e)
15 {
16 this.Button1.Attributes.Add("onclick", "check(this);");
17 }
18 protected void Button1_Click(object sender, EventArgs e)
19 {
20 //模擬資料庫儲存,處理時間5秒
21 System.Threading.Thread.Sleep(5000);
22 Response.Write(this.TextBox1.Text);
23 }
24 }