zoukankan      html  css  js  c++  java
  • SmartDropDownList 绑定类型及类型下的内容

    <asp:Panel ID="Panel1" runat="server"></asp:Panel>

    if (!IsPostBack) { }
               
                IList<Accounts_Duty> adlist = new Accounts_DutyBLL().GetALLItems();
                SmartDropDownList sddl = new SmartDropDownList();
                sddl.AutoPostBack = true;
                sddl.ID = "sddlControl";
                sddl.Items.Add(new ListItem("请选择..", ""));
                //sddl.Attributes.Add("valid", "required");
                //sddl.Attributes.Add("errmsg", "请选择套系类型!");
                ListItem li = new ListItem();
                foreach (Accounts_Duty type in adlist)
                {
                    li = new ListItem();
                    li.Attributes.Add("optgroup", type.DutyName);
                    List<Accounts_User> aulist = new Accounts_UserBLL().GetItemsByCondition(new List<string> { "DutyID," + type.ID + ",=" });
                    if (aulist.Count > 0)
                    {
                        sddl.Items.Add(li);
                        foreach (Accounts_User m in aulist)
                        {
                            li = new ListItem(m.TrueName, m.UserID.ToString());
                            sddl.Items.Add(li);
                        }
                    }
                }
                sddl.SelectedIndexChanged += new EventHandler(this.sddlControl_SelectedIndexChanged);

                Panel1.Controls.Add(sddl);

     protected void sddlControl_SelectedIndexChanged(object sender, EventArgs e)
            {
                SmartDropDownList sddl = (SmartDropDownList)this.FindControl("sddlControl");
                //选中的值
                string value = sddl.SelectedValue;
              
            }

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI.WebControls;
    using System.Collections;
    using System.Web.UI;

    namespace Demo.Web.General.Controls.Extension
    {
        public class SmartDropDownList:DropDownList
        {
            //重写方法
            protected override void RenderContents(HtmlTextWriter writer)
            {
                string optgroup;
                ArrayList optOptionGroups = new ArrayList();
                foreach (ListItem item in this.Items)
                {
                    if (item.Attributes["optgroup"] == null)
                    {
                        RenderListItem(item, writer);
                    }
                    else
                    {
                        optgroup = item.Attributes["optgroup"];
                        if (optOptionGroups.Contains(optgroup))
                        {
                            RenderListItem(item, writer);
                        }
                        else
                        {
                            if (optOptionGroups.Count > 0)
                            {
                                optgroupEndTag(writer);
                            }
                            optgroupBeginTag(optgroup, writer);
                            optOptionGroups.Add(optgroup);
                        }
                    }
                }
                if (optOptionGroups.Count > 0)
                {
                    optgroupEndTag(writer);
                }
            }

            //option 简单添加style
            private void RenderListItem(ListItem item, HtmlTextWriter writer)
            {
                writer.WriteBeginTag("option");
                writer.WriteAttribute("value", item.Value, true);
                if (item.Selected)
                {
                    writer.WriteAttribute("selected", "selected", false);
                }
                foreach (string key in item.Attributes.Keys)
                {
                    writer.WriteAttribute(key, item.Attributes[key]);
                }
                writer.Write(HtmlTextWriter.TagRightChar);
                HttpUtility.HtmlEncode(item.Text, writer);
                writer.WriteEndTag("option");
                writer.WriteLine();
            }

            //option 添加optgroup
            private void optgroupBeginTag(string name, HtmlTextWriter writer)
            {
                writer.WriteBeginTag("optgroup");
                writer.WriteAttribute("label", name);
                writer.Write(HtmlTextWriter.TagRightChar);
                writer.WriteLine();
            }

            private void optgroupEndTag(HtmlTextWriter writer)
            {
                writer.WriteEndTag("optgroup");
                writer.WriteLine();
            }

            //调用(根据DropDownList 无限级分级一文修改这个构造函数)
            //public NewDropDownList()
            //{
            //    ListItem li1 = new ListItem();
            //    li1.Attributes.Add("optgroup", "dsfasf");
            //    Items.Add(li1);
            //    ListItem li2 = new ListItem();
            //    Items.Add(li2);
            //    ListItem li3 = new ListItem();
            //    Items.Add(li3);
            //}
        }
    }


     

  • 相关阅读:
    删除XML文档中某节点
    水晶报表之创建子报表
    给字符串中的每个字符加上单引号
    Failed to export using the options you specified. Please check your options and try again
    从ASP.NET传递参数给水晶报表
    两个文本框异动任何一个能即时更新计算结果
    添加节点至XML文档中去
    读取XML文档存入泛型List<T>集合中
    泛型List<T>转存为XML文档
    怎样创建XML文档
  • 原文地址:https://www.cnblogs.com/liyuxin/p/2097118.html
Copyright © 2011-2022 走看看