开发了一款有趣的自定义Asp.net服务器控件: GroupList[开放源代码]
Posted on 2005-04-28 15:05 bestcomy 阅读(4111) 评论(5) 编辑 收藏 举报简介
此控件是封装一个标准的HTML控件,但是ASP.NET缺没有提供此控件的服务器版,因为我自己的项目中用到了这样的控件,所以趁这几天有空整理了一下,先体验一下设计时和运行时的效果:
- 设计时:
- 运行时:
此控件与我前些日子介绍的ComboBox原理相同。区别在于:
- ComboBox只实现了一层控件层次结构,而GroupList实现了嵌套的二层控件层次结构。我还想实现如多级菜单和树的无限级控件层次结构,通过这两个控件的开发我发现这不难实现。
- 实现了一个SelectedIndexChanged事件。
特点
相对于DropDownList来说有如下两个有趣的特点:- 实现了选择项的分组
- 分组项不能被选择
使用示例
- 编程方式动态添加分组选择项
for(int i=1; i < 4; i++)
{
GroupItem gItem = new GroupItem("Group_"+i.ToString());
for(int j=1; j < 6; j++)
{
GroupListItem item = new GroupListItem(gItem.Label + "_Item_" + j.ToString());
gItem.Items.Add(item);
}
GroupList1.GroupItems.Add(gItem);
} - 设计时页面声明
<bestcomy:grouplist id="Grouplist3" runat="server">
<bestcomy:GroupItem Label="Group1">
<bestcomy:GroupListItem Text="Group1_Item1" Value="Group1_Item1" Selected="False"></bestcomy:GroupListItem>
<bestcomy:GroupListItem Text="Group1_Item2" Value="Group1_Item2" Selected="False"></bestcomy:GroupListItem>
</bestcomy:GroupItem>
<bestcomy:GroupItem Label="Group2">
<bestcomy:GroupListItem Text="Group2_Item1" Value="Group2_Item1" Selected="False"></bestcomy:GroupListItem>
<bestcomy:GroupListItem Text="Group2_Item2" Value="Group2_Item2" Selected="True"></bestcomy:GroupListItem>
</bestcomy:GroupItem>
</bestcomy:grouplist>
- 数据绑定
DataTable dt = new DataTable();
dt.Columns.Add("group", typeof(string));
dt.Columns.Add("text", typeof(string));
dt.Columns.Add("value", typeof(string));
for(int i=0; i < 10; i++)
{
DataRow ndr = dt.NewRow();
ndr["group"] = "Group_" + ((int)(i%2)+1).ToString();
ndr["text"] = "Text_" + ((int)(i+1)).ToString();
ndr["value"] = "value_" + ((int)(i+1)).ToString();
dt.Rows.Add(ndr);
}
this.Grouplist2.DataGroupField = "group";
this.Grouplist2.DataTextField = "text";
this.Grouplist2.DataValueField = "value";
this.Grouplist2.DataSource = dt.DefaultView;
this.Grouplist2.DataBind();
源代码
分类:
随笔
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架