Inherited Textbox required validator

B

bcutting

I have a control which inherits from TextBox and supplies some
additional data functionality. I was hoping to include a boolean flag
"Required" that can be set in the designer. When the flag is set to
True the control should include a RequiredFieldValidator for itself.
No matter how I add the validator within the control it never seems to
show up in the page.

protected override void CreateChildControls()
{
if(m_bRequiredField)
{
RequiredFieldValidator rfv = new RequiredFieldValidator ();
rfv.ControlToValidate = this.ID;
rfv.Display = ValidatorDisplay.Dynamic;
rfv.ErrorMessage = UserFieldName + " is required.";
this.Controls.Add(rfv);
}
base.CreateChildControls ();
}


I would like to avoid creating a composite control. I have also tried
other events such as PreRender and DataBind.

I even tried hacking the text HTML for the validator in the Render and
add an empty validator to the Page.Validators with the same clientID.
But the page doesn't seem to register it.

my attempted hack

protected override void OnPreRender(EventArgs e)
{
RequiredFieldValidator rvf = new RequiredFieldValidator ();
rvf.ID=this.ClientID + "_Validator";
Page.Validators.Add(rvf);
base.OnPreRender (e);
}
protected override void Render(HtmlTextWriter writer)
{
base.Render (writer);
if(m_bRequiredField)
{
writer.Write("<span id=\""+this.ClientID+"_Validator\" " +
"controltovalidate=\"" + this.ClientID + "\" " +
"errormessage=\""+UserFieldName+" is required\" display=\"Dynamic\"
evaluationfunction=\"RequiredFieldValidatorEvaluateIsValid\" " +
"initialvalue=\"\" style=\"color:Red;display:none;\">*</span>");
}
}


Any ideas would be appreciated.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top