【问题标题】:Change color of text box with CustomValidator使用 CustomValidator 更改文本框的颜色
【发布时间】:2012-03-12 20:16:09
【问题描述】:

我正在运行时创建一些文本框,如果文本框留空,我想更改文本框的颜色 然后用户提交表单。

我正在使用代码隐藏方法,这是我在 .aspx.cs 文件中编写的代码

textBoxObj 是我在运行时创建的文本框对象,它是我想要对其进行空验证的对象。

CustomValidator customValidatorObj = new CustomValidator();
customValidatorObj.ControlToValidate = textBoxObj.ID;
customValidatorObj.ClientValidationFunction = "changeColorofTextBox";

我在 .aspx 文件中编写了一个小的 Javascript sn-p,如下所示(我还没有编写更改颜色的逻辑, 只是让它暂时无效)

<script type="text/javascript">
    function changeColorofTextBox(oSrc, args) {
        if (args.Value.length > 0) {
            args.IsValid = true;
        }
        else {
            args.IsValid = false;
        }
    }
</script>   

在表单的提交按钮点击功能中,我有这个检查if(Page.IsValid),然后提交表单。但是,即使文本框为空, 表单被提交。似乎该功能甚至没有被击中。你对我做错了什么有任何指示吗? 无论是客户端验证还是服务器端验证,我都可以。

编辑

我得到了错误,我只需要这样做

customValidatorObj.ValidateEmptyText = true;

它开始工作了。谢谢,我没有意识到 customValidator 类不会尝试验证控件是否为空白。

但我又卡住了:(

在表单中,我有很多文本框。假设用户输入了 3 个文本框的文本并将其中 2 个留空,我如何找到文本框 id 以便我只能更改空白的颜色。或者,如何在 javascript 中编写代码以在运行时找出控件 ID?

我知道我们必须这样做

document.getElementById(CONTROLIDGOESHERE).style.backgroundColor = "red";

但是我如何将 CONTROLIDGOESHERE 值传递给 getElementById 函数?

任何指点,谢谢。

【问题讨论】:

    标签: c# asp.net validation customvalidator


    【解决方案1】:

    尝试设置 customValidatorObj.EnableClientScipt = True

    【讨论】:

      【解决方案2】:

      假设您运行的是 .NET Framework 4.0 版,那么您可以使用 ClientIDMode="Static" 声明您的文本框。这样他们将在客户端和服务器端拥有相同的 ID,例如

      <asp:TextBox runat="server" ID="txtName" ClientIDMode="Static" />
      

      然后您可以通过声明这样的按钮来触发按钮单击时的客户端验证:

      <input type="submit" id="btnSubmit" onclick="ClientSideValidation(); return false;" value="Save"/>
      

      JavaScript 函数可能如下所示:

      <script type="text/javascript">
          function ClientSideValidation() {
              var txtName = document.getElementById("txtName");
              if (txtName.value.length == 0) {
                  txtName.style.background = "#DE0000";
              }
              // Check other text boxes...
          }
      </script>
      

      【讨论】:

        【解决方案3】:

        谢谢各位,我知道了。这段代码为我完成了这项工作

        .aspx.cs

            CustomValidator customValidator = new CustomValidator();
            customValidator.ControlToValidate = textBox.ID;
            customValidator.ClientValidationFunction = "changeColorofTextBox";
            customValidator.ValidateEmptyText = true;
            customValidator.EnableClientScript = true;
            e.Item.Controls.Add(customValidator);
        

        .aspx

        <script type="text/javascript">
            function changeColorofTextBox(oSrc, args) {
                if (args.Value.length > 0) {
                    args.IsValid = true;
                }
                else {
                    var ctrlid = oSrc.id;
                    var validatorid = document.getElementById(ctrlid);
                    ctrlid = validatorid.controltovalidate;
                    document.getElementById(ctrlid).style.backgroundColor = "Tomato";
                    args.IsValid = false;
                }
            }
        </script>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-10-01
          • 1970-01-01
          • 1970-01-01
          • 2016-11-20
          • 1970-01-01
          • 2010-10-30
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多