validating a control created at runtime

D

David Foster

Hello,

I have a question regarding validation of a control that I'm creating in an
event handler (the control is not part of the static HTML page). I'm
creating a text box and adding it to a html table like this (all the
AddTextBox method does is create a new textbox and applies the appropriate
styles):

TextBox objTxtNum =
objCommonProcs.AddTextBox(objOneRow[(int)AnswerFields.QuestionId].ToString()
);

responses.Rows[RowIndex].Cells[1].Controls.Add(objTxtNum);


Now, I want to create a validator for this control on the fly:

CompareValidator ValidateTextNumeric = new CompareValidator();

ValidateTextNumeric.ControlToValidate = objTxtNum.ID;

ValidateTextNumeric.Operator = ValidationCompareOperator.DataTypeCheck;

ValidateTextNumeric.Visible = true;

ValidateTextNumeric.Type = ValidationDataType.Integer;

ValidateTextNumeric.Text = "*";

ValidateTextNumeric.ErrorMessage = "Response must be a number.";

Page.Validators.Add( ValidateTextNumeric );

responses.Rows[RowIndex].Cells[1].Controls.Add(ValidateTextNumeric);



Now, adding the validator to the page's validator's collection doesn't seem
to do anything. What I'd expect is to be able to call Page.Validate(), and
then check Page.Is_Valid to know if the page is ok or not. From what I've
read, a validator needs to be added in the Page_Init method, which is
obviously inappropriate for what I'm trying to do. Is there any way to
dynamically add a validator to the page, and have it registered for the
purpose of Page.Is_Valid, or is ther some other way to solve this problem?

Thanks,

Dave
 

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

Forum statistics

Threads
474,104
Messages
2,570,646
Members
47,248
Latest member
Angelita78

Latest Threads

Top