【问题标题】:asp.net validator add class to input if validation is wrong如果验证错误,asp.net 验证器将类添加到输入
【发布时间】:2012-01-31 09:58:27
【问题描述】:

如果值无效,是否可以将 css 类添加到经过验证的输入?我正在使用 asp.net 验证器。

也许我可以使用 taht 验证器上的 javascript 事件?

感谢您的建议

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    使用普通的 ASP.NET 验证器很难做到这一点,因为它不提供丰富的客户端库。

    如果您不需要更改当前的验证器类型。您可以使用来自 ASP.NET Ajax Toolkit 的 ValidatorCallout 控件的 HighlightCssClass 属性。

    您可以在此处查看演示:

    http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/ValidatorCallout/ValidatorCallout.aspx

    【讨论】:

      【解决方案2】:

      你可以像这样使用内置的CustomValidator

      <asp:CustomValidator ID="vldCustomValidator" EnableClientScript="true"
          ErrorMessage="Your error message goes here"
          ClientValidationFunction="JavascriptValidationFunction" 
          ControlToValidate="txbControl"
          runat="server" />
      

      使用场景http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.customvalidator.clientvalidationfunction.aspx

      【讨论】:

        【解决方案3】:

        您可以覆盖默认的 ASP.NET 验证脚本并注入您自己的脚本以将类添加到父 div。

        更多信息在这里:https://stackoverflow.com/a/9857696/618044

        【讨论】:

          【解决方案4】:

          你可以这样做。您添加自定义 HTML 标记和 CSS 类。

          <asp:RequiredFieldValidator ID="RrequiredFieldValidator1" runat="server"
              ControlToValidate="txtCustomerName" ForeColor="Red"
              Display="Dynamic">
              <span class="bubbletooltip">Please Enter Name Here</span>
             </asp:RequiredFieldValidator>
          

          【讨论】:

            猜你喜欢
            • 2014-07-11
            • 2011-12-18
            • 2019-11-02
            • 2014-11-10
            • 1970-01-01
            • 2022-01-23
            • 2012-08-14
            • 2017-12-31
            • 1970-01-01
            相关资源
            最近更新 更多