參考資料1.重覆按下Button,造成重覆寫入問題??
參考資料2.How to avoid double click on submit button?
此篇參考上方資料,實作一次,並紀錄下來,請透過實作自行體驗。
程式碼如下
aspx檔
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript" src="Scripts/jquery-1.4.1.min.js"></script>
<script type="text/javascript">
var strpoint = '';
function rec() {
if (strpoint != '') {
if (strpoint.length <= 2) {
strpoint = strpoint + '.';
} else {
strpoint = '';
};
} else {
strpoint = '.';
};
$('#<%=Label1.ClientID %>').html('訊息:請稍候' + strpoint);
setTimeout("rec();", 1000);
};
function button1() {
$('#<%=Button1.ClientID %>').attr('disabled', 'disabled');
setTimeout(function () { rec(); }, 500);
return true;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="法1" UseSubmitBehavior="False"/>
<asp:Button ID="Button2" runat="server" Text="法2" /><br/>
<asp:Label ID="Label1" runat="server" />
</div>
</form>
</body>
</html>
apsx.vb檔
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
'方法1的第一種方法,此方法直接在onclick上寫入javascript語法
'Button1.Attributes.Add("onclick", "this.disabled=true;")
Button1.Attributes.Add("onclick", "button1();") '方法1的第二種方法
'方法2 ClientScript.GetPostBackEventReference等於是加入__doPostBack方法
Button2.Attributes.Add("onclick", "this.disabled=true;" & ClientScript.GetPostBackEventReference(Button2, "").ToString())
End If
End Sub
Sub Loading()
For i As Integer = 0 To 5000000
Label1.Text = i.ToString()
Next
End Sub
Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
'以下程式碼只是展示效果,沒有特別作用
Loading()
End Sub
Protected Sub Button2_Click(sender As Object, e As System.EventArgs) Handles Button2.Click
Loading()
End Sub
以上如有錯誤請指正,謝謝
沒有留言:
張貼留言