Search code examples
asp.netcomparevalidator

asp.net CompareValidator not running as expected on submit


I'm trying to get a CompareValidator to work but having a few problems. If the submit button does not run a function it works fine, but the validator does not work if there is a function attached to the button.

Here's the code.

Sub myGo(sender As Object, e As EventArgs)
    response.redirect("http://www.google.co.uk")
End Sub

and

<form runat="server">
<asp:TextBox id="txt1" runat="server" /> = <asp:TextBox id="txt2" runat="server" /> <asp:Button OnClick="myGo" Text="not working with onclick" runat="server" /> <asp:Button Text="working button" runat="server" />
<br />
<asp:CompareValidator EnableClientScript="false" id="compval" Display="dynamic" ControlToValidate="txt1" ControlToCompare="txt2" Type="String" Text="Validation Failed!" runat="server" />
</form>

any ideas?


Solution

  • The validator should work fine, but you probably ought to perform the redirect only if the page is valid:

    Sub myGo(sender As Object, e As EventArgs)
        If Page.IsValid Then
            Response.Redirect("http://www.google.co.uk")
        End If
    End Sub