views:

52

answers:

3

Hi..

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 be dynamic..is there another way out??

A: 

The closest would be an integer property.

Darin Dimitrov
A: 

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.

Matt Dearing
A: 

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

}
Mehdi Golchin
hi.thnx for reply!The code works but when a value is selected from dropdown list GetStandardValues gets called twice with every click...whts going wrong??
anay
@anay, You are right. Whereas it merely happens on design-time, it's not important. Anyway, you can cache data to increase performance. http://social.msdn.microsoft.com/Forums/en-US/vsx/thread/4769c7b5-fd66-4490-8fa0-e1cecad80bce
Mehdi Golchin