天天看點

擴充DropDownList控件和ListBox控件(1) - 支援分組功能(optgroup标簽)

DropDownList(ListBox)控件既強大又好用。為了讓它更強大、更好用,我們來寫一個繼承自DropDownList(ListBox)的控件。

[×××]

擴充DropDownList控件和ListBox控件(1) - 支援分組功能(optgroup标簽)

作者:webabcd

介紹

擴充DropDownList控件和ListBox控件:

通過DropDownList控件和ListBox控件的.Items.Add(ListItem item)方法,來為其添加optgroup标簽,進而實作分組功能

使用方法

1、設定屬性:

OptionGroupValue - 用于添加DropDownList(ListBox)控件的分組項的ListItem的Value值(預設為optgroup)

2、使用DropDownList(ListBox)控件的.Items.Add(ListItem item)方法:

OptionGroupValue為預設值時:SmartDropDownList.Items.Add(new ListItem("中國", "optgroup"));

圖示

擴充DropDownList控件和ListBox控件(1) - 支援分組功能(optgroup标簽)

關鍵代碼(以DropDownList為例)

SmartDropDownList.cs

using System;

using System.Collections.Generic;

using System.Text;

using System.Web.UI.WebControls;

using System.Web.UI;

[assembly: System.Web.UI.WebResource("YYControls.SmartDropDownList.Resources.Icon.bmp", "p_w_picpath/bmp")]

namespace YYControls

{

        /// <summary>

        /// SmartDropDownList類,繼承自DropDownList

        /// </summary>

        [ToolboxData(@"<{0}:SmartDropDownList runat='server'></{0}:SmartDropDownList>")]

        [System.Drawing.ToolboxBitmap(typeof(YYControls.Resources.Icon), "SmartDropDownList.bmp")]

        public partial class SmartDropDownList : DropDownList

        {

                /// <summary>

                /// 構造函數

                /// </summary>

                public SmartDropDownList()

                {

                }

                /// 将控件的内容呈現到指定的編寫器中

                /// <param name="writer">writer</param>

                protected override void RenderContents(HtmlTextWriter writer)    

                        // 呈現Option或OptionGroup

                        OptionGroupRenderContents(writer);

        }

}

Property.cs

using System.ComponentModel;

        /// SmartDropDownList類的屬性部分

        public partial class SmartDropDownList

                /// 用于添加SmartDropDownList的分組項的ListItem的Value值

                [

                Browsable(true),

                Description("用于添加DropDownList的分組項的ListItem的Value值"),

                Category("擴充")

                ]

                public virtual string OptionGroupValue

                        get

                        {

                                string s = (string)ViewState["OptionGroupValue"];

                                return (s == null) ? "optgroup" : s;

                        }

                        set

                                ViewState["OptionGroupValue"] = value;

OptionGroup.cs

using System.Data;

using System.Web;

                /// 呈現Option或OptionGroup

                private void OptionGroupRenderContents(HtmlTextWriter writer)

                        // 是否需要呈現OptionGroup的EndTag

                        bool writerEndTag = false;

                        foreach (ListItem li in this.Items)

                                // 如果沒有optgroup屬性則呈現Option

                                if (li.Value != this.OptionGroupValue)

                                {

                                        // 呈現Option

                                        RenderListItem(li, writer);

                                }

                                // 如果有optgroup屬性則呈現OptionGroup

                                else

                                        if (writerEndTag)

                                                // 呈現OptionGroup的EndTag

                                                OptionGroupEndTag(writer);

                                        else

                                                writerEndTag = true;

                                        // 呈現OptionGroup的BeginTag

                                        OptionGroupBeginTag(li, writer);

                        if (writerEndTag)

                                // 呈現OptionGroup的EndTag

                                OptionGroupEndTag(writer);

                /// 呈現OptionGroup的BeginTag

                /// <param name="li">OptionGroup資料項</param>

                private void OptionGroupBeginTag(ListItem li, HtmlTextWriter writer)

                        writer.WriteBeginTag("optgroup");

                        // 寫入OptionGroup的label

                        writer.WriteAttribute("label", li.Text);

                        foreach (string key in li.Attributes.Keys)

                                // 寫入OptionGroup的其它屬性

                                writer.WriteAttribute(key, li.Attributes[key]);

                        writer.Write(HtmlTextWriter.TagRightChar);

                        writer.WriteLine();

                /// 呈現OptionGroup的EndTag

                private void OptionGroupEndTag(HtmlTextWriter writer)

                        writer.WriteEndTag("optgroup");

                /// 呈現Option

                /// <param name="li">Option資料項</param>

                private void RenderListItem(ListItem li, HtmlTextWriter writer)

                        writer.WriteBeginTag("option");

                        // 寫入Option的Value

                        writer.WriteAttribute("value", li.Value, true);

                        if (li.Selected)

                                // 如果該Option被選中則寫入selected

                                writer.WriteAttribute("selected", "selected", false);

                                // 寫入Option的其它屬性

                        // 寫入Option的Text

                        HttpUtility.HtmlEncode(li.Text, writer);

                        writer.WriteEndTag("option");

OK