custom client-side onClick events for asp:button

G

giant food

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
 
C

Chris Jackson

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(); }
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top