Reputation: 35
I have a form that needs dynamic input boxes that have to be integers. I used a slightly modified version of the code found here to do that: http://www.learning2code.net/Learn/2009/8/12/Adding-Controls-to-an-ASPNET-form-Dynamically.aspx
I have a the following code to add to the placeholder:
CompareValidator cmpVal = new CompareValidator();
cmpVal.ID = "cv" + textboxID;
cmpVal.ControlToValidate = textboxID;
DynamicTextBoxIntegerValidation.Controls.Add(cmpVal);
Obviously this is missing two very important pieces; the Type and Operator fields. The problem is I can't figure out how to add them. Any help would be appreciated.
Upvotes: 1
Views: 871
Reputation: 6674
Type
and Operator
are simply properties of CompareValidator
. You can add them as follows:
CompareValidator cmpVal = new CompareValidator();
cmpVal.ID = "cv" + textboxID;
cmpVal.ControlToValidate = textboxID;
cmpVal.Type = ValidationDataType.Integer; //Set your type and operator here.
cmpVal.Operator = ValidationCompareOperator.Equal;
DynamicTextBoxIntegerValidation.Controls.Add(cmpVal);
Upvotes: 1