天天看點

SmartDropDownList 綁定類型及類型下的内容

SmartDropDownList 綁定類型及類型下的内容

<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 &gt; 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 &gt; 0)

                        {

                            optgroupEndTag(writer);

                        }

                        optgroupBeginTag(optgroup, writer);

                        optOptionGroups.Add(optgroup);

            if (optOptionGroups.Count &gt; 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();

繼續閱讀