Setup: I have a composite control that contains a label, a textbox, and an Flags enum with certain validation values like so enum ValidationTypes{ Mandatory, Date, Phone, Email }
Problem: I'd like to allow the user to enter multiple enum values at design time to allow for various forms of validation. When I use a string[] it works fine, but I cannot figure out how to setup a property of the composite control to allow for something
like ValidationTypes[]. The code, as I have it now implemented taking in a string and then converting to enum, is:
<code>
[
Bindable(true),
Category("Appearance"),
Description("The basic validation for text entered in the textbox. Separate multiple validations by using a space. Possible Values Are: Mandatory, Email, Phone, AlphaOnly, NumericOnly, and DateMMDDYYYY")
]
public string BaseValidation
{
set
{
EnsureChildControls();
string[] validations = value.Split(' ');
Validation.BaseValidation.BaseValidationType newValidation = Validation.BaseValidation.BaseValidationType.None;
foreach (string validation in validations)
{
switch (validation.Trim().ToUpper())
{
case "MANDATORY":
newValidation |= Validation.BaseValidation.BaseValidationType.Mandatory;
break;
case "EMAIL":
newValidation |= Validation.BaseValidation.BaseValidationType.Email;
break;
case "PHONE":
newValidation |= Validation.BaseValidation.BaseValidationType.PhoneNumber;
break;
case "ALPHAONLY":
newValidation |= Validation.BaseValidation.BaseValidationType.AlphaOnly;
break;
case "NUMERICONLY":
newValidation |= Validation.BaseValidation.BaseValidationType.NumericOnly;
break;
case "DATEMMDDYYYY":
newValidation |= Validation.BaseValidation.BaseValidationType.DateMMDDYYYY;
break;
}
}
basicValidation = newValidation;
}
}
</code>
Problem: Obviously, because it's not tightly bound, the user could easily enter an invalid value like "PhoneNumber" instead of "Phone". So to avoid this, I'd prefer to have this accept a List of enums or an enum[]. Is this possible?
Thank you for the quick response! The throwing of the error is a good additiong, but I'm still having the same issue with the design time portion of the problem.
To be more specific as to the design time issue:
If I change the property from string to List<BaseValidationType>, at design time, the control does not activate intellisense to show what options are available. For example, I'd like to have the tag <cc1:CustomControl id="something" runat="server" baseValidation="Mandatory
Email" /> with Mandatory and Email being suggested by intellisense and containing only the enumerated types listed in BaseValidationType.
jcheriat
Member
247 Points
67 Posts
List<Enum> Property/Attribute For Composite Control
May 31, 2012 08:27 PM|LINK
Setup: I have a composite control that contains a label, a textbox, and an Flags enum with certain validation values like so enum ValidationTypes{ Mandatory, Date, Phone, Email }
Problem: I'd like to allow the user to enter multiple enum values at design time to allow for various forms of validation. When I use a string[] it works fine, but I cannot figure out how to setup a property of the composite control to allow for something like ValidationTypes[]. The code, as I have it now implemented taking in a string and then converting to enum, is:
<code>
[
Bindable(true),
Category("Appearance"),
Description("The basic validation for text entered in the textbox. Separate multiple validations by using a space. Possible Values Are: Mandatory, Email, Phone, AlphaOnly, NumericOnly, and DateMMDDYYYY")
]
public string BaseValidation
{
set
{
EnsureChildControls();
string[] validations = value.Split(' ');
Validation.BaseValidation.BaseValidationType newValidation = Validation.BaseValidation.BaseValidationType.None;
foreach (string validation in validations)
{
switch (validation.Trim().ToUpper())
{
case "MANDATORY":
newValidation |= Validation.BaseValidation.BaseValidationType.Mandatory;
break;
case "EMAIL":
newValidation |= Validation.BaseValidation.BaseValidationType.Email;
break;
case "PHONE":
newValidation |= Validation.BaseValidation.BaseValidationType.PhoneNumber;
break;
case "ALPHAONLY":
newValidation |= Validation.BaseValidation.BaseValidationType.AlphaOnly;
break;
case "NUMERICONLY":
newValidation |= Validation.BaseValidation.BaseValidationType.NumericOnly;
break;
case "DATEMMDDYYYY":
newValidation |= Validation.BaseValidation.BaseValidationType.DateMMDDYYYY;
break;
}
}
basicValidation = newValidation;
}
}
</code>
Problem: Obviously, because it's not tightly bound, the user could easily enter an invalid value like "PhoneNumber" instead of "Phone". So to avoid this, I'd prefer to have this accept a List of enums or an enum[]. Is this possible?
Thanks in advance for any help.
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: List<Enum> Property/Attribute For Composite Control
Jun 02, 2012 01:50 AM|LINK
Hello:)
In your switch,you can add a "default"——to tell if someone inputs others,an exception will be thrown out——
switch (validation.Trim().ToUpper()) { case "MANDATORY": newValidation |= Validation.BaseValidation.BaseValidationType.Mandatory; break; case "EMAIL": newValidation |= Validation.BaseValidation.BaseValidationType.Email; break; case "PHONE": newValidation |= Validation.BaseValidation.BaseValidationType.PhoneNumber; break; case "ALPHAONLY": newValidation |= Validation.BaseValidation.BaseValidationType.AlphaOnly; break; case "NUMERICONLY": newValidation |= Validation.BaseValidation.BaseValidationType.NumericOnly; break; case "DATEMMDDYYYY": newValidation |= Validation.BaseValidation.BaseValidationType.DateMMDDYYYY; break; default: throw new Exception("Invalid Input !"); break; }If you want to define you own Array with the type of enum,you can try this:
private List<EnumType> enums = null; public List<EnumType> Enums { get { if(enums==null) { enums = new List<EnumType>(); } return enums; } }jcheriat
Member
247 Points
67 Posts
Re: List<Enum> Property/Attribute For Composite Control
Jun 06, 2012 03:57 PM|LINK
Thank you for the quick response! The throwing of the error is a good additiong, but I'm still having the same issue with the design time portion of the problem.
To be more specific as to the design time issue:
If I change the property from string to List<BaseValidationType>, at design time, the control does not activate intellisense to show what options are available. For example, I'd like to have the tag <cc1:CustomControl id="something" runat="server" baseValidation="Mandatory Email" /> with Mandatory and Email being suggested by intellisense and containing only the enumerated types listed in BaseValidationType.