views:

31

answers:

1

Hello everybody
I have code that gives a list of all possible values for any given enum
i bound it pretty often to dropdownlists in my webpages
now im trying to make a usercontrol which accepts the type name as a parameter, which in turn calls the code to create the value list
as my sub expects a type parameter

Shared Function EnumList(ByVal EnumType As Type) As List(Of ListItem)
        Dim ret As New List(Of ListItem)
        Dim consts = [Enum].GetValues(EnumType)
            For Each c In consts
                ret.Add(New ListItem With {.Text = c.ToString, .Value = c.ToString})
            Next
        Return ret
    End Function

im trying to turn the string used in the usercontrols declration into a type. the problem is i can only do it with system types (even non mscorlib, a bit clumsily). but for enums declared in my app_code, i cannot figure out the way to do it
the aqn makes some string with a funny code like this (AstroDate is the name of my class):
"AstroDate, App_Code.rujpwg3d, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"
but if i use it in the gettype, it errors

please advise

Edit here is the code in the usercontrol trying to get a list of the Enum

    Sub RefillData()
    Dim TempValue = Value
    MainList.Items.Clear()
    MainList.DataSource = EnumList(Type.GetType(EnumType, True, True))
    If EmptyText <> "" Then
        Dim itm As New ListItem(EmptyText, "")
        MainList.Items.Add(itm)
    End If
    MainList.DataBind()
    Value = TempValue
End Sub

"EnumType" is a string property passed in the declartaion of the usercontrol in the page.

A: 

Hi,

I had difficulties understanding what you exactly wanted to do. So, I am taking a guess :

You have one UserControl which creates a DrowDownList based on the Enum Type you provide. But, you had difficulties reading it back.

I created a working sample which might be helpful to you :

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="WebApplication2.WebForm1" %>

<%@ Register src="DynamicComboFromEnum.ascx" tagname="DynamicComboFromEnum" tagprefix="uc1" %>

<html xmlns="http://www.w3.org/1999/xhtml"&gt;

<head runat="server">
<title></title>

</head>

<body>
<form id="form1" runat="server">
<div>    
    <uc1:DynamicComboFromEnum ID="DynamicComboFromEnum1" runat="server" />
    <asp:Button ID="Button1" runat="server" Text="Button" />
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>    
</div>
</form>

</body>

</html>

Code behind :

Public Enum TestEnum
Value1
Value2
Value3
Value4
Value5

End Enum

Public Class WebForm1 Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If (Not Page.IsPostBack) Then
        DynamicComboFromEnum1.EnumType = GetType(TestEnum)
    End If
End Sub

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
    Label1.Text = DynamicComboFromEnum1.GetSelectedValue().ToString()
End Sub

End Class

User Control :

Public Class DynamicComboFromEnum
Inherits System.Web.UI.UserControl

Public Property EnumType() As Type
    Get
        Return ViewState("EnumType")
    End Get
    Set(ByVal value As Type)
        ViewState("EnumType") = value
    End Set
End Property

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If (Not Page.IsPostBack) Then
        RefillData()
    End If
End Sub

Sub RefillData()
    MainList.Items.Clear()
    MainList.DataSource = EnumList(EnumType)
    MainList.DataBind()
End Sub

Private Function EnumList(ByVal type As Type) As Object
    Dim Names As String() = [Enum].GetNames(type)
    Return Names
End Function

Public Function GetSelectedValue() As Object
    Return [Enum].Parse(EnumType, MainList.SelectedValue)
End Function

End Class

<%@ Control Language="vb" AutoEventWireup="false" CodeBehind="DynamicComboFromEnum.ascx.vb" Inherits="WebApplication2.DynamicComboFromEnum" %>

<asp:DropDownList ID="MainList" runat="server"></asp:DropDownList>

I am not sure what is going on but I am having problems pasting the code. So, please bare with it and if someone could fix it up for me!

decyclone
thanks decyclone. u REALLY took time to this. now it will be a bit easier to explain. ;-) imagine we have 2 or 10 or more different enums in our system. can u please declare in <uc1:DynamicComboFromEnum> exactly which enum we want listed in the dropdown? thats what im trying to accomplish. thanks a million!!!!
Yisman
So, you mean you want to specify enum type from the markup (in aspx file rather than code behind file), right?
decyclone
correct. thanks.
Yisman