开发者

Custom textbox control in asp.net

开发者 https://www.devze.com 2022-12-25 09:34 出处:网络
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 cl

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; }

}
0

精彩评论

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

关注公众号