<asp:Panel ID="Panel1" runat="server"></asp:Panel>
if (!IsPostBack) { }
IList<Accounts_Duty> adlist = new Accounts_DutyBLL().GetALLItems();
SmartDropDownList sddl = new SmartDropDownList();
sddl.AutoPostBack = true;
sddl.ID = "sddlControl";
sddl.Items.Add(new ListItem("请选择..", ""));
//sddl.Attributes.Add("valid", "required");
//sddl.Attributes.Add("errmsg", "请选择套系类型!");
ListItem li = new ListItem();
foreach (Accounts_Duty type in adlist)
{
li = new ListItem();
li.Attributes.Add("optgroup", type.DutyName);
List<Accounts_User> aulist = new Accounts_UserBLL().GetItemsByCondition(new List<string> { "DutyID," + type.ID + ",=" });
if (aulist.Count > 0)
{
sddl.Items.Add(li);
foreach (Accounts_User m in aulist)
{
li = new ListItem(m.TrueName, m.UserID.ToString());
sddl.Items.Add(li);
}
}
}
sddl.SelectedIndexChanged += new EventHandler(this.sddlControl_SelectedIndexChanged);
Panel1.Controls.Add(sddl);
protected void sddlControl_SelectedIndexChanged(object sender, EventArgs e)
{
SmartDropDownList sddl = (SmartDropDownList)this.FindControl("sddlControl");
//选中的值
string value = sddl.SelectedValue;
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.Collections;
using System.Web.UI;
namespace Demo.Web.General.Controls.Extension
{
public class SmartDropDownList:DropDownList
{
//重写方法
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);
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();
}
//调用(根据DropDownList 无限级分级一文修改这个构造函数)
//public NewDropDownList()
//{
// ListItem li1 = new ListItem();
// li1.Attributes.Add("optgroup", "dsfasf");
// Items.Add(li1);
// ListItem li2 = new ListItem();
// Items.Add(li2);
// ListItem li3 = new ListItem();
// Items.Add(li3);
//}
}
}