从数据库读取数据动态生成树形菜单示例

用C#从数据库读取数据,动态生成树形菜单例子

数据库表

前台代码

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

<!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 id="Head1" runat="server">
<title>树形</title>
<style type="text/css">
 body{background:#ecefff}
.navPoint{COLOR:#666;CURSOR:hand;FONT-FAMILY:Webdings; FONT-SIZE:9pt}
.NiuNiutree{width:232px; height:525px; overflow:auto;}
</style>
</head>
<body>
  <form id="form1" runat="server">
   <div>
    <asp:TreeView ID="tree" runat="server" CssClass="NiuNiutree">
                </asp:TreeView> 
   </div>
 </form>
</body>
</html>

后台代码

using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.Odbc;
using System.Configuration;
using System.Collections;
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.Data.OracleClient;

public partial class demo : System.Web.UI.Page
{
    private SQLHelper sqlHelper;
    protected void Page_Load(object sender, EventArgs e)
    {
        sqlHelper = new SQLHelper();
        if (!Page.IsPostBack)
        {
           
            tree.ShowLines = true;
            tree.ShowExpandCollapse = true;
            TreeNodeCollection tnc = new TreeNodeCollection();
            tnc = tree.Nodes;
            //开始调用tncXunHuan,数据库中fuid=9999为根节点
            tncXunHuan(tnc, 9999);
        }

    }
    //树形菜单
    public void tncXunHuan(TreeNodeCollection tnc, int fuid)
    {

        try
        {
            DataSet ds = new DataSet();
            string sql = "select id,fuid,the from sys_organ where fuid='" + fuid + "'";
            ds = sqlHelper.getDataSet(sql);
            // ----------------end------------数据库获取数据
            //------------------start-----------根据数据结构向treeView添加节点
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                int id = Convert.ToInt16(dr["id"]);
                int Fuid = Convert.ToInt16(dr["fuid"]);
                string the = dr["the"].ToString();
                TreeNode tn = new TreeNode();
                tn.Text = the;
                tn.NavigateUrl = "javascript:getBoundary('" + the + "')";//调用前台js方法
                tn.ImageUrl = "images/file.png";//默认图标为file.png                   
                tnc.Add(tn);
                int tncInt = ds.Tables[0].Rows.IndexOf(dr);
                tncXunHuan(tnc[tncInt].ChildNodes, id); //----------递归调用
                // tnc = tnc;
            }
            //------------------end-----------根据数据结构向treeView添加节点
            //------------------start---------设置包含子节点的父节点的图标
            if (ds.Tables[0].Rows.Count > 0)
            {
                tnc[0].Parent.ImageUrl = "images/openfoldericon.png";//设置父文件图标

            }
            else
            {

                tnc[0].Parent.ImageUrl = "images/file.png";
            }


        }
        catch (Exception e) { return; }
    }
    
}

数据库操作文件 sqlHelper

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.OracleClient;

/// <summary>
///Class1 的摘要说明
/// </summary>
public class SQLHelper
{
    public SQLHelper()
	{
		//
		//TODO: 在此处添加构造函数逻辑
		//
	}
    private static string GetConnectionString()
    {
        return "Data Source=wgyx;user=wgyx;password=wgyx;";
    }
    /// <summary>
    /// 返回查询的数据集
    /// </summary>
    /// <param name="sql"></param>
    /// <returns></returns>
    public DataSet getDataSet(String sql)
    {
        DataSet ds = new DataSet();
        OracleConnection oraConn = null;
        try
        {
            oraConn = new OracleConnection(GetConnectionString());
            oraConn.Open();
            OracleDataAdapter oraDa = new OracleDataAdapter(sql, oraConn);
            oraDa.Fill(ds);
        }
        catch
        {
            return null;
        }
        finally
        {
            oraConn.Close();
        }
        return ds;
    }
    /// <summary>
    /// OracleDataReader
    /// </summary>
    /// <param name="sql"></param>
    /// <returns></returns>
    public OracleDataReader getDataReader(String sql)
    {

        OracleDataReader oraDr;
        OracleConnection oraConn = null;
         OracleCommand oraCmd=null;
        try
        {
            oraConn = new OracleConnection(GetConnectionString());
            oraConn.Open();
            oraCmd= new OracleCommand(sql, oraConn);
            oraDr = oraCmd.ExecuteReader();


        }
        catch
        {
            return null;
        }
        finally
        {
            
            oraConn.Close();
            
        }
        return oraDr;
    }
    
}

效果图



展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客
应支付0元
点击重新获取
扫码支付

支付成功即可阅读