Asp. NET Colorful dropdown Box Development Example _ practical skills

Source: Internet
Author: User

This article is mainly to demonstrate how to read the system color and display the corresponding color in each entry in the Drop-down box, which mainly displays the following content:

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

2, how to eliminate the system environment color, such as "Active Border"

3. How to assign colors to each entry in the dropdown box

Code Detailed:

Named dropdown box for Ddlmulticolor to display the color name and color, with the <div> label to display the right rectangle results, the ASPX code is as follows

 <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 take a look at the Page_Load event, and in Page_Load we'll show you how to handle the selected Drop-down list

 protected void Page_Load (object sender, EventArgs e) 
 { 
 if (Page.IsPostBack = = False) 
 { 
 Populateddlmulticolor (); 51aspx.com 
 colormanipulation (); 
 } 
 

Now let's take a look at the Populateddlmulticolor () function

 private void Populateddlmulticolor () 
 { 
 Ddlmulticolor.datasource = Finalcolorlist (); 
 Ddlmulticolor.databind (); 
 the

 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 is the asp.net system itself, and I've listed these colors by enumerating them and binding them through Finalcolorlist () correspondence. To implement this feature, I used one of the most basic enumeration features: the Enum.getnames () shared method, which detects the enumeration content and outputs the result to a string sequence in which each value corresponds to each result in the enumeration.

However, there are still some problems with this approach. According to the above idea, the enumeration amount will include the system environment color, such as "Active Border (note: active border)", in order to solve this problem, I extended the system environment color. I used the System.Reflection.MemberInfo class.

Here I populate the systemenvironmentcolors with the System.Drawing.SystemColors attribute, and then create a list of graphs called finalcolorlist, in Finalcolorlist I only call a known color, but not in the system ambient color. Then bind the finalcolorlist to the Ddlmulticolor. Now that we have a drop-down box that includes all the color names, let's take a look at the following:

 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 
 }

Each row of the background color in the Drop-down box has the style property that corresponds to the color name that the row displays. The line selected in the OnSelectedIndexChanged event Drop-down box is highlighted with the following function combination <div> label, and the color of the rectangle on the right is also changed.

 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 to get system.drawing and exhaust the system environment color, and bind the color name to the Drop-down list.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.