When generating webcontrols on the fly, keep in mind…

Hi,

I have been working on a code snippet that was rendering ASP.NET server side webcontrols such as TextBox, DropDownList, Validators, … etc . The problem was there was a bug that prevented regularExpression validators not to be rendered even they have been added to the page at runtime.
I was added two kinds of webserver controls, one was requiredFieldValidator control, and the other was RegularExpressionValidator control. These two kinds of validators were added in the codebehind, but i could only see the requiredFieldValidator controls on the rendered page.
I saw that requiredFieldValidators were added to a TableCell just after their creation. I thought that if that cell was started to be rendered any other element may not be seen on the cell. Thus after adding requiredFieldValidators i was adding RegularExpressionValidators, as i have added to tableCell’s controls collection.
The problem was solved by creating an array of controls namely “List ValiadtorList” and i have added each validator control to the list. After filling the list, i have added all the validator controls to a TableCell in a foreach loop that was working on the Validator List controlList.
Just to cut the long story short, see the code segment below:
BEFORE: The problem was requiredFieldValidators were rendered, but regExValidator weren’t
——————————————————————————————-
RequiredFieldValidator rfv = new RequiredFieldValidator();
rfv.ControlToValidate = cont.Value.ID;
rfv.ErrorMessage =”REQUIRED”;
rfv.Display = ValidatorDisplay.Dynamic;
TableCell cellValidator=new TableCell();
cellValidator.Controls.Add(rfv);
RegularExpressionValidator regExValidator = new RegularExpressionValidator();
regExValidator.ControlToValidate = cont.Value.ID;
regExValidator.ValidationExpression = inheritedTypeDataValidatorRegEx;
regExValidator.ErrorMessage =”INVALID INPUT”;
regExValidator.Display = ValidatorDisplay.Dynamic;
cellValidator.Controls.Add(regExValidator);
——————————————————————————————-
AFTER: By collectIng all the valIdators In a LIst, and addIngthem to the TableCell dIrectly, bulk add to the tableCell, solved the problem.
——————————————————————————————-
List ValidatorsList = new List();
RequiredFieldValidator rfv = new RequiredFieldValidator();
rfv.ControlToValidate = cont.Value.ID;
rfv.ErrorMessage =”reUIRED”;
rfv.Display = ValidatorDisplay.Dynamic;
TableCell cellValidator=new TableCell();
// cellValidator.Controls.Add(rfv);
ValıdatorLıst .Add(rfv);
RegularExpressionValidator regExValidator = new RegularExpressionValidator();
regExValidator.ControlToValidate = cont.Value.ID;
regExValidator.ValidationExpression = inheritedTypeDataValidatorRegEx;
regExValidator.ErrorMessage =”INVALID INPUT”;
regExValidator.Display = ValidatorDisplay.Dynamic;
// cellValidator.Controls.Add(regExValidator);
ValıdatorLıst .Add(regExValıdator);
foreach(Control c in ValidatorList){
cellValidator.Add(c);
}
——————————————————————————————-
Also have a look at the link below: