Implementation of different colors in ASP. NET drop-down boxes

Source: Internet
Author: User

Development Background: 
Someone once asked me to develop a management tool that displays different colors based on different choices. I started to consider using the drop-down box to change the background and display color of the entries according to their names. Based on this idea, I searched the internet for a long time and did not find any related solutions, finally, I thought of a solution that is more complex than the original requirements, including databases), so I tried to find a simpler implementation solution.
 

This article mainly demonstrates how to read the system color and display the corresponding color in each entry in the drop-down box. The source code mainly displays the following content:
1. How to obtain the list enumeration of the System. Drawing. KnownColor Control
2. How to exclude the system environment color, such as "Active Border"
3. How to assign colors to each entry in the drop-down box
ASP. NET drop-down box code details:
The name drop-down box is ddlMultiColor to display the color name and color. Use the label to display the result of the Right rectangle. The Aspx code is as follows:

 
 
  1. < table>   
  2. < tr>   
  3. < td>   
  4. < asp:DropDownList ID ="ddlMultiColor"      
  5. OnSelectedIndexChanged="ddlMultiColor_OnSelectedIndexChanged"   
  6. runat="server" AutoPostBack="true">   
  7. < /asp:DropDownList>   
  8. < /td>   
  9. < td>   
  10. < div id="msgColor" runat="server">   
  11. < /div>   
  12. < /td>   
  13. < /tr>   
  14. < /table>   

ASP. NET drop-down box implementation requirements: In the cs file, we need to reference the following namespace:
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 first. In Page_Load, We will display the selected drop-down list.
 
 
  1. protected void Page_Load(object sender, EventArgs e)   
  2. {   
  3. if (Page.IsPostBack == false)   
  4. {   
  5. populateDdlMultiColor(); //51aspx.com   
  6. colorManipulation();   
  7. }   
  8. }   
Now let's take a look at the populateDdlMultiColor () function.
 
 
  1. Private void populateDdlMultiColor ()
  2. {
  3. DdlMultiColor. DataSource=FinalColorList();
  4. DdlMultiColor. DataBind (); // liudao Translation
  5. }
  6. FinalColorList () method
  7. Private List finalColorList ()
  8. {
  9. String []AllColors=Enum. GetNames (typeof (System. Drawing. KnownColor ));
  10. String []SystemEnvironmentColors=
  11. NewString [(
  12. Typeof (System. Drawing. SystemColors). GetProperties (). Length];
  13. IntIndex=0;
  14. Foreach (MemberInfo member in (
  15. Typeof (System. Drawing. SystemColors). GetProperties ())
  16. {
  17. SystemEnvironmentColors [index ++] = member. Name;
  18. }
  19. ListFinalColorList=NewList ();
  20.  
  21. Foreach (string color in allColors)
  22. {
  23. If (Array. IndexOf (systemEnvironmentColors, color)< 0)
  24. {
  25. FinalColorList. Add (color );
  26. }
  27. }
  28. Return finalColorList;
  29. }
System. Drawing. KnownColor is the color that comes with the Asp.net System. I have listed these colors through enumeration and bound them through finalColorList () correspondence. To implement this function, I used one of the most basic enumeration features: Enum. getNames () sharing method. This method detects the enumerated content and outputs the result as a string sequence. Each value in the string corresponds to each result in the enumeration.


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


Here, I use the System. Drawing. SystemColors attribute to fill in systemEnvironmentColors, and then create a list of images named finalColorList. In finalColorList, I only call known colors, but not System environment colors. Then, bind finalColorList to ddlMultiColor. So far, we have a drop-down box that includes all the color names. Let's take a look:

 
 
  1. Private void colorManipulation ()
  2. {
  3. Int row;
  4. For (Row=0; Row< DdlMultiColor. Items. Count-1; row ++)
  5. {
  6. DdlMultiColor. Items [row]. Attributes. Add ("style ",
  7. "Background-color:" + ddlMultiColor. Items [row]. Value );
  8. }
  9. DdlMultiColor. BackColor=
  10. Color. FromName (ddlMultiColor. SelectedItem. Text); // liudao Translation
  11. }

The Style attribute of the background color of each row in the drop-down box corresponds to the color name displayed in the row. In the OnSelectedIndexChanged event, the selected row in the drop-down box is highlighted using the following function and tag, and the rectangle color on the right also changes.

 
 
  1. protected void ddlMultiColor_OnSelectedIndexChanged(object sender,    
  2. EventArgs e)   
  3. {           
  4. ddlMultiColor.BackColor = Color.FromName(ddlMultiColor.SelectedItem.Text);   
  5. colorManipulation();      
  6. ddlMultiColor.Items.FindByValue(ddlMultiColor.SelectedValue).Selected =    
  7. true;       
  8. msgColor.Attributes.Add("style", "background:" +    
  9. ddlMultiColor.SelectedItem.Value + ";width:30px;height:25px;");   
  10. }   
So far, we have learned to get System. Drawing, export the System environment color, and bind the color name to the drop-down list. The ASP. NET drop-down box with different colors is implemented.
  1. Asp. Net function set and method set
  2. Interface Design Standards for ASP. NET Programming specifications
  3. Step 11 of ASP. NET Programming Model Page Lifecycle
  4. HTTP protocol analysis of ASP. NET Programming Model
  5. ASP. NET Programming Model HTTP Working Principle

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.