扩展DropDownList控件和ListBox控件
DropDownList(ListBox)控件既强大又好用。为了让它更强大、更好用,我们来写一个继承自DropDownList(ListBox)的控件。
介绍
扩展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
{
/**////
/// SmartDropDownList类,继承自DropDownList
///
[ToolboxData(@"<{0}:SmartDropDownList runat='server'>")]
[System.Drawing.ToolboxBitmap(typeof(YYControls.Resources.Icon), "SmartDropDownList.bmp")]
public partial class SmartDropDownList : DropDownList
{
/**////
/// 构造函数
///
public SmartDropDownList()
{
}
/**////
/// 将控件的内容呈现到指定的编写器中
///
///
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
{
/**////
/// 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;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Web;
namespace YYControls
{
/**////
/// SmartDropDownList类的属性部分
///
public partial class SmartDropDownList
{
/**////
/// 呈现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
///
///
///
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");
writer.WriteLine();
}
/**////
/// 呈现Option
///
///
///
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();
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端