创建一个带分组功能的下拉列表框控件

介绍了OptgroupCombobox控件的实现原理及使用方法,该控件可在页面PostBack后保存状态,支持分组显示选项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这是 http://www.codeplex.com/上的一个开源项目SharpPieces , 不过作者目前只开发了这一个控件, 实现了页面PostBack后保存控件状态 .分享一下 .
效果如图:
单击显示全图,Ctrl+滚轮缩放图片


源码下载地址: 点击查看 
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");
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值