asp:Button Validation with OnClientClick javascript - Not Validating

Last post 01-20-2010 8:18 AM by omprakash566. 25 replies.

Sort Posts:

  • asp:Button Validation with OnClientClick javascript - Not Validating

    02-15-2006, 5:03 PM
    • Member
      115 point Member
    • lap993
    • Member since 02-09-2006, 10:05 PM
    • Posts 23

    Validation worked fine until I added a OnClientClick javascript to prompt the use to make sure they want to save the form data back to a sql database.  Is my syntax worng or does the OnClientClick over ride the validation?

    See below for my asp button control syntax

    <asp:Button ID="btSaveForm" runat="server" Style="z-index: 138; left: 764px; position: absolute; top: 726px" Text="Save Form Data" Width="151px" CausesValidation="True" OnClientClick="javascript: return confirm('Are you sure you want to add this estimate data?')" />

  • Re: asp:Button Validation with OnClientClick javascript - Not Validating

    02-15-2006, 8:27 PM
    • All-Star
      26,076 point All-Star
    • budugu
    • Member since 01-12-2006, 7:15 PM
    • North Carolina
    • Posts 3,864

    Try this..

    btSaveForm.Attributes.Add("OnClientClick", "if (! confirm(''Are you sure you want to add this estimate data??')) return;");

     

    Vijay Kodali || My Blog


    "Don't be afraid to be wrong; otherwise you'll never be right."
  • Re: asp:Button Validation with OnClientClick javascript - Not Validating

    02-16-2006, 11:16 AM
    • All-Star
      30,305 point All-Star
    • PLBlum
    • Member since 06-28-2002, 9:20 AM
    • Boston, MA
    • Posts 5,344
    • TrustedFriends-MVPs

    That's almost correct. Make sure that statement returns false.

    if (! confirm('Are you sure you want to add this estimate data??')) return false;

    --- Peter Blum
    Creator of Peter's Data Entry Suite (formerly Professional Validation And More and Peter's Date Package) and Peter's Polling Package
    www.PeterBlum.com
  • Re: asp:Button Validation with OnClientClick javascript - Not Validating

    10-16-2006, 11:15 PM
    • Member
      125 point Member
    • xazos79
    • Member since 05-30-2006, 7:26 AM
    • Adelaide
    • Posts 45

    Hi all,

    Sorry to dredge this up from the dead, but i am unable to get this working with validation on my aspx pages. The following:

    btnSaveCardHolder.Attributes.Add("OnClick", "if (! confirm('Are you sure you want to save the changes made to this card holder?')) return false;");

    Gives me the confirmation dialog, but breaks validation. This:

    btnSaveCardHolder.Attributes.Add("OnClientClick", "if (! confirm('Are you sure you want to save the changes made to this card holder?')) return false;");

    Doesn't give me the dialog, and validation is fine. Is there a fix for this problem out there?

  • Re: asp:Button Validation with OnClientClick javascript - Not Validating

    10-17-2006, 1:09 AM
    • Star
      10,830 point Star
    • mokeefe
    • Member since 08-20-2006, 5:15 AM
    • Canberra Australia
    • Posts 2,098

    Actually, there are some misnomers here.

    OnClientclick is a server side attribute and as such adding it to a control in this mechanism is doing nothing other than invalidating the HTML control - non standard attribute in HTML.

    For your requirements the following is applicable

    In ASP.net 1.1 to execute client side script on the client click event you would use (note the casing) -

    Me.SubmitButton.Attributes.Add("onclick", "javascript:return confirm('You wish to do this?")

     In ASP.net 2.0 you would use -

    Me.SubmitButton.OnClientClick = "javascript:return confirm('You wish to do this?');"

    False will halt the submit.

    Regards,

    Martin

    Rgds,
    Martin.

    For the benefit of all users please mark any post answers as appropriate.
  • Re: asp:Button Validation with OnClientClick javascript - Not Validating

    10-17-2006, 1:09 AM
    • Star
      10,830 point Star
    • mokeefe
    • Member since 08-20-2006, 5:15 AM
    • Canberra Australia
    • Posts 2,098
    Replacing the Button ID as appropriate - btnSaveCardHolder
    Rgds,
    Martin.

    For the benefit of all users please mark any post answers as appropriate.
  • Re: asp:Button Validation with OnClientClick javascript - Not Validating

    10-17-2006, 8:57 PM
    • Member
      125 point Member
    • xazos79
    • Member since 05-30-2006, 7:26 AM
    • Adelaide
    • Posts 45

    Sorry Martin, i've already tried using 'OnClientClick' through the property of the button during design time and setting the property during runtime. While the confirm works fine, it breaks any validation controls on the page. This is the problem i'm having.

     

    Cheers.

  • Re: asp:Button Validation with OnClientClick javascript - Not Validating

    10-18-2006, 4:10 PM
    • All-Star
      26,076 point All-Star
    • budugu
    • Member since 01-12-2006, 7:15 PM
    • North Carolina
    • Posts 3,864
    xazos79:

    Sorry Martin, i've already tried using 'OnClientClick' through the property of the button during design time and setting the property during runtime. While the confirm works fine, it breaks any validation controls on the page. This is the problem i'm having.

     

    Cheers.

    What do you mean by breaks any validation controls? .

    Which validation control is not working? Can you please explian little bit more.

    Vijay Kodali || My Blog


    "Don't be afraid to be wrong; otherwise you'll never be right."
  • Re: asp:Button Validation with OnClientClick javascript - Not Validating

    10-18-2006, 11:21 PM
    • Member
      125 point Member
    • xazos79
    • Member since 05-30-2006, 7:26 AM
    • Adelaide
    • Posts 45
    Basically all the validation controls on the page will not fire if i set the OnClientClick property of the button with the javascript confirm method. When i comment out the OnClientClick propery and reload the page, validation works again.
  • Re: asp:Button Validation with OnClientClick javascript - Not Validating

    10-19-2006, 12:16 AM
    • Star
      10,830 point Star
    • mokeefe
    • Member since 08-20-2006, 5:15 AM
    • Canberra Australia
    • Posts 2,098

    Validation controls are called inside the page postback logic and as such should be getting called when you select yes with the confirm dialogue. However, if you select no then the validation will not be called. You could of course reverse engineer the validation component of the postback and call the validation prior to the confirm prompt, but this is likely to be a fair bit of work.

    If your page validation is not calling even after clicking yes in the confirm you have an error condition.

    Rgds,

    Martin.

    Rgds,
    Martin.

    For the benefit of all users please mark any post answers as appropriate.
  • Re: asp:Button Validation with OnClientClick javascript - Not Validating

    10-23-2006, 12:38 AM
    • Member
      125 point Member
    • xazos79
    • Member since 05-30-2006, 7:26 AM
    • Adelaide
    • Posts 45
    If there's an error condition, its not being thrown. I do have master pages, user controls, and 3rd party controls on some of my pages but it would be strange if one of those decides to disable validation when used in conjuction with OnClientClick and the confirm method. I may make a simple page with one button and one text box and see if it still breaks.
  • Re: asp:Button Validation with OnClientClick javascript - Not Validating

    10-23-2006, 2:20 AM
    • Star
      10,830 point Star
    • mokeefe
    • Member since 08-20-2006, 5:15 AM
    • Canberra Australia
    • Posts 2,098

    I did test prior to my last post. Let us know how you go.

    Martin.

    Rgds,
    Martin.

    For the benefit of all users please mark any post answers as appropriate.
  • Re: asp:Button Validation with OnClientClick javascript - Not Validating

    11-08-2007, 12:19 AM
    • Member
      2 point Member
    • CFord
    • Member since 07-12-2007, 10:18 PM
    • Posts 4

    Thanks a whole lot martin, you just saved me a whole lot of headache!

     Cheers!

    Troubled Developer
  • Re: asp:Button Validation with OnClientClick javascript - Not Validating

    11-23-2007, 3:19 AM

    Hi,

     I tried the same thing, but my required field validators, regular expression validators does not fire....

    I have checked that the following:

    <asp:Button ID="btnUpdate" runat="server" CausesValidation="true" CommandName="Update" Text="Update" Width="100px" ValidationGroup="EditValidate" OnClientClick="javascript:return confirm('Update this application?');" />

     

    What else can I do to make it work?

     

    I have been stuck at this problem for 2 weeks now....zzzzzzz

  • Re: asp:Button Validation with OnClientClick javascript - Not Validating

    11-23-2007, 10:33 AM
    • Member
      356 point Member
    • sukdeb
    • Member since 10-27-2006, 11:20 AM
    • Kolkata, India
    • Posts 45

     Can you try this...

    function Validate()
    {
        var yes=confirm("Update this application?");
        if(yes)
        {
            Page_ClientValidate();
            return Page_IsValid;
        }
    return false;

    }
    </script>

    <asp:Button runat="server" Text="Button" OnClientClick="return Validate()" /> 

    Hope it will work. 

    Thanks..
    Sukdeb

    Please click on 'Mark as Answer' if this post answered your question!
Page 1 of 2 (26 items) 1 2 Next >