views:

143

answers:

0

I'm building a data entry screen that uses web services to update the db rather than the usual postback. However, I'd still like to use the AJAX validation classes to do my client side validation.

Here's an edited version of what I've got so far:

function ValidateData() {

   var res = Page_ClientValidate("Update");

   if (res == true) {
       //Update db
   }

   return false;
}



function DoNothing() {
   return false;
}

<asp:TextBox runat="server" ID="tb1" Width="50px"></asp:TextBox>
<asp:RequiredFieldValidator ID="rv1" runat="server" ControlToValidate="tb1" ValidationGroup="Update" ErrorMessage="Enter data"></asp:RequiredFieldValidator>
<asp:Button ID="UpdateButton" runat="server" OnClientClick="return ValidateData()" CausesValidation="true" ValidationGroup="Update" Text="Update" />
<asp:Button ID="CancelButton" runat="server" OnClientClick="return DoNothing()" CausesValidation="false" Text="Cancel" />

So, an Update button that runs Page_ClientValidate, and a Cancel button that (in this example) does nothing. If you put this in a page and load it, it works fine - clicking on the Update button validates the empty textbox and raises an error, and clicking on the Cancel button does nothing.

However, if you put some data in the textbox, and then delete it so the textbox is empty again, and then hit the Cancel button again, the validator runs and raises an error! I cannot understand why the second click on the Cancel button is causing the validator to run.

Any ideas?