这段代码我做错了什么?

时间:2012-03-13 13:53:43

标签: c# asp.net validation customvalidator

我写了这段代码,我试图弄清楚我对customValidator做错了什么,我得到了这个错误。任何人都可以帮我这个。

在我的表单中,我在转发器中有许多单选按钮,我根据来自数据库的一些唯一字段生成radioButton.ID。属于同一类别的Radiobutton被赋予相同的GroupName,如下所示: (所以我的逻辑是,即使选择了该组的一个RadioButton,该组也会被验证)enter code here

radioButton.GroupName = dataRowTemp["QuestionID"].ToString() + dataRowTemp["ControlID"].ToString();

我在.aspx.cs文件后面的代码中有以下代码

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

我在.aspx文件中有以下代码

<script type="text/javascript">
    function checkRadiobuttonSelection(oSrc, args) {
        args.IsValid = false;
        var element;
        var element2;
        var ctrlid = oSrc.id;
        var validatorid = document.getElementById(ctrlid);
        ctrlid = validatorid.controltovalidate;
        element2 = document.getElementById(ctrlid);
        for (var i = 0; i < document.forms[0].elements.length; i++) {
            element = document.forms[0].elements[i];
            if (element.type == "radio") {
                if (element.GroupName == element2.GroupName) {
                    if (element.checked == true) {
                        args.IsValid = true;
                    }
                }
            }
        }
    }       
</script>

当我执行代码时,我收到此错误

[HttpException (0x80004005): Control '1' referenced by the ControlToValidate property of '' cannot be validated.]
   System.Web.UI.WebControls.BaseValidator.CheckControlValidationProperty(String name, String propertyName) +8757509
   System.Web.UI.WebControls.CustomValidator.ControlPropertiesValid() +35
   System.Web.UI.WebControls.BaseValidator.get_PropertiesValid() +21
   System.Web.UI.WebControls.BaseValidator.OnPreRender(EventArgs e) +27
   System.Web.UI.Control.PreRenderRecursiveInternal() +80
   System.Web.UI.Control.PreRenderRecursiveInternal() +171
   System.Web.UI.Control.PreRenderRecursiveInternal() +171
   System.Web.UI.Control.PreRenderRecursiveInternal() +171
   System.Web.UI.Control.PreRenderRecursiveInternal() +171
   System.Web.UI.Control.PreRenderRecursiveInternal() +171
   System.Web.UI.Control.PreRenderRecursiveInternal() +171
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +842

对我来说有什么指示?谢谢。

我有一个类似的文本框代码,工作正常,并整齐地验证文本框。

文本框代码可以正常工作

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

1 个答案:

答案 0 :(得分:0)

您不能将ControlToValidate与单选按钮一起使用。您需要考虑创建一个自定义验证器,this answer应该有帮助。