<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<table>
<tr>
<td style="width: 68px">
<asp:DropDownList ID ="ddlMultiColor"
OnSelectedIndexChanged="ddlMultiColor_OnSelectedIndexChanged"
runat="server" AutoPostBack="true">
</asp:DropDownList>
</td>
<td>
<div id="msgColor" runat="server">
</div>
</td>
</tr>
</table>
</form>
</body>
</html>
using System;
using System.Web;
using System.Reflection;
using System.Drawing;
using System.Collections.Generic;
using System.Collections;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack == false)
{
populateDdlMultiColor();
colorManipulation();
}
}
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;");
}
private void populateDdlMultiColor()
{
ddlMultiColor.DataSource = finalColorList();
ddlMultiColor.DataBind();
}
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翻译
}
private ArrayList 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;
}
System.Collections.ArrayList finalColorList = new ArrayList();
foreach (string color in allColors)
{
if (Array.IndexOf(systemEnvironmentColors, color) < 0)
{
finalColorList.Add(color);
}
}
return finalColorList;
}
}
发表于 @ 2007年09月10日 18:47:00 | 评论( loading... ) | 举报| 收藏