Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 835
cweston Avatar asked Nov 05 '10 14:11

cweston


1 Answers

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.

like image 65
Tim Schmelter Avatar answered Sep 28 '22 04:09

Tim Schmelter