zoukankan      html  css  js  c++  java
  • asp.net中用DropDownList实现无限级分类菜单(转)

    因项目需要,要做一个菜单无限级分类,最后选择使用DroopDownList控件.时间紧急,只贴出代码,有相同需求的,可以看看 
       (1)数据库表说明:
       PKID:不用说了吧
       ClassName:类别名称
       UPID:上级类别的ID(默认为0,表示一级大类)

    (2)代码如下:
    <1>default.aspx页面代码如下:

    <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:PlaceHolder ID="PH" runat="server"></asp:PlaceHolder>
            <asp:Button ID="btn_Enter" runat="server" OnClick="btn_Enter_Click" Text="  确定  " /></div>
        </form>
    </body>
    </html>


    <2>default.aspx.cs代码如下:

    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Collections;
    using System.Data.SqlClient;


    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ArrayList al;
            if (Session["Controls"] != null)
            {

                al = (ArrayList)Session["Controls"];
                PH.Controls.Clear();
                for (int i = 0; i < al.Count; i++)
                {
                    DropDownList ddl = (DropDownList)al[i];
                    ddl.SelectedIndexChanged += new EventHandler(dl_SelectedIndexChanged);
                    PH.Controls.Add(ddl);
                }
            }

            if (!IsPostBack)
            {
                CreateControls("select * from t_sj_classinfo where upid=0", 0);
            }
        }
        void CreateControls(string sql, int CurID)
        {
            ArrayList al;
           
            if (Session["Controls"] == null)
                al = new ArrayList();
            else
            {
                al = (ArrayList)Session["Controls"];
                for (int i = 0; i < al.Count; i++)
                {
                    DropDownList ddl_Remove = (DropDownList)al[i];

                    if (Convert.ToInt32(ddl_Remove.ID) > CurID)
                    {
                        i--;
                        al.Remove(ddl_Remove);
                        PH.Controls.Remove(ddl_Remove);
                    }
                }
            }
          

            SqlConnection conn = new SqlConnection("server=localhost;uid=sa;pwd=123456;database=test");

            SqlCommand cmd = new SqlCommand(sql, conn);
            DataTable dt = new DataTable();
            SqlDataAdapter drp = new SqlDataAdapter(cmd);
            drp.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                DropDownList dl = new DropDownList();
                CurID++;
                dl.ID = CurID.ToString();
                dl.AutoPostBack = true;
                dl.SelectedIndexChanged += new EventHandler(dl_SelectedIndexChanged);
                dl.DataSource = dt;
                dl.DataTextField = "ClassName";
                dl.DataValueField = "PKID";
                dl.DataBind();
                dl.Items.Insert(0, new ListItem("--请选择--", "0"));
                al.Add(dl);
                PH.Controls.Add(dl);
                Session["Controls"] = al;
            }


        }
        void dl_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList dl = (DropDownList)sender;
            string LBID = dl.SelectedValue;
            CreateControls("select * from t_sj_classinfo where upid=" + LBID, Convert.ToInt32(dl.ID));
        }

        protected void btn_Enter_Click(object sender, EventArgs e)
        {
            ControlCollection Ctl = PH.Controls;
            System.Text.StringBuilder sb = new System.Text.StringBuilder();

            foreach (Control c in Ctl)
            {
             //如果PH中还有其它的服务器控件,此处必须加入服务器控件类型的判断
                DropDownList dl = (DropDownList)c;
                sb.Append("选中的文本:" + dl.SelectedItem.Text + "    选中的值:" + dl.SelectedItem.Value + "<br/>");           
            }
            Response.Write(sb.ToString());
        }
    }

    最后显示的结果页面样例如:


  • 相关阅读:
    C#编程总结 字符转码
    获取 苹果UDID 序列号
    SVN :This XML file does not appear to have any style information associated with it.
    oracle 实时查询最耗CPU资源的SQL语句
    Oracle 优化和性能调整
    weblogic管理脚本
    weblogic状态监控脚本
    oracle性能检测sql语句
    Oracle动态性能表-V$SESSION_WAIT,V$SESSION_EVENT
    linux HBA 卡驱动安装
  • 原文地址:https://www.cnblogs.com/xlfj521/p/987343.html
Copyright © 2011-2022 走看看