ASP. NET Colorful Drop down Box Development Example

  • 2021-07-01 07:16:52
  • OfStack

This article mainly demonstrates how to read the system color and display the corresponding color in each item in the drop-down box. The source code mainly shows the following contents:

1. How to get the list enumeration of System. Drawing. KnownColor color controls

2. How to exclude system environment colors, such as "Active Border"

3. How to assign colors to each entry in the drop-down box

Detailed code explanation:

Name the drop-down box ddlMultiColor to display the color name and color, and use < div > The label displays the rectangular result on the right side, and the Aspx code is


 <table> 
 <tr> 
 <td> 
 <asp:DropDownList ID ="ddlMultiColor" 
 OnSelectedIndexChanged="ddlMultiColor_OnSelectedIndexChanged" 
 runat="server" AutoPostBack="true"> 
 </asp:DropDownList> 
 </td> 
 <td> 
 <div id="msgColor" runat="server"> 
 </div> 
 </td> 
 </tr> 
 </table>

In the cs file, we need to reference the following namespaces:


 using System; 
 using System.Web; 
 using System.Reflection; 
 using System.Drawing; 
 using System.Collections.Generic;

Let's first look at the Page_Load event under 1. In Page_Load, we process and display the selected drop-down list


 protected void Page_Load(object sender, EventArgs e) 
 { 
 if (Page.IsPostBack == false) 
 { 
 populateDdlMultiColor(); //51aspx.com 
 colorManipulation(); 
 } 
 }

Now let's look at the populateDdlMultiColor () function under 1


 private void populateDdlMultiColor() 
 { 
 ddlMultiColor.DataSource = finalColorList(); 
 ddlMultiColor.DataBind(); // 
 }

 finalColorList() Method 

 private List finalColorList() 
 { 
 string[] allColors = Enum.GetNames(typeof(System.Drawing.KnownColor)); 
 string[] systemEnvironmentColors = 
 new string[( 
 typeof(System.Drawing.SystemColors)).GetProperties().Length]; 
 
 int index = 0; 
 
 foreach (MemberInfo member in ( 
 typeof(System.Drawing.SystemColors)).GetProperties()) 
 { 
 systemEnvironmentColors[index ++] = member.Name; 
 } 
 List finalColorList = new List(); 
 foreach (string color in allColors) 
 { 
 if (Array.IndexOf(systemEnvironmentColors, color) < 0) 
 { 
 finalColorList.Add(color); 
 } 
 } 
 return finalColorList; 
 }

System. Drawing. KnownColor are the Asp. net system's own colors, which I have enumerated and bound through finalColorList () correspondence. To do this, I use one of the most basic enumeration features, the 1: Enum. GetNames () shared method, which detects the contents of the enumeration and outputs a sequence of strings, each value of which corresponds to each result in the enumeration.

However, there are still some problems with this method. According to the above idea, the system environment color, such as "Active Border", will be included in the enumeration amount. To solve this problem, I expanded the system environment color. I used the System. Reflection. MemberInfo class.

Here I populate systemEnvironmentColors with the System. Drawing. SystemColors attribute, and then create a list of graphics named finalColorList, where I only call the known colors, but not in the system environment colors. Then bind finalColorList into ddlMultiColor. At this point, we have a drop-down box with all the color names. Let's do 1:


 private void colorManipulation() 
 { 
 int row; 
 for (row = 0; row < ddlMultiColor.Items.Count - 1; row++) 
 { 
 ddlMultiColor.Items[row].Attributes.Add("style", 
 "background-color:" + ddlMultiColor.Items[row].Value); 
 } 
 ddlMultiColor.BackColor = 
 Color.FromName(ddlMultiColor.SelectedItem.Text);//liudao Translation  
 }

The Style] attribute of the background color for every 1 row in the drop-down box corresponds to the color name displayed for that row. The rows selected in the drop-down box in the OnSelectedIndexChanged event are combined by the following function < div > The label is highlighted, and the color of the rectangle on the right side changes accordingly.


 protected void ddlMultiColor_OnSelectedIndexChanged(object sender, 
 EventArgs e) 
 { 
 ddlMultiColor.BackColor = Color.FromName(ddlMultiColor.SelectedItem.Text); 
 colorManipulation(); 
 ddlMultiColor.Items.FindByValue(ddlMultiColor.SelectedValue).Selected = 
 true; 
 msgColor.Attributes.Add("style", "background:" + 
 ddlMultiColor.SelectedItem.Value + ";width:30px;height:25px;"); 
 }

At this point, we learned how to get System. Drawing and discharge the system environment color, and bind the color name to the drop-down list.


Related articles: