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