I want to be able to create RequiredFieldValidators in code behind. The controls (to be validated) are created in the code behind which reads the control details from a database - so there could be any number of controls on the page.
So, starting with basics, let's assume that I have 1 DropDownList (myDDL1) on my page. The code would be as follows:
Code
RequiredFieldValidator myRFV = new RequiredFieldValidator();
myRFV.ControlToValidate = "myDDL1";
myRFV.ErrorMessage = "you must select a value!";
form1.Controls.Add(myRFV);
When there is more than 1 control (eg myDDL1 and myDDL2), then I need to create a different RequiredFieldValidator for each control to be validated. The following code doesn't work - it is just to give a simplified example of what I am trying to achieve. The problem is that I need to be able to dynamically name the requiredFieldValidator - myRFV
Code
Int32 myCount = 1;
Code
RequiredFieldValidator myRFV + myCount = new RequiredFieldValidator(); // myRFV + myCount does not work!!!
myRFV + myCount.ControlToValidate = "myDDL1";
myRFV + myCount.ErrorMessage = "you must select a value!";
form1.Controls.Add(myRFV + myCount);
myCount = 2;
Code
RequiredFieldValidator myRFV + myCount = new RequiredFieldValidator();
myRFV + myCount.ControlToValidate = "myDDL2";
myRFV + myCount.ErrorMessage = "you must select a value!";
form1.Controls.Add(myRFV + myCount);
So, first the RFV control name would be myRFV1 and the second RFV control would be myRFV2. But C# wont let me create the RFV instances in this way. If I use the same RFV name (myRFV), and only change the .ControlToValidate, then only the second drop down list will get validated.
Thanks
Gary King
4 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Rakesh Jha85 reputationPosted Oct 5, 2010, 8:18 AM EDT
you can use following code, but please specify distinct ID.
RequiredFieldValidator ObjMyRFV;
ObjMyRFV = new RequiredFieldValidator();
ObjMyRFV.ID = "myRFV1";
ObjMyRFV.ControlToValidate = "myDDL1";
ObjMyRFV.ErrorMessage = "you must select a value!";
form1.Controls.Add(ObjMyRFV);
ObjMyRFV = new RequiredFieldValidator();
ObjMyRFV.ID = "myRFV2";
ObjMyRFV.ControlToValidate = "myDDL2";
ObjMyRFV.ErrorMessage = "you must select a value!";
form1.Controls.Add(ObjMyRFV);
hope it will surely help you.
Suthish NairRank #92 · 22.8K reputationPosted Oct 5, 2010, 8:12 AM EDT
I think instead of using RequiredFieldValidator, you can try with CustomValidator.
So you can do one to many controls validations using CustomValidator server event ServerValidate.
Presently i dont have any sample. Hope someone will post here.
later you can refer:
http://www.dotnetspider.com/forum/158038-Custom-validation-multiple-controls-c-example.aspx
http://www.codeproject.com/KB/validation/MultiDependValidator.aspx
Gary King77 reputationPosted Oct 5, 2010, 8:27 AM EDT
I think I was going wrong with
RequiredFieldValidator myRFV = new RequiredFieldValidator();
Suthish NairRank #92 · 22.8K reputationPosted Oct 5, 2010, 8:47 AM EDT
You post says..
"the code behind which reads the control details from a database - so there could be any number of controls on the page."
So you will hardcord every controls like this..? why dont you try like this to add all controls
(myRFV.ID = "myRFV_" + myCount;)