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:
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
Int32 myCount = 1;
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;
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
Rakesh JhaPosted Oct 5, 2010, 12:18 PM
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 NairPosted Oct 5, 2010, 12:47 PM
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;)
Gary KingPosted Oct 5, 2010, 12:27 PM
I think I was going wrong with
RequiredFieldValidator myRFV = new RequiredFieldValidator();
Suthish NairPosted Oct 5, 2010, 12:12 PM
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