By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,307 Members | 2,102 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,307 IT Pros & Developers. It's quick & easy.

custom client-side onClick events for asp:button

P: n/a
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
Nov 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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: po***********@itreya.com.REMOVETHIS
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 asubmit 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'resure 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 Iclick ok, the submit goes through even if the txName required field isblank... i.e. the client-side validators are never called (ASP addsthe Page_ClientValidate() call after my checkClick() call).

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

Any suggestions?
thanks,
Frank
.

Nov 18 '05 #2

P: n/a
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" <di**********@yahoo.com> wrote in message
news:a6**************************@posting.google.c 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

Nov 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.