I wrote this code and I am trying to figure out what I have done wrong with the customValidator and I get this error. Could anyone help me with this.
In my form I have many radio buttons inside a repeater and I generate the radioButton.ID based on some unique fields that are coming from the database. Radiobuttons that belong to the same category are given the same GroupName like this:
(so my logic is that even if one RadioButton of that Group is selected, that group is validated)enter code here
radioButton.GroupName = dataRowTemp["QuestionID"].ToString() + dataRowTemp["ControlID"].ToString();
I have the following code in the code behind .aspx.cs file
CustomValidator customValidator = new CustomValidator();
customValidator.ControlToValidate = radioButton.ID;
customValidator.ClientValidationFunction = "checkRadiobuttonSelection";
customValidator.ValidateEmptyText = true;
customValidator.EnableClientScript = true;
e.Item.Controls.Add(customValidator);
and I have the following code in the .aspx file
<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>
When I execute the code, I get this error
[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
Any pointers for me? Thank you.
I have a similar code for text boxes, which works fine and validates the text boxes neatly.
Text Boxes code which works fine
.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>
You can’t use ControlToValidate with radio buttons. You need to look into creating a custom validator, for which this answer should help.