473,503 Members | 2,150 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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="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
2 2109
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
7515
by: Lo?c Mah? | last post by:
Hello I try to use a Validator for a TxtCtrl placed in a Panel with a Button in order to trigger the Validator and test the content of TxtCtrl. I have looked into wxPython documentation and...
0
1901
by: Sundown | last post by:
I am trying to create a custom button control for the web that, when clicked, disables and changes the text of itself and a bunch of other controls (in the collection). My goal is to end up with a...
5
6713
by: Vern | last post by:
I'd like the default text to show up in designer, and allow it to be changed to something else. I was able to change some of the other properties, but for some reason I'm having trouble with the...
3
1290
by: Vern | last post by:
I have a custom button control that compares the user security level to the buttons security level property. If the user level is greater than the button level, then the button is enabled, else it...
3
1724
by: CroDude | last post by:
Hi all! I've made a button from scratch derived from a Control class. The main reason why Control and not Button class is used for inheritance is that when deriving from a control class it's...
3
7652
by: tshad | last post by:
Is there a way to use a custom button with the: <Input ID="MyFile" Type="File" RunAt="Server">? This gives you a generic button, but I would like to use my own button that has the same style...
1
1684
by: Daniel Friend | last post by:
How do I add a custom button to the caption bar of a form like AOL does for it's favorites. Thanks, Dan
3
2399
by: Kannan | last post by:
I am creating Outlook Add-in Component using C#. I have created custom button "Formard mail" in outlook using C# component. This is displaying in Outlook menu bar. If user pressed this button, I...
2
1495
by: Bill Steele | last post by:
Wanted a "mail this page" button to not look like a button. So this: <button class="emailArticleButton" onmouseover="this.className='emailArticleButtonChange';"...
7
9061
by: Marcolino | last post by:
Hi all, I need to add a custom button to title bar on a form in addition of a standard button. (Minimize/Maximize, close etc.) I need also to handle OnClick event of this button. I looked around...
0
7205
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7093
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7349
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7008
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5022
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3168
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1521
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
746
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
399
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.