Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > custom client-side onClick events for asp:button

Reply
Thread Tools

custom client-side onClick events for asp:button

 
 
giant food
Guest
Posts: n/a
 
      12-12-2003
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
 
Reply With Quote
 
 
 
 
Pooran Prasad
Guest
Posts: n/a
 
      12-12-2003
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
>.
>

 
Reply With Quote
 
 
 
 
Chris Jackson
Guest
Posts: n/a
 
      12-12-2003
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: om...
> 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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Overriding an onclick with another onclick tomlong@gmail.com Javascript 4 01-26-2006 09:26 PM
button.onclick = new Function("func2()") + button.onclick foldface@yahoo.co.uk Javascript 2 09-26-2005 08:13 AM
Events Events Events Please Help Chris ASP .Net Web Controls 0 08-30-2005 08:21 PM
OnClick Events Yama ASP .Net 0 01-03-2004 05:50 AM
document.onclick=doIt() same as document.onclick=doIt ? bob Javascript 3 08-21-2003 12:14 PM



Advertisments