可以设置选项背景颜色的DropDownList
来源:互联网 发布:开淘宝店要交保证金 编辑:程序博客网 时间:2024/04/29 23:03
前段时间CSDN上的一个朋友问到DropDownList添加ListItem的时候在Attributes中添加了Style项,但实际浏览的时候确没有这个效果。后来我反编译了DropDownList看了一下,发觉DropDownList控件并没有把ListItem的Attributes进行输出。所以我从DropDownList派生出来重写了几个方法实现相关功能。代码如下:
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());
}
}
}
}
- 可以设置选项背景颜色的DropDownList
- 可以设置背景和文字颜色的Static控件
- DropdownList设置空白选项
- Dropdownlist--自定义下拉选项的颜色
- 设置对话框的背景颜色
- 设置控件的背景颜色。
- 设置GtkEntry的背景颜色
- 设置uinavigationbar的背景颜色
- 设置tableViewCell的背景颜色
- 自定义背景颜色的设置
- UITableViewCell的背景颜色设置
- 设置Activity的背景颜色
- Myeclipse的背景颜色设置
- 设置progressbar的背景颜色
- 设置背景的颜色透明度
- 设置UISearchBar的背景颜色
- 设置UITableViewHeaderFooterView的背景颜色
- 设置Eclipse的背景颜色
- 一个数据访问层的概要设计
- 数据访问层的数据库提供者接口和数据操作接口
- vc中的坐标转换(抗遗忘贴)
- 一个ASP.NET工具栏和菜单控件(源码)
- 一个简单的分页控件(源码)
- 可以设置选项背景颜色的DropDownList
- DateDiff 函数-两个指定日期间的时间间隔数目
- ASP.NET运行模式
- Eclipse快速上手Hibernate之入门实例
- ASP.NET运行模式:PageHandlerFactory
- 巧用缓存提高asp程序的性能
- RUNDLL32简述
- asp.net实现组合查询的一种方法
- 利用JSF、SpringFramework和Hibernate构建Web应用的实例讲述