问个苯苯的问题哈!关于自定义控件的!谢谢!Please Come In!

hackate 2005-08-09 10:15:32
最近看到QQ论坛上有个可以变色的DropDownList的下拉框,他里面的项的字体是可以变色的!代码如下:
<select id="ddl">
<option style="COLOR:#ff3f6f">你好</option>
</select>
但是默认的DropDownList是没这个功能的!所以只有自己做个自定义控件了!

昨天用Reflector看了看!发现DropDownList是继承至ListControl的,可是ListControl他的items集合是来自ListItemCollection类,可是继承不了ListItemCollection
这样无法重写里面的add方法,等等,这样我想在加的时候可以这样
ddl.items.add("nihao","value","color")
这种形式的!

请问我应该怎么做嫩,能给我个思路。或者给个相应的例子,有代码也可以,非常非常感谢,谢谢!
...全文
555 38 打赏 收藏 转发到动态 举报
写回复
用AI写文章
38 条回复
切换为时间正序
请发表友善的回复…
发表回复
CnEve 2005-10-12
  • 打赏
  • 举报
回复
学到不少,留个记号。
hackate 2005-09-09
  • 打赏
  • 举报
回复
后来发现控件在PostBack后不能保持状态!
请教Haneryfan1的答案如下:
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, 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());
}
}

}



}
chang110cn 2005-08-10
  • 打赏
  • 举报
回复
好多星星,眼都花了.....
还得多努力呀.
hackate 2005-08-10
  • 打赏
  • 举报
回复
楼上这个方法,也挺不错的,谢谢!!

本来想结帖了,不过再挂到晚上.就结帖!

先再加些分上帖子再说....
henryfan1 2005-08-10
  • 打赏
  • 举报
回复
ddl.items.add(new listitem("文本","值","颜色等"))
ListItem不允许派生,所以只能用别的方法,可以创建一个ListItem工厂.

public class ListItemFactory
{
private ListItemFactory()
{
}
private ListItemFactory mSingleObj =null;
public ListItemFactory SingleObj
{
get
{
if(mSingleObj == null)
mSingleObj = new ListItemFactory();
return mSingleObj;
}
}
public System.Web.UI.WebControls.ListItem Create(string name)
{
return new System.Web.UI.WebControls.ListItem(name);
}
public System.Web.UI.WebControls.ListItem Create(string name,string value)
{
return new System.Web.UI.WebControls.ListItem(name,value);
}
public System.Web.UI.WebControls.ListItem Create(string name,string value,string color)
{
System.Web.UI.WebControls.ListItem item = Create(name,value);
item.Attributes.Add("style","color:"+color +";");
return item;
}
}
bugsbuddy 2005-08-09
  • 打赏
  • 举报
回复
继承DropDownList,新增一个string[] ColorArray的属性,
重写DropDownList控件的Render方法,如下:
System.Text.StringBuilder sbHtml = new System.Text.StringBuilder(32);
System.IO.StringWriter tw = new System.IO.StringWriter(sbHtml);
System.Web.UI.HtmlTextWriter htw = new HtmlTextWriter(tw);
base.RenderControl(htw);
int nIdx = 0;
int i = 0;
while (nIdx = sbHtml.ToString().IndexOf("<option",nIdx) > -1)
 {
nIdx += 6;
sbHtml = sbHtml.Insert(nIdx," style=\"COLOR:"+ ColorArray[i] +"\"");
 }
writer.Write(sbHtml.ToString());
hackate 2005-08-09
  • 打赏
  • 举报
回复
现在SP1234大哥,给出一个方法,是用ArrayList数组解决的方式,,十分感谢
解决了问题了!不过想在这里看看大家还有什么好方法,也来这里发表一下,谢谢!
TechEye 2005-08-09
  • 打赏
  • 举报
回复
其实自己完全写一个也不难啊,用ArrayList存Option..
henryfan1 2005-08-09
  • 打赏
  • 举报
回复
ListItem实现了 IAttributeAccessor接口,应该可以实现这样的功能.
ListItem.Attributes.Add("style","COLOR:#ff3f6f")
试下
lhcoolhacker 2005-08-09
  • 打赏
  • 举报
回复
重写DropDownList控件的Render事件。
在Render事件中,改变其输出的option
hackate 2005-08-09
  • 打赏
  • 举报
回复
谢谢楼上的支持!
peterHunter 2005-08-09
  • 打赏
  • 举报
回复
呵呵,只能关注
daishengs 2005-08-09
  • 打赏
  • 举报
回复
楼主好谦虚哦,可惜我解决不了
lovebanyi 2005-08-09
  • 打赏
  • 举报
回复
#ddl option
{color:#CC6633;
}
</style>

倒。有了车轮你还造车轮啊。 用个CSS控制就OK了
hedonister 2005-08-09
  • 打赏
  • 举报
回复
潜水好久,冒个头喘气学习
adandelion 2005-08-09
  • 打赏
  • 举报
回复
楼主好谦虚!向搂主学习!!!!
偶菜,帮顶~~~~
saucer 2005-08-09
  • 打赏
  • 举报
回复
that was a bug, see

http://support.microsoft.com/default.aspx?scid=kb;en-us;Q309338
hackate 2005-08-09
  • 打赏
  • 举报
回复
没关系!呵呵,原来我在测试楼上这个方法出错了,怪不得!

现在两种解决方式了,期待新的方法,和更简洁的方法,恩..
henryfan1 2005-08-09
  • 打赏
  • 举报
回复
不好意思公司的网有站,补答.
这个是通过测试的了
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, writer);
writer.WriteEndTag("option");
writer.WriteLine();
}
}
}
}
soft_biao 2005-08-09
  • 打赏
  • 举报
回复
mark
加载更多回复(18)

62,047

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧