创建一个带分组功能的下拉列表框控件
2008-07-21 11:57
441 查看
这是http://www.codeplex.com/上的一个开源项目SharpPieces , 不过作者目前只开发了这一个控件, 实现了页面PostBack后保存控件状态 .分享一下 .
效果如图:
源码下载地址: 点击查看
http://www.codeplex.com/SharpPieces/Release/ProjectReleases.aspx?ReleaseId=6782
源代码如下 作者有详细的注释
复制
保存
使用方法:
复制
保存
效果如图:
源码下载地址: 点击查看
http://www.codeplex.com/SharpPieces/Release/ProjectReleases.aspx?ReleaseId=6782
源代码如下 作者有详细的注释
复制
保存
using System; using System.Collections.Generic; using System.Text; using System.Web.UI.WebControls; using System.Web; namespace ASPNETControls { public class OptgroupCombobox : DropDownList { private const string optGroupAttributeKey = "optgroup"; /// <summary> /// Override the SaveViewState to save the control's Attributes /// </summary> /// <returns></returns> protected override object SaveViewState() { // Create an object array with one element for the CheckBoxList's // ViewState contents, and one element for each ListItem in skmCheckBoxList object[] state = new object[this.Items.Count + 1]; object baseState = base.SaveViewState(); state[0] = baseState; // Now, see if we even need to save the view state bool itemHasAttributes = false; for (int i = 0; i < this.Items.Count; i++) { if (this.Items[i].Attributes.Count > 0) { itemHasAttributes = true; // Create an array of the item's Attribute's keys and values object[] attribKV = new object[this.Items[i].Attributes.Count * 2]; int k = 0; foreach (string key in this.Items[i].Attributes.Keys) { attribKV[k++] = key; attribKV[k++] = this.Items[i].Attributes[key]; } state[i + 1] = attribKV; } } // return either baseState or state, depending on whether or not // any ListItems had attributes if (itemHasAttributes) return state; else return baseState; } /// <summary> /// Override the LoadViewState to load the control's Attributes /// </summary> protected override void LoadViewState(object savedState) { if (savedState == null) return; // see if savedState is an object or object array if (savedState is object[]) { // we have an array of items with attributes object[] state = (object[]) savedState; base.LoadViewState(state[0]); // load the base state for (int i = 1; i < state.Length; i++) { if (state[i] != null) { // Load back in the attributes object[] attribKV = (object[]) state[i]; for (int k = 0; k < attribKV.Length; k += 2) this.Items[i - 1].Attributes.Add(attribKV[k].ToString(), attribKV[k + 1].ToString()); } } } else // we have just the base state base.LoadViewState(savedState); } protected override void RenderContents(System.Web.UI.HtmlTextWriter writer) { if (this.Items.Count > 0) { bool selected = false; bool optGroupStarted = false; for (int i = 0; i < this.Items.Count; i++) { ListItem item = this.Items[i]; if (item.Enabled) { if (item.Attributes[optGroupAttributeKey] != null) { if (optGroupStarted) writer.WriteEndTag("optgroup"); writer.WriteBeginTag("optgroup"); writer.WriteAttribute("label", item.Text); writer.Write('>'); writer.WriteLine(); optGroupStarted = true; } else { writer.WriteBeginTag("option"); if (item.Selected) { if (selected) { this.VerifyMultiSelect(); } selected = true; writer.WriteAttribute("selected", "selected"); } writer.WriteAttribute("value", item.Value, true); if (item.Attributes.Count > 0) { item.Attributes.Render(writer); } if (this.Page != null) { this.Page.ClientScript.RegisterForEventValidation(this.UniqueID, item.Value); } writer.Write('>'); HttpUtility.HtmlEncode(item.Text, writer); writer.WriteEndTag("option"); writer.WriteLine(); } } } if (optGroupStarted) writer.WriteEndTag(optGroupAttributeKey); } } /// <summary> /// Adds a optgroup element. /// </summary> public void AddGroup(string text) { ListItem li = new ListItem(); li.Text = text; li.Attributes[optGroupAttributeKey] = "1"; this.Items.Add(li); } } }
使用方法:
复制
保存
this.cboTest.Items.Add("Choose an item"); this.cboTest.AddGroup("Computers"); this.cboTest.Items.Add("Monitor"); this.cboTest.Items.Add("Mouse"); this.cboTest.Items.Add("Keyboard"); this.cboTest.AddGroup("Phones"); this.cboTest.Items.Add("iPhone"); this.cboTest.Items.Add("gPhone");
相关文章推荐
- 创建一个带分组功能的下拉列表框控件
- 创建一个带分组功能的下拉列表框控件
- C#第十周任务之最后一项之创建一个如下的窗体,并在窗体上放置一个菜单、一个工具栏控件。菜单内容如第二个图所示。工具栏上有两个按钮,分别对应“打开文本文件”、“保存文本文件”。菜单和工具栏具体功能实现可
- 创建一个如下的窗体,并在窗体上放置colorDialog控件。实现功能:1)程序运行时,单击打开颜色对话框按钮,可选择颜色,并以所选颜色作为窗体背景色
- 用jQuery创建一个可编辑的SELECT下拉控件(HTML/CSS)
- 使用CMFCOutlookBarPane创建分组控件,禁用双击编辑的功能
- (CEGUI)如何创建控件 ------下拉列表框,表格框,LISTBOX
- 一个模仿HTML5功能的jquery控件
- 一个模仿HTML5功能的jquery控件
- CComboBox控件的下拉列表框,用鼠标在该ListBox上滑动的时候显示出当前具有focus的item项的索引
- 使用两个 Windows 窗体 DataGridView 控件创建一个主/从窗体
- QML创建一个带多个下拉输入框的窗口(ComboBox)
- 创建一个带模版的用户控件
- 一个简单的文件上传功能控件(原创)
- 【iOS开发新手上路】教你一步一步创建一个功能齐全的UITableViewController(上)
- 自己创建一个新的Vec类用来实现vector的功能
- 博为峰Java技术文章 ——JavaSE Swing使用数组和Vector创建下拉列表框
- 类库-----一些经常用到的对控件的处理1. 获取文本框中输入内容2.清除文本框内容3. 获取下拉列表框中选择内容,类型0为Text值,类型1为Value值4.DropDownList绑定日期
- "创建控件时出错/lm/w3svc/... 不是一个有效的iis应用程序" 的解决办法
- 一个常被问道的问题:如何让层盖住下拉列表框? 问题解决方案.