<script type="text/JavaScript"> function DisableControl(controlId) { document.getElementById(controlId).disabled =true; } function DisableControl_SetTimeout(controlId,interval) { setTimeout("DisableControl('"+controlId +"')",interval); } function btnSave_Click(control) { DisableControl_SetTimeout(control.id,100); } </script>
PrivateSub Page_Load()Sub Page_Load(ByVal sender As System.Object,_ ByVal e As System.EventArgs) HandlesMyBase.Load IfNotMe.Page.IsPostBack Then Me.btnSave.Attributes.Add("onclick",_"btnSave_Click(this);") EndIf End Sub
<%@ Page Language="VB" %> <script runat="server"> Dim counter As Integer Sub Page_Load() If Session("counter") Is Nothing Then counter = 0 Else counter = CInt( Session("counter") ) End If SafeButton.Attributes.Add("onclick", "disableButton();return true") End Sub
Sub Process_Click(sender As Object, e As EventArgs) counter += 1 Session("counter") = counter Dim i As Long ' 2-second delay Dim endTime As DateTime = DateTime.Now.AddSeconds(2) While DateTime.Now < endTime End While Label1.Text = counter.ToString() Label2.Text = DateTime.Now.ToString() End Sub
Sub ClearCounter_Click(sender As Object, e As EventArgs) Session("counter") = 0 Label1.Text = Session("counter").ToString() End Sub </script> <HTML> <head> <style> BODY { FONT-SIZE: 10pt; FONT-FAMILY: verdana; arial: } H2 { FONT-SIZE: 14pt; FONT-FAMILY: verdana; arial: font-weight:bold } </style> <script language="JavaScript"> function disableButton() { document.form1.SafeButton.style.visibility = "hidden"; } </script> </head> <body> <form id="form1" runat="server"> <h2>Testing Multiple Posting </h2> <p> Process count: <ASP:Label id="Label1" runat="server" forecolor="Red" font-bold="True"> </ASP:Label> </p> <p> Last clicked: <ASP:Label id="Label2" runat="server"></ASP:Label> </p> <p> The buttons run a process that takes 2 seconds to run. </p> <p> You can click this button multiple times during processing, potentially resulting in the process running multiple times: </p> <p> <ASP:Button id="UnsafeButton" onclick="Process_Click" runat="server" Text="Unsafe Button"> </ASP:Button> </p> <p> You can click this button only once to run the process: </p> <p> <ASP:Button id="SafeButton" onclick="Process_Click" runat="server" Text="Safe Button"> </ASP:Button> </p> <p> <ASP:LinkButton id="ClearCounter" onclick="ClearCounter_Click" runat="server" Text="Clear Counter"> </ASP:LinkButton> </p> <span id="span1"></span> </form> </body> </HTML>
function disableButton(){ window.setTimeout('disableAfterTimeout()',0);}function disableAfterTimeout(){ document.form1.SafeButton.disabled =true;}
<%@ Page Language="VB" %> <script runat="server"> Protected sessionGuid As String Protected viewstateGuid As String Protected doublePost As Boolean Protected pageCounter As Integer
Sub Page_Load() If Session("pageCounter") Is Nothing Then pageCounter = 0 Session("pageCounter") = 0 Else pageCounter = CInt(Session("pageCounter")) End If
Dim newGuid As String = System.guid.NewGuid.ToString If IsPostBack Then sessionGuid = Session("guid").ToString() viewstateGuid = Viewstate("guid").ToString() If sessionGuid = viewstateGuid Then doublePost = false Else doublePost = true Response.Write("Page has already been submitted.") End If End If Viewstate("guid") = newGuid Session("guid") = newGuid End Sub
Sub Button1_Click(sender As Object, e As EventArgs) System.Threading.Thread.Sleep(2000) ' Fake a long process If Not DoublePost Then pageCounter += 1 Session("pageCounter") = pageCounter End If Label1.Text = pageCounter.ToString() End Sub