By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,213 Members | 2,172 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,213 IT Pros & Developers. It's quick & easy.

Adding validation controls programmatically

P: n/a
I am creating a server control that adds web controls (i.e. textboxes, etc) to a form. I use HtmlTable to build the table and insert the controls. Now I want to add validators to the textbox. Here is the code that I am using

bool lastnamerequired=false
public bool LastNameRequire

get{return lastnamerequired;
set{lastnamerequired = value;
if(lastnamerequired

System.Web.UI.WebControls.RequiredFieldValidator rfv = new RequiredFieldValidator()
rfv.ControlToValidate = "sFirstName"
rfv.Display = ValidatorDisplay.Dynamic
rfv.Text = ""
rfv.ErrorMessage = "Required"
rfv.EnableClientScript = true
rfv.ID = "rfvFirstName"
td.Controls.Add(rfv)
This code seems to only add the word "Required" to my form when rendered. It's not dynamic nor does it go away when the field is valid

Any ideas

Nov 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Are you adding these controls in the PreRender stage? Validators must be
setup prior to PreRender because they have to be able to run their own
OnPreRender method. That method outputs the necessary javascript for
validation and converts the IsValid property into the shown or hidden <span>
tag as the page is initially drawn.

--- Peter Blum
www.PeterBlum.com
Email: PL****@PeterBlum.com
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx

"D Sheldon" <an*******@discussions.microsoft.com> wrote in message
news:0C**********************************@microsof t.com...
I am creating a server control that adds web controls (i.e. textboxes, etc) to a form. I use HtmlTable to build the table and insert the controls.
Now I want to add validators to the textbox. Here is the code that I am
using:
bool lastnamerequired=false;
public bool LastNameRequired
{
get{return lastnamerequired;}
set{lastnamerequired = value;}
}

if(lastnamerequired)
{
System.Web.UI.WebControls.RequiredFieldValidator rfv = new RequiredFieldValidator(); rfv.ControlToValidate = "sFirstName";
rfv.Display = ValidatorDisplay.Dynamic;
rfv.Text = "";
rfv.ErrorMessage = "Required";
rfv.EnableClientScript = true;
rfv.ID = "rfvFirstName";
td.Controls.Add(rfv);
}

This code seems to only add the word "Required" to my form when rendered. It's not dynamic nor does it go away when the field is valid.
Any ideas?

Nov 18 '05 #2

P: n/a
That would make sense. Can you show me an example to illustrate

Thanks.
Nov 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.