问题
I have a wrapper UserControl control around a DropDownList for managing language specific translation on the DropDownList values.
I also have a required field validator that is requried for the inner dropdownlist.
How can I expose this control via the usercontrol to allow validation?
The error I am currently getting is as follows:
... cannot be validated. at System.Web.UI.WebControls.BaseValidator.CheckControlValidationProperty(String name, String propertyName)
at System.Web.UI.WebControls.BaseValidator.ControlPropertiesValid()
Edit: I'm now using...
[ValidationPropertyAttribute("SelectedValue")]
.... With
public string SelectedValue
{
get { return cboI18nItems.SelectedValue; }
}
Which is now working if I check the IsValid property of the page on postback.
回答1:
Your Validator should be inside of your UserControl but accessible from the page.
Or set ValidationProperty on the UserControl
- Put the validator inside the UserControl. It can access the ID of the data entry control.
- Use a CustomValidator. Do not use its ControlToValidate property. Instead, within your own evaluation function you will access the DropDownList through the UserControl. You probably will make the DropDownList field Public so it can be seen once you typecast the UserControl object to the class of that UserControl.
- Its possible to use the ValidationPropertyAttribute. You need to add a property to your usercontrol that returns a string value of the data.
Found here.
回答2:
I handled this issue a bit differently than what was suggested above. Here is a code snippet taken from my code:
Protected Sub ValidateCreditCard()
Dim validators = Page.GetValidators("cc")
For Each v In validators
DirectCast(v, BaseValidator).Validate()
Next
End Sub
VB.NET Code but should be easy to figure out.
来源:https://stackoverflow.com/questions/4106868/asp-net-validating-control-inside-usercontrol