I have just created a custom control. it consists of a radiobuttonlist, and
a validator for that radiobuttonlist. I've added a property in my control
that holds a placeholder. I want to use this placeholder, so that I can add
the validator of my control somewhere else in the page than "inside" my
custom control.
By design it would be like this:
<someplaceholder>[Here goes the validator]</someplaceholder>
<myControl>[Here goes the radioButtonlist]</myControl>
What happens, is that I get this error:
"Control '_ctl0' referenced by the ControlToValidate property of
'ctrlReqFieldBool' cannot be validated."
Seems kinda strange to me, as I have no problem adding other things to my
"validatorPlacement" referenced control:
validatorPlacement.Controls.Add(new TextBox());
works just fine
In the .net reference, this is what I found about the validator:
"The ID must refer to a control within the same container as the validation
control. It must be in the same page or user control, or it must be in the
same template of a templated control."
I'm not sure if this means that I can't do what I'm trying to.
Here is some of my code:
--------------------------------------------------------------------------
public class
quizAns_Boolean:System.Web.UI.WebControls.WebContr ol,INamingContainer
{
private PlaceHolder ctrlControl;
public PlaceHolder validatorPlacement
{
get{return ctrlControl;}
set{ctrlControl=value;}
}
protected override void CreateChildControls()
{
//the radiobuttonlist
RadioButtonList ctrlAnsBool=new RadioButtonList();
ctrlAnsBool.ID=this.ID;
foreach(DataRow dr in ctrlDataSource)
{
ctrlAnsBool.Items.Add(new
ListItem(dr["answer"].ToString(),dr["answerID"].ToString()));
}
this.Controls.Add(ctrlAnsBool);
//the validator
RequiredFieldValidator ctrlReqFieldBool=new RequiredFieldValidator();
ctrlReqFieldBool.ID="ctrlReqFieldBool";
ctrlReqFieldBool.ControlToValidate=ctrlAnsBool.ID;
ctrlReqFieldBool.Display=ValidatorDisplay.Static;
ctrlReqFieldBool.Text="Required Answer";
ctrlReqFieldBool.ErrorMessage="Error";
//this one works just fine
validatorPlacement.Controls.Add(new TextBox());
//add the validator somewhere else, gives me an error
validatorPlacement.Controls.Add(ctrlReqFieldBool);
}
}