1. 编译MyDrop.cs为DLL,csc /t:library MyDrop.cs
2. 将DLL栲到BIN下
3. web.config 中指定对MyDrop.dll的引用
<system.web>
<compilation debug="true">
<assemblies>
<add assembly="*"/>
</assemblies>
</compilation>
</system.web>
4. .aspx文件中使用控件
<%@ Register TagPrefix="zxh" Namespace="Test2005" Assembly="MyDrop" %>
<!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">
<div>
<zxh:MyDrop id="drp" runat="server" width="100"></xhwc:MyDrop>
</div>
</form>
</body>
</html>
5. .aspx.cs中加入数据
protected void Page_Load(object sender, EventArgs e)
{
ListItem item = new ListItem(" 萧萧", "bb");
item.Attributes.Add("style", "color:#ff3939");
item.Attributes.Add("onClick", "alert('你好')");
drp.Items.Add(item);
item = new ListItem(" 欢欢", "cj");
item.Attributes.Add("style", "color:#ff39ee");
item.Attributes.Add("onclick", "alert('我好')");
drp.Items.Add(item);
}
6. MyDrop.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.ComponentModel;
using System.Collections;
using System.Drawing;
namespace Test2005
{
/// <summary>
/// MyDrop 的摘要说明
/// </summary>
public class MyDrop : DropDownList
{
protected override void RenderContents(HtmlTextWriter writer)
{
ListItemCollection listItemCollection = base.Items;
int i = base.Items.Count;
bool flag = false;
if (i > 0)
{
for (int j = 0; j < i; j++)
{
ListItem listItem = listItemCollection[j];
writer.WriteBeginTag("option");
if (listItem.Selected)
{
if (flag)
{
throw new HttpException("Cant_Multiselect_In_DropDownList");
}
flag = true;
writer.WriteAttribute("selected", "selected", false);
}
writer.WriteAttribute("value", listItem.Value, true);
System.Web.UI.AttributeCollection attributeCollection = listItem.Attributes;
IEnumerator iEnumerator = attributeCollection.Keys.GetEnumerator();
while (iEnumerator.MoveNext())
{
string str2 = (String)iEnumerator.Current;
writer.Write(" " + str2 + "=\"" + attributeCollection[str2] + "\"");
}
writer.Write('>');
HttpUtility.HtmlEncode(listItem.Text.Replace(" ", ((char)58853).ToString()), writer);
writer.WriteEndTag("option");
writer.WriteLine();
}
}
}
protected override object SaveViewState()
{
object[] objs = new object[2];
objs[0] = base.SaveViewState();
System.Collections.ArrayList list = new ArrayList();
objs[1] = list;
foreach (ListItem item in this.Items)
{
System.Collections.Hashtable hash = new Hashtable();
foreach (Object key in item.Attributes.Keys)
{
hash.Add(key, item.Attributes[key.ToString()]);
}
list.Add(hash);
}
return objs;
}
protected override void LoadViewState(object savedState)
{
object[] objs = (Object[])savedState;
base.LoadViewState(objs[0]);
System.Collections.ArrayList list = (System.Collections.ArrayList)objs[1];
for (int i = 0; i < list.Count; i++)
{
System.Collections.Hashtable hash = (System.Collections.Hashtable)list[i];
foreach (object key in hash.Keys)
{
Items[i].Attributes.Add(key.ToString(), hash[key].ToString());
}
}
}
}
}
参考:http://hackate.cnblogs.com/archive/2005/09/09/233166.html