【问题标题】:asp.net CompareValidator not running as expected on submitasp.net CompareValidator 在提交时未按预期运行
【发布时间】:2010-11-09 14:14:41
【问题描述】:

我正在尝试让 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>

有什么想法吗?

【问题讨论】:

  • 如果您启用clientscript,验证器将在您重定向之前进行验证。因为您在代码隐藏中重定向,所以您必须先 Page.Validate 并检查 Page.IsValid,然后才能在代码隐藏中重定向。始终建议在服务器端也进行验证,因为 javascript 可能会被停用。

标签: asp.net comparevalidator


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    使用 ValidationGroup="...."

    【讨论】:

    • 这不是问题... Frédéric 已经一针见血了。
    猜你喜欢
    • 2018-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-25
    • 2014-11-12
    • 1970-01-01
    • 2020-06-28
    • 2012-02-18
    相关资源
    最近更新 更多