我很难理解你到底想做什么。所以,我在猜测:
您有一个 UserControl,它根据您提供的枚举类型创建 DrowDownList。但是,你很难读回来。
我创建了一个可能对您有帮助的工作示例:
<%@ 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">
<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>
后面的代码:
Public Enum TestEnum
Value1
Value2
Value3
Value4
Value5
结束枚举
公共类 WebForm1 继承 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
结束类
用户控制:
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
结束类
<%@ Control Language="vb" AutoEventWireup="false" CodeBehind="DynamicComboFromEnum.ascx.vb" Inherits="WebApplication2.DynamicComboFromEnum" %>
<asp:DropDownList ID="MainList" runat="server"></asp:DropDownList>
我不确定发生了什么,但我在粘贴代码时遇到问题。所以,请忍受它,如果有人可以为我修好它!