天天看點

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

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

<a href="http://down.51cto.com/data/101204" target="_blank">[源碼下載下傳]</a>

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

介紹

擴充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為例)

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", "image/bmp")] 

namespace YYControls 

        /// &lt;summary&gt; 

        /// SmartDropDownList類,繼承自DropDownList 

        /// &lt;/summary&gt; 

        [ToolboxData(@"&lt;{0}:SmartDropDownList runat='server'&gt;&lt;/{0}:SmartDropDownList&gt;")] 

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

        public partial class SmartDropDownList : DropDownList 

        { 

                /// &lt;summary&gt; 

                /// 構造函數 

                /// &lt;/summary&gt; 

                public SmartDropDownList() 

                { 

                } 

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

                /// &lt;param name="writer"&gt;writer&lt;/param&gt; 

                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 

                /// &lt;param name="li"&gt;OptionGroup資料項&lt;/param&gt; 

                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 

                /// &lt;param name="li"&gt;Option資料項&lt;/param&gt; 

                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

     本文轉自webabcd 51CTO部落格,原文連結:http://blog.51cto.com/webabcd/345425,如需轉載請自行聯系原作者

繼續閱讀