ASP. NET multi-color drop-down box development instance, asp.net drop-down box instance

Source: Internet
Author: User

ASP. NET multi-color drop-down box development instance, asp.net drop-down box instance

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

Code details:

The name drop-down box is ddlMultiColor to display the color name and color. Use the <div> label to display the result of the Right rectangle. 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 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.

 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 (); //} 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 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:

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 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 <div> label combined with the following function, and the rectangle color on the right also changes.

 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;");  }

So far, we have learned to get System. Drawing, export the System environment color, and bind the color name to the drop-down list.

Related Article

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.