I have a web form that collects basic data from a user (name, address, etc.), and then submits that data to a SQL Server 2000 table. It works great, except that some of those users click on the submit button multiple times, which submits duplicate entries into the database.
So I'd like to disable the Submit button upon the button being clicked and the form being valid. I Google'd "OnClientClick Property with validation" (see
http://www.google.com/search?client=...=Google+Search), and tried several of their different suggestion without success. Here's what happens with each attempt:
ATTEPT #1 Add "this.disabled=true;" to submit button's OnClientClick property directly to the asp.net web form:
regform.aspx.vb (#1):
Code:
Partial Class regform
Inherits System.Web.UI.Page
Private ds As New DataSet()
Dim sqlConn As SqlConnection
Dim sqlCmd As SqlCommand
Dim strConnection As String
Private cmd As New SqlCommand() 'Here we declare the parameter which we have to use in our application
Sub SubmitBtn_Click(ByVal Sender As Object, ByVal E As EventArgs)
If Page.IsValid Then
Try
'Jobs go there that send data to database
Catch
lblErrorMsg.Text = "There was a problem while submitting this form. Please contact the Webmaster at xxx-xxxx.</a>."
End Try
End If
End Sub
End Class
regform.aspx (#1):
Code:
<%@ Page Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" EnableSessionState="true" EnableViewState="true" CodeFile="regform.aspx.vb" Inherits="regform" title="Registration Form" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<% If Not Page.IsPostBack Then%>
<h2>Registration Form</h2>
<br />
This form will allow for you to register online for class(es):
<br /><br />
<form id="formReg" name="formReg" action="" method="post" runat="server">
***
<asp:Button ID="btnSubmit" Text="Submit" OnClick="SubmitBtn_Click" runat="server" OnClientClick="this.disabled=true;" />
</form>
<% Else %>
<asp:label id="lblErrorMsg" runat="server" /><br />
<% End If %>
</asp:Content>
#1 result:
The submit button does get disabled once it's clicked, but the SubmitBtn_Click job doesn't fire at all.
ATTEPT #2 Add "javascript:this.disabled=true;" to submit button's OnClientClick property via code file:
regform.aspx.vb (#2):
Code:
Partial Class regform
Inherits System.Web.UI.Page
Private ds As New DataSet()
Dim sqlConn As SqlConnection
Dim sqlCmd As SqlCommand
Dim strConnection As String
Private cmd As New SqlCommand() 'Here we declare the parameter which we have to use in our application
Sub SubmitBtn_Click(ByVal Sender As Object, ByVal E As EventArgs)
If Page.IsValid Then
Try
'Disable button when form is submitted
'btnSubmit.Attributes.Add("OnClientClick", "this.disabled=true;")
'Jobs go there that send data to database
Catch
lblErrorMsg.Text = "There was a problem while submitting this form. Please contact the Webmaster at xxx-xxxx.</a>."
End Try
End If
End Sub
End Class
regform.aspx (#2):
Code:
<%@ Page Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" EnableSessionState="true" EnableViewState="true" CodeFile="regform.aspx.vb" Inherits="regform" title="Registration Form" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<% If Not Page.IsPostBack Then%>
<h2>Registration Form</h2>
<br />
This form will allow for you to register online for class(es):
<br /><br />
<form id="formReg" name="formReg" action="" method="post" runat="server">
***
<asp:Button ID="btnSubmit" Text="Submit" OnClick="SubmitBtn_Click" runat="server" />
</form>
<% Else %>
<asp:label id="lblErrorMsg" runat="server" /><br />
<% End If %>
</asp:Content>
#2 result:
The web form goes through fine with the validation, but the button never gets disabled, and the form can be submitted multiple times.
The submit button does get disabled once it's clicked, but the SubmitBtn_Click job doesn't fire at all.
Using suggestions from the Google search results, I've also tried adding the following properties to the Page_Load and SubmitBtn_Click jobs within the code-file without success:
btnSubmit.Attributes.Add("OnClientClick", "this.style.display='none';")
btnSubmit.Enabled = False
btnSubmit.Attributes.Add("onclick", "this.disabled=true;")
Me.btnSubmit.Attributes.Add("onclick", Me.Page.ClientScript.GetPostBackEventReference(btn Submit, [String].Empty) + ";this.value='Please wait...';this.disabled = true;")
btnSubmit.Attributes.Item("onclick") = "this.disabled=true;"
btnSubmit.Attributes.Item("onclick") = "javascript
:document.formReg.btnSubmit.disable d =1;"
btnSubmit.Attributes.Add("onclick", ClientScript.GetPostBackEventReference(btnSubmit) & ";this.value='Please wait...';this.disabled = true;")
If anyone could help me and/or point me in the right direction, I'd very much appreciate it. Thanks.
KWilliams