<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>
在cs文件中我们需要引用以下命名空间:
using System;
using System.Web;
using System.Reflection;
using System.Drawing;
using System.Collections.Generic;
我们先看一下Page_Load事件,在Page_Load中我们对选中的下拉列表进行处理显示
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack == false)
{
populateDdlMultiColor(); //51aspx.com
colorManipulation();
}
}
现在让我们来看一下populateDdlMultiColor() 函数
private void populateDdlMultiColor()
{
ddlMultiColor.DataSource = finalColorList();
ddlMultiColor.DataBind(); //liudao 翻译
}
finalColorList()方法
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;
}