custom client-side onClick events for asp:button

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • giant food

    custom client-side onClick events for asp:button

    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="Singl eLine" Runat="server" />
    <asp:RequiredFi eldValidator ID="vName" Runat=server
    ControlToValida te="txName" />
    <asp:Button ID="btnSave" CssClass="butto n" 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=javasc ript>
    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.Attribu tes("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_ClientVali date() 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

    #2
    custom client-side onClick events for asp:button

    Hi Frank,
    This is what you need :)


    Hope that helps
    Have a great day :)
    R. Pooran Prasad
    Itreya Technologies Pvt Ltd.,
    Mail: pooran.prasad@i treya.com.REMOVETHIS
    Phone(Off) : +91 80 5200179 Extn: 42
    Mobile: +91 98860 29578[color=blue]
    >-----Original Message-----
    >Hi, I'm writing an asp app. I have a text box with a[/color]
    validator and a[color=blue]
    >submit button. Here is code from my .aspx file....
    >
    >
    ><asp:TextBox ID="txName" TextMode="Singl eLine"[/color]
    Runat="server" />[color=blue]
    ><asp:RequiredF ieldValidator ID="vName" Runat=server
    >ControlToValid ate="txName" />
    ><asp:Button ID="btnSave" CssClass="butto n" Runat="server" />
    >
    >
    >I wanted to have a javascript box pop up and ask the user[/color]
    if they're[color=blue]
    >sure they want to continue, so I added this script:
    ><script langauge=javasc ript>
    >function checkClick()
    >{
    >return confirm("are you sure...");
    >}
    ></script>
    >
    >to call this onClick, I couldn't add it directly into the[/color]
    asp:Button,[color=blue]
    >so I added it in the codebehind file (.vb):
    >
    >btnSave.Attrib utes("onClick") = "return checkClick();"
    >
    >----
    >The only problem with this is that, in the resulting HTML,
    >checkClick() is called before the client-side validation,[/color]
    so if I[color=blue]
    >click ok, the submit goes through even if the txName[/color]
    required field is[color=blue]
    >blank... i.e. the client-side validators are never called[/color]
    (ASP adds[color=blue]
    >the Page_ClientVali date() call after my checkClick() call).
    >
    >As a result, I need to check Page.IsValid on the server[/color]
    side. That's[color=blue]
    >ok (and I have to do it anyway for Netscape browsers), but[/color]
    it would be[color=blue]
    >nice to have the client-side validation and my javascript[/color]
    confirm()[color=blue]
    >message coexist.
    >
    >Any suggestions?
    >thanks,
    >Frank
    >.
    >[/color]

    Comment

    • Chris Jackson

      #3
      Re: custom client-side onClick events for asp:button

      Just replace your <asp:button> with an <input type="button"
      onclick="checkC lick()">. Then, have your checkClick() function call the
      validator: if (Page_ClientVal idate()) { document.form.s ubmit(); }

      --
      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" <dinosaur8000@y ahoo.com> wrote in message
      news:a61ab53c.0 312121151.48e39 6bd@posting.goo gle.com...[color=blue]
      > 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="Singl eLine" Runat="server" />
      > <asp:RequiredFi eldValidator ID="vName" Runat=server
      > ControlToValida te="txName" />
      > <asp:Button ID="btnSave" CssClass="butto n" 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=javasc ript>
      > 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.Attribu tes("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_ClientVali date() 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[/color]


      Comment

      Working...