Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Professional
|
ASP.NET 2.0 Professional If you are an experienced ASP.NET programmer, this is the forum for your 2.0 questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 22nd, 2008, 01:50 PM
Banned
 
Join Date: Jul 2005
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Default Disable Submit Button w/OnClientClick: Not Working

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
 
Old August 22nd, 2008, 04:21 PM
Banned
 
Join Date: Jul 2005
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Before I received a response from this forum, I got referred to this article via the ASP.NET "web form" forum: http://dotnetchris.wordpress.com/200...-double-click/

...and it worked great!!! Thanks anyway:)

KWilliams
 
Old August 22nd, 2008, 04:23 PM
Banned
 
Join Date: Jul 2005
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Default

...sorry. Here's the full link: http://dotnetchris.wordpress.com/200...-double-click/

KWilliams
 
Old August 25th, 2008, 09:55 AM
Banned
 
Join Date: Jul 2005
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Default

...spoke too soon:

I'm having an issue with this solution: I have required field and regular expression validators on my web form. If a user clicks the submit button when the form is invalid, the submit button is disabled and the user cannot correct their mistake. Could you give me an idea of how to solve this? I'm trying different solutions to this issue out without success so far. Thanks for any help.

KWilliams
 
Old August 25th, 2008, 12:00 PM
Banned
 
Join Date: Jul 2005
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ok, got it. Here is the full solution (with help from another forum):

Sub Page_Load(ByVal Sender As Object, ByVal E As EventArgs)
        If Not Page.IsPostBack Then
            'Disable submit button when VALID form is submitted
            btnSubmit.OnClientClick = "javascript: Page_ClientValidate(); if (Page_IsValid) { " + btnSubmit.ClientID + ".style.visibility = 'hidden';" + btnSubmit.ClientID + ".style.display = 'none';" + btnSubmitDisabled.ClientID + ".style.visibility = 'visible';return true}"
        End If
    End Sub

This needs to be placed before the btnSubmit_Click event. Thanks:)

KWilliams





Similar Threads
Thread Thread Starter Forum Replies Last Post
Disable a button sansircar ASP.NET 1.0 and 1.1 Professional 6 January 12th, 2007 09:38 AM
Make a image button as default submit button toshi ASP.NET 1.0 and 1.1 Basics 1 June 1st, 2006 05:25 AM
submit-button grstad Javascript How-To 1 May 25th, 2005 08:33 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.