ASP.NET - Validating Control Inside UserControl

删除回忆录丶 提交于 2020-01-12 08:26:24

问题


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

  1. Put the validator inside the UserControl. It can access the ID of the data entry control.
  2. 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.
  3. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!