I'm trying to create a custom textbox with a enum kind property in it(like textmode). The enum values will come from database. But enums cant b开发者_C百科e dynamic..is there another way out??
The closest would be an integer property.
Enums are compile-time constants. If the database values won't change at runtime, then you could always use a codegen tool to generate the enum values from the database (at pre-compile time). If they will change, you may need to just do a String Property or something similar, instead of the Enum.
You have to write a custom TypeConverter
to accomplish this duty.
public class MyItemsConverter : TypeConverter
{
public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
{
StringCollection values = new StringCollection();
// Connect to database and read values.
return new StandardValuesCollection(values);
}
public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
{
return (context != null);
}
public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
{
return true;
}
}
public class MyControl : WebControl
{
[TypeConverter(typeof(MyItemsConverter))]
public string MyItem { get; set; }
}
精彩评论