当前位置:编程学习 > C#/ASP.NET >>

如何在asp.net中对listBox 用 optgroup 分组

如何在asp.net中对listBox 用 optgroup 分组
分多个组 --------------------编程问答-------------------- 友情up一下。。。 --------------------编程问答-------------------- .net的listbox貌似没有分组功能,可以试试重写一个optgroup功能 --------------------编程问答-------------------- 没有这功能.得重写..

给你一个dropdownlist重写的例子,应该类似:


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();
        }
    }
}
--------------------编程问答--------------------

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;
            }
        }
    }
}
--------------------编程问答--------------------

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);
        }
    }
}

--------------------编程问答-------------------- listbox的,代码如下: --------------------编程问答--------------------

using System;
using System.Collections.Generic;
using System.Text;

using System.Web.UI.WebControls;
using System.Web.UI;

[assembly: System.Web.UI.WebResource("YYControls.SmartListBox.Resources.Icon.bmp", "image/bmp")]

namespace YYControls
{
    /// <summary>
    /// SmartListBox类,继承自ListBox
    /// </summary>
    [ToolboxData(@"<{0}:SmartListBox runat='server'></{0}:SmartListBox>")]
    [System.Drawing.ToolboxBitmap(typeof(YYControls.Resources.Icon), "SmartListBox.bmp")]
    public partial class SmartListBox : ListBox
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public SmartListBox()
        {

        }

        /// <summary>
        /// 将控件的内容呈现到指定的编写器中
        /// </summary>
        /// <param name="writer">writer</param>
        protected override void RenderContents(HtmlTextWriter writer) 
        {
            // 呈现Option或OptionGroup
            OptionGroupRenderContents(writer);
        }
    }
}

--------------------编程问答--------------------

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>
    /// SmartListBox类的属性部分
    /// </summary>
    public partial class SmartListBox
    {
        /// <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();
        }
    }
}

--------------------编程问答--------------------

using System;
using System.Collections.Generic;
using System.Text;

using System.ComponentModel;
using System.Web.UI;

namespace YYControls
{
    /// <summary>
    /// SmartListBox类的属性部分
    /// </summary>
    public partial class SmartListBox
    {
        /// <summary>
        /// 用于添加SmartListBox的分组项的ListItem的Value值
        /// </summary>
        [
        Browsable(true),
        Description("用于添加ListBox的分组项的ListItem的Value值"),
        Category("扩展")
        ]
        public virtual string OptionGroupValue
        {
            get
            {
                string s = (string)ViewState["OptionGroupValue"];

                return (s == null) ? "optgroup" : s;
            }
            set
            {
                ViewState["OptionGroupValue"] = value;
            }
        }
    }
}

--------------------编程问答-------------------- http://www.cnblogs.com/webabcd/archive/2008/01/03/1024977.html --------------------编程问答-------------------- 用HTML 输出..


            #region HTML输出
                string sql = "select * from Dictionaries where type=5 and ParentID=0";//父级类别
               DataTable 类别 = SQLHelper.ExecuteDataTable(CommandType.Text, sql, new DataSet(), "s", null);
                L类别.Text += "<select   id=select name='filter' onchange=quzhi(); >   ";
                L类别.Text += "<option   value='0'>请选择</option> ";
                foreach (DataRow row in 类别.Rows)
                {
                    sql = "select  * from Dictionaries where ParentID=" + row[0].ToString();//子集类别
                    DataTable 子项 = SQLHelper.ExecuteDataTable(CommandType.Text, sql, new DataSet(), "s", null);
                    if (子项.Rows.Count > 0)//存在子集就添加子集
                    {
                        L类别.Text += " <optgroup   label='--------------------- '>   </optgroup>     ";
                        L类别.Text += " <optgroup   label=" + row[1].ToString() + ">   ";
                        foreach (DataRow item in 子项.Rows)
                        {
                            L类别.Text += "   <option   value=" + item[0].ToString() + " >" + item[1].ToString() + "</option>    ";
                        }
                        L类别.Text += "  </optgroup>   ";
                        L类别.Text += " <optgroup   label=' '>   </optgroup>     ";
                    }
                    else
                    {
                        L类别.Text += "  <option   value=" + row[0].ToString() + ">" + row[1].ToString() + "</option>   ";
                    }
                }
                L类别.Text += "</select>     ";
                #endregion


用JS获取选中的值 赋值给隐藏域

   $("#hide").val($("#select").val());


--------------------编程问答-------------------- 用扩展控件SmartDropDownList
http://www.cnblogs.com/webabcd/archive/2008/01/03/1024977.html
http://www.cnblogs.com/chenying99/archive/2012/02/23/2365448.html
补充:.NET技术 ,  ASP.NET
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,