2

I am facing a problem. I have set of some enum in my app. Like

public enum EnmSection
{
    Section1,
    Section2,
    Section3
}

public enum Section1
{
    TestA,
    TestB
}

public enum Section2
{
    Test1,
    Test2
}

EnmSection is main enum which contains the other enum(as string) which are declared below it. Now i have to fill the values of EnmSection in a drop-down.I have done it. Like this...

drpSectionType.DataSource = Enum.GetNames(typeof(EnmSection));
drpSectionType.DataBind();

Now my drop-down has values: Section1,Section2,Section3

Problem is:

I have another drop-down drpSubSection. Now i want to fill this drop-down whatever i have selected in the drpSectionType.

for ex If I selected Section1 in drpSectionType then drpSubsection should contain the value TestA,TestB. Like this:

protected void drpSectionType_SelectedIndexChanged(object sender, EventArgs e)
{
    string strType = drpSectionType.SelectedValue;
    drpSubsection.DataSource = Enum.GetNames(typeof());
    drpSubsection.DataBind();
}

Here typeof() is expecting the enum.But i am getting selected value as string. How can i achieve this functionality.

Thanks

4 Answers 4

3

What if you reference an assembly that contains another enum with a value named Section1?

You'll just have to try all the enums you care about, one at a time, and see which one works. You'll probably want to use Enum.TryParse.

Sign up to request clarification or add additional context in comments.

Comments

0

Something like this might work, but you have to do some exception handling:

protected void drpSectionType_SelectedIndexChanged(object sender, EventArgs e) 
{    
  string strType = drpSectionType.SelectedValue;    
  EnmSection section = (EnmSection)Enum.Parse(typeof(EnmSection), strType);
  drpSubsection.DataSource = Enum.GetNames(typeof(section));     
  drpSubsection.DataBind();

 }

1 Comment

Not working. I can't able to aceess section enum in typeof() ---> Enum.GetNames(typeof(section));
0

This might be a bit over the top but it would work if you bind bind Arrays of IEnumItem to your drop down and set it up to show their display text.

public interface IEnumBase
{
  IEnumItem[] Items { get; }
}

public interface IEnumItem : IEnumBase
{
  string DisplayText { get; }
}

public class EnumItem : IEnumItem
{
  public string DisplayText { get; set; }
  public IEnumItem[] Items { get; set; }
}

public class EnmSections : IEnumBase
{
  public IEnumItem[] Items { get; private set; }

  public EnmSections()
  {
    Items = new IEnumItem[]
    {
      new EnumItem
      {
        DisplayText = "Section1",
        Items = new IEnumItem[]
        {
          new EnumItem { DisplayText = "TestA" },
          new EnumItem { DisplayText = "TestB" }
        }
      },
      new EnumItem
      {
        DisplayText = "Section2",
        Items = new IEnumItem[]
        {
          new EnumItem { DisplayText = "Test1" },
          new EnumItem { DisplayText = "Test2" }
        }
      }
    };
  }
}

Comments

0
drpSubsection.DataSource = Enum.GetNames(Type.GetType("Your.Namespace." + strType));

If the enums are in another assembly, (i.e. they're not in mscorlib or the current assembly) you'll need to provide the AssemblyQualifiedName. The easiest way to get this will be to look at typeof(Section1).AssemblyQualifiedName, then modify your code to include all the necessary parts. The code will look something like this when you're done:

drpSubsection.DataSource = Enum.GetNames(Type.GetType("Your.Namespace." + strType + ", MyAssembly, Version=1.3.0.0, Culture=neutral, PublicKeyToken=b17a5c561934e089"));

3 Comments

Not working. It gives me error:- Value cannot be null. Parameter name: enumType
That means that Type.GetType isn't finding the type. Are you sure you've got the namespace of the enum in there correctly?
If the types are in another assembly, you'll need to provide the assembly-qualified name, instead of just what I've shown there. I'll edit my answer to help with that.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.