In the development process, some state representation is used to the enumeration type, so how to bind the enum type directly to ListControl (DropDownList) is the subject of this time, the nonsense is not much to say, the direct code:
First look at the tool class code:
/// <summary> ///binding to a generic class for a ListControl control by an enumeration type///usage: Pass directly to the Control:enummanager to bind< enumerations >. Bind_enum_control (ListControl); /// </summary> /// <typeparam name= "Tenum" ></typeparam> Public Static classEnummanager<tenum>whereTenum:struct, system.iconvertible {Private Static ReadOnlysystem.collections.generic.dictionary<string, system.collections.generic.dictionary<int,string>> enumdictionary =Newsystem.collections.generic.dictionary<string, system.collections.generic.dictionary<int,string>>(); /// <summary> ///Get Enumeration/// </summary> /// <returns></returns> Public Staticsystem.collections.generic.dictionary<int,string>getenumdictionary () {if(Enumdictionary.containskey (typeof(Tenum). FullName))returnenumdictionary[typeof(Tenum). FullName]; varEnumdict =add_enum_to_diconary (); returnenumdict; } /// <summary> ///incoming controls to bind/// </summary> /// <param name= "Control" ></param> Public Static voidBind_enum_control (System.Web.UI.WebControls.ListControl control) {control. DataSource=getenumdictionary (); Control. DataTextField="value"; Control. DataValueField="Key"; Control. DataBind (); } /// <summary> ///get the value of an enum by key/// </summary> /// <param name= "key" ></param> /// <returns></returns> Public Static stringGetEnumValue (intkey) { if(Enumdictionary.containskey (typeof(Tenum). FullName))returnenumdictionary[typeof(Tenum). Fullname][key]; varEnumdic =add_enum_to_diconary (); returnEnumdic[key]; } Private Staticsystem.collections.generic.dictionary<int,string>add_enum_to_diconary () {System.Type T=typeof(Tenum); string[] _names =System.Enum.GetNames (t); int[] _values = System.Enum.GetValues (t) as int[]; System.Collections.Generic.Dictionary<int,string> enumdict =Newsystem.collections.generic.dictionary<int,string>(); for(inti =0; I < _values. Length; i++) {Enumdict.add (_values[i], _names[i]); } enumdictionary.add (T.fullname, enumdict); returnenumdict; } }
Use the tool class to create a new WebForm1.aspx page:
<%@ Page language="C #"autoeventwireup="true"Codebehind="WebForm1.aspx.cs"inherits="Test_tofixed.webform1"%><! DOCTYPE HTML Public"-//W3C//DTD XHTML 1.0 transitional//en" "HTTP://WWW.W3.ORG/TR/XHTML1/DTD/XHTML1-TRANSITIONAL.DTD">"http://www.w3.org/1999/xhtml">"Server"> <title></title>"Form1"runat="Server"> <div>The test binds the enumeration type data to a drop-down list:<asp:dropdownlist runat="Server"Id="DDL"/> </div> </form></body>usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;namespacetest_tofixed{ Public enumStatus {Audit Failed=0, not audited=1, in the audit=2, audited=3 } Public Partial classWebForm1:System.Web.UI.Page {protected voidPage_Load (Objectsender, EventArgs e) { if(!IsPostBack) {Common.enummanager<status>. Bind_enum_control ( This. DDL);//binding An enumeration type to a DropDownList control This. DDL. Items.insert (0,NewListItem ("Please select","-1")); } } }}
Effect:
ASP. NET binds an enum type (enum) to a ListControl (DropDownList) control