Search code examples
asp.neteventsvalidationpage-lifecyclecomparevalidator

Button_Click Event not raised due to CompareValidator


I have a page containing an <asp:Button/> to leave the page and a <asp:TextBox/> to get some date and another <asp:TextBox/> to confirm that data.

The confirm validator is configured as follows:

<asp:CompareValidator ID="CompareValidator" runat="server"
ErrorMessage="error message" ControlToValidate="ConfirmTextBox"
ControlToCompare="TextBox"
Operator="Equal"></asp:CompareValidator>

On the page, when editing the fields, the compare validator runs when the ControlToValidate or ControlToCompare loses focus.

When editing either fields, then clicking the button to leave the page, the compare validator runs and displays the error message but the Button_Click method is not run.

The causesValidation attribute of the button is set to false.

Can I make the Button_Click method run while maintaining the compare validator's functionality, without resorting to server validating or a regexValidator that uses the TextBox.Text value?


Solution

  • The CompareValidator performs the comparison all on the client side in the user's browser. If it raises an error, then it will automatically prevent the Button_Click event from firing, since a postback won't occur. I think your solution is to just perform the comparison on the server side.