custom client-side onClick events for asp:button

  • Thread starter Thread starter giant food
  • Start date Start date
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
 
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(); }
 
Back
Top