为DropDownList 添加optgroup分组以及为ListItem 加式样

先写一个类库文件(对原文件做了修改),如下:
/// <summary>
    /// 冯东耀修改于2008-11-21
    /// </summary>
    public class NewDropDownList : DropDownList
    {
        //构造函数
        public NewDropDownList(DataTable dt)
        {
            ListItem item = new ListItem();
            foreach (DataRow row in dt.Rows)
            {
                item.Value = row[0].ToString();
                item.Text = row[1].ToString();
                if (row[3].ToString() == "0")
                    item.Attributes.Add("optgroup", item.Text);
                Items.Add(item);
            }
        }

        //重写方法
        protected override void RenderContents(HtmlTextWriter writer)
        {
            string optgroup;
            ArrayList optOptionGroups = new ArrayList();
            foreach (ListItem item in this.Items)
            {
                if (item.Attributes["optgroup"] == null)
                {
                    RenderListItem(item, writer);
                }
                else
                {
                    optgroup = item.Attributes["optgroup"];
                    if (optOptionGroups.Contains(optgroup))
                    {
                        RenderListItem(item, writer);
                    }
                    else
                    {
                        if (optOptionGroups.Count > 0)
                        {
                            optgroupEndTag(writer);
                        }
                        optgroupBeginTag(optgroup, writer);
                        optOptionGroups.Add(optgroup);
                    }
                }
            }
            if (optOptionGroups.Count > 0)
            {
                optgroupEndTag(writer);
            }
        }

        //option 简单添加style
        private void RenderListItem(ListItem item, HtmlTextWriter writer)
        {
            writer.WriteBeginTag("option");
            writer.WriteAttribute("value", item.Value, true);
            //writer.WriteAttribute("style", "color:#7395c1", true);//此处可以更改选项的字体颜色
            if (item.Selected)
            {
                writer.WriteAttribute("selected", "selected", false);
            }
            foreach (string key in item.Attributes.Keys)
            {
                writer.WriteAttribute(key, item.Attributes[key]);
            }
            writer.Write(HtmlTextWriter.TagRightChar);
            HttpUtility.HtmlEncode(item.Text, writer);
            writer.WriteEndTag("option");
            writer.WriteLine();
        }

        //option 添加optgroup
        private void optgroupBeginTag(string name, HtmlTextWriter writer)
        {
            writer.WriteBeginTag("optgroup");
            writer.WriteAttribute("label", name);
            writer.Write(HtmlTextWriter.TagRightChar);
            writer.WriteLine();
        }
        private void optgroupEndTag(HtmlTextWriter writer)
        {
            writer.WriteEndTag("optgroup");
            writer.WriteLine();
        }

    }
将它保存为一个类库文件。然后新建一个aspx页面,前台添加一个Panel,后台代码要添加上面类库文件的命名空间。
aspx代码
    <form id="form1" runat="server">
        <asp:Panel ID="Panel1" runat="server" Height="50px" Width="125px">
        </asp:Panel>
    </form>
aspx.cs代码
    protected void Page_Load(object sender, EventArgs e)
    {
        DataTable dt = RK.Dal.ArticleMgr.GetFSCategory();
//首先得到一个dt,数据要求,
//第一列为DropDownList选项的值,
//第二列为DropDownList选项的显示文本,
//第三列是父级id,如果为0则表示是顶级;
        NewDropDownList ddl = new NewDropDownList(dt);
        ddl.ID = "DropClassId";
        this.Panel1.Controls.Add(ddl);
}

你可能感兴趣的:(group)