Answered by:
Required field validator in a user control

Question
-
User1897897189 posted
Dear all,
I have a drop down list user control which is used to display names of employees.
How can i apply required field validator?
Thanks
Nick
Sunday, March 8, 2015 10:20 AM
Answers
-
User1644755831 posted
Hello nicklibee,
You need to apply ValidationProperty attribute to your user control, its value pointing to the name of the property which will be validated by the validation control. This way validation control at server-side knows which property to look for when validating.
To allow a Custom User Control to be validated the first thing which is required is the addition of
[ValidationPropertyAttribute("value")].
This specifies the property which supplies the string to be validated, where "value" is the name of the property.
https://msdn.microsoft.com/en-us/library/system.web.ui.validationpropertyattribute(v=vs.110).aspx
Secondly the ControlToValidate="" property of validation control on the page should be the ID of the user control, colon(:), then the ID of the control to which 'value' is associated. Something like below
ControlToValidate="cboTask:DropDownList1"
Hope this helps.
With Regards,
Krunal Parekh
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, March 10, 2015 3:05 AM
All replies
-
User475983607 posted
Set the InitialValue of the required field validator.
Please see the following MSDN documentation. Remember, MSDN is your friend!
Sunday, March 8, 2015 10:28 AM -
User1897897189 posted
yes i tried , it says control cannot be validated....i put the validation in the user control
Sunday, March 8, 2015 10:35 AM -
User1644755831 posted
Hello nicklibee,
You need to apply ValidationProperty attribute to your user control, its value pointing to the name of the property which will be validated by the validation control. This way validation control at server-side knows which property to look for when validating.
To allow a Custom User Control to be validated the first thing which is required is the addition of
[ValidationPropertyAttribute("value")].
This specifies the property which supplies the string to be validated, where "value" is the name of the property.
https://msdn.microsoft.com/en-us/library/system.web.ui.validationpropertyattribute(v=vs.110).aspx
Secondly the ControlToValidate="" property of validation control on the page should be the ID of the user control, colon(:), then the ID of the control to which 'value' is associated. Something like below
ControlToValidate="cboTask:DropDownList1"
Hope this helps.
With Regards,
Krunal Parekh
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, March 10, 2015 3:05 AM