开发者

ASP.NET - Validating Control Inside UserControl

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 follo开发者_开发技巧ws:

... 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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜