扩展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为例)
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 

         /// <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() 
                { 
 
                } 
 
                 /// <summary> 
                 /// 将控件的内容呈现到指定的编写器中 
                 /// </summary> 
                 /// <param name="writer">writer</param> 
                 protected  override  void RenderContents(HtmlTextWriter writer)    
                { 
                         // 呈现Option或OptionGroup 
                        OptionGroupRenderContents(writer); 
                } 
        } 
}
 
Property.cs
using System; 
using System.Collections.Generic; 
using System.Text; 
 
using System.ComponentModel; 
using System.Web.UI; 
 
namespace YYControls 

         /// <summary> 
         /// SmartDropDownList类的属性部分 
         /// </summary> 
         public partial  class SmartDropDownList 
        { 
                 /// <summary> 
                 /// 用于添加SmartDropDownList的分组项的ListItem的Value值 
                 /// </summary> 
                [ 
                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; 
using System.Collections.Generic; 
using System.Text; 
 
using System.Data; 
using System.Web.UI.WebControls; 
using System.Web.UI; 
using System.Web; 
 
namespace YYControls 

         /// <summary> 
         /// SmartDropDownList类的属性部分 
         /// </summary> 
         public partial  class SmartDropDownList 
        { 
                 /// <summary> 
                 /// 呈现Option或OptionGroup 
                 /// </summary> 
                 /// <param name="writer">writer</param> 
                 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); 
                } 
 
                 /// <summary> 
                 /// 呈现OptionGroup的BeginTag 
                 /// </summary> 
                 /// <param name="li">OptionGroup数据项</param> 
                 /// <param name="writer">writer</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(); 
                } 
 
                 /// <summary> 
                 /// 呈现OptionGroup的EndTag 
                 /// </summary> 
                 /// <param name="writer">writer</param> 
                 private  void OptionGroupEndTag(HtmlTextWriter writer) 
                { 
                        writer.WriteEndTag( "optgroup"); 
                        writer.WriteLine(); 
                } 
 
                 /// <summary> 
                 /// 呈现Option 
                 /// </summary> 
                 /// <param name="li">Option数据项</param> 
                 /// <param name="writer">writer</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); 
                        } 
 
                         foreach ( string key  in li.Attributes.Keys) 
                        { 
                                 // 写入Option的其它属性 
                                writer.WriteAttribute(key, li.Attributes[key]); 
                        } 
 
                        writer.Write(HtmlTextWriter.TagRightChar); 
 
                         // 写入Option的Text 
                        HttpUtility.HtmlEncode(li.Text, writer); 
 
                        writer.WriteEndTag( "option"); 
                        writer.WriteLine(); 
                } 
        } 
}
 


     本文转自webabcd 51CTO博客,原文链接: http://blog.51cto.com/webabcd/345425 ,如需转载请自行联系原作者

相关文章
|
6月前
element-ui实现表格单选的功能
element-ui实现表格单选的功能
|
6月前
easyui-combobox 下拉列表排序
easyui-combobox 下拉列表排序
98 0
|
6月前
|
索引
[Qt5&控件] 下拉框ComBoBox和层叠窗口StackedWidget控件组合使用
[Qt5&控件] 下拉框ComBoBox和层叠窗口StackedWidget控件组合使用
152 0
|
移动开发 HTML5
Element-ui中 选择器(select)多选下拉框实现全选功能
Element-ui中 选择器(select)多选下拉框实现全选功能
854 0
Element-ui中 选择器(select)多选下拉框实现全选功能
|
C# C++
WPF - Group分组对ListBox等列表样式的约束
原文:WPF - Group分组对ListBox等列表样式的约束   在做WPF主题支持时,出现一个分组引起的莫名错误,可是折腾了我一番。在没有使用样式时,列表分组很正常,使用了别人写的ListBox列表样式后,发现GroupItem分组区没有内容,是空的,本篇把这一问题的解决过程给大家说一下,做主题时可以注意分组对列表样式的限制了。
1743 0
|
数据库 算法 缓存