asp.net CompareValidator在提交时未按预期运行

时间:2010-11-09 14:14:41

标签: asp.net comparevalidator

我正在尝试让CompareValidator工作但有一些问题。如果提交按钮没有运行一个函数,它工作正常,但如果按钮附加了一个函数,则验证器不起作用。

这是代码。

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

<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>

任何想法?

2 个答案:

答案 0 :(得分:2)

验证器应该可以正常工作,但只有在页面有效时才应该执行重定向:

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

答案 1 :(得分:0)

使用ValidationGroup =“....”