custom client-side onClick events for asp:button

Discussion in 'ASP .Net' started by giant food, Dec 12, 2003.

  1. giant food

    giant food Guest

    Hi, I'm writing an asp app. I have a text box with a validator and a
    submit button. Here is code from my .aspx file....


    <asp:TextBox ID="txName" TextMode="SingleLine" Runat="server" />
    <asp:RequiredFieldValidator ID="vName" Runat=server
    ControlToValidate="txName" />
    <asp:Button ID="btnSave" CssClass="button" Runat="server" />


    I wanted to have a javascript box pop up and ask the user if they're
    sure they want to continue, so I added this script:
    <script langauge=javascript>
    function checkClick()
    {
    return confirm("are you sure...");
    }
    </script>

    to call this onClick, I couldn't add it directly into the asp:Button,
    so I added it in the codebehind file (.vb):

    btnSave.Attributes("onClick")= "return checkClick();"

    ----
    The only problem with this is that, in the resulting HTML,
    checkClick() is called before the client-side validation, so if I
    click ok, the submit goes through even if the txName required field is
    blank... i.e. the client-side validators are never called (ASP adds
    the Page_ClientValidate() call after my checkClick() call).

    As a result, I need to check Page.IsValid on the server side. That's
    ok (and I have to do it anyway for Netscape browsers), but it would be
    nice to have the client-side validation and my javascript confirm()
    message coexist.

    Any suggestions?
    thanks,
    Frank
     
    giant food, Dec 12, 2003
    #1
    1. Advertising

  2. Hi Frank,
    This is what you need :)
    http://www.dotnetjohn.com/articles/articleid39.aspx

    Hope that helps
    Have a great day :)
    R. Pooran Prasad
    Itreya Technologies Pvt Ltd.,
    Mail: VETHIS
    Phone(Off) : +91 80 5200179 Extn: 42
    Mobile: +91 98860 29578
    >-----Original Message-----
    >Hi, I'm writing an asp app. I have a text box with a

    validator and a
    >submit button. Here is code from my .aspx file....
    >
    >
    ><asp:TextBox ID="txName" TextMode="SingleLine"

    Runat="server" />
    ><asp:RequiredFieldValidator ID="vName" Runat=server
    >ControlToValidate="txName" />
    ><asp:Button ID="btnSave" CssClass="button" Runat="server" />
    >
    >
    >I wanted to have a javascript box pop up and ask the user

    if they're
    >sure they want to continue, so I added this script:
    ><script langauge=javascript>
    >function checkClick()
    >{
    >return confirm("are you sure...");
    >}
    ></script>
    >
    >to call this onClick, I couldn't add it directly into the

    asp:Button,
    >so I added it in the codebehind file (.vb):
    >
    >btnSave.Attributes("onClick")= "return checkClick();"
    >
    >----
    >The only problem with this is that, in the resulting HTML,
    >checkClick() is called before the client-side validation,

    so if I
    >click ok, the submit goes through even if the txName

    required field is
    >blank... i.e. the client-side validators are never called

    (ASP adds
    >the Page_ClientValidate() call after my checkClick() call).
    >
    >As a result, I need to check Page.IsValid on the server

    side. That's
    >ok (and I have to do it anyway for Netscape browsers), but

    it would be
    >nice to have the client-side validation and my javascript

    confirm()
    >message coexist.
    >
    >Any suggestions?
    >thanks,
    >Frank
    >.
    >
     
    Pooran Prasad, Dec 12, 2003
    #2
    1. Advertising

  3. Just replace your <asp:button> with an <input type="button"
    onclick="checkClick()">. Then, have your checkClick() function call the
    validator: if (Page_ClientValidate()) { document.form.submit(); }

    --
    Chris Jackson
    Software Engineer
    Microsoft MVP - Windows Client
    Windows XP Associate Expert
    --
    More people read the newsgroups than read my email.
    Reply to the newsgroup for a faster response.
    (Control-G using Outlook Express)
    --

    "giant food" <> wrote in message
    news:...
    > Hi, I'm writing an asp app. I have a text box with a validator and a
    > submit button. Here is code from my .aspx file....
    >
    >
    > <asp:TextBox ID="txName" TextMode="SingleLine" Runat="server" />
    > <asp:RequiredFieldValidator ID="vName" Runat=server
    > ControlToValidate="txName" />
    > <asp:Button ID="btnSave" CssClass="button" Runat="server" />
    >
    >
    > I wanted to have a javascript box pop up and ask the user if they're
    > sure they want to continue, so I added this script:
    > <script langauge=javascript>
    > function checkClick()
    > {
    > return confirm("are you sure...");
    > }
    > </script>
    >
    > to call this onClick, I couldn't add it directly into the asp:Button,
    > so I added it in the codebehind file (.vb):
    >
    > btnSave.Attributes("onClick")= "return checkClick();"
    >
    > ----
    > The only problem with this is that, in the resulting HTML,
    > checkClick() is called before the client-side validation, so if I
    > click ok, the submit goes through even if the txName required field is
    > blank... i.e. the client-side validators are never called (ASP adds
    > the Page_ClientValidate() call after my checkClick() call).
    >
    > As a result, I need to check Page.IsValid on the server side. That's
    > ok (and I have to do it anyway for Netscape browsers), but it would be
    > nice to have the client-side validation and my javascript confirm()
    > message coexist.
    >
    > Any suggestions?
    > thanks,
    > Frank
     
    Chris Jackson, Dec 12, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeff

    asp:Button client-side events

    Jeff, Oct 24, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    7,112
    David
    Oct 24, 2003
  2. =?Utf-8?B?U2VkZWY=?=

    overriding client-side onclick of Button

    =?Utf-8?B?U2VkZWY=?=, Feb 19, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    747
    Peter Blum
    Feb 21, 2004
  3. Mark Rae
    Replies:
    4
    Views:
    19,235
    Mark Rae
    Jul 26, 2004
  4. Zoe Hart
    Replies:
    1
    Views:
    373
    Scott Wisniewski
    Jan 8, 2004
  5. Replies:
    2
    Views:
    261
Loading...

Share This Page