从数据库读出数据动态生成菜单栏

注:这个是当初水平有限比较初级的 ,,后来后来 就 有相对高级点的参考http://blog.csdn.net/zhao8912/article/details/14441897
 
 
 
DBUnitly层:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Data;
namespace DBUnitly
{
   public class DBHelp
    {
       public static string _connectionString = ConfigurationManager.AppSettings["connectionString"];//连接数据库
       /// <summary>
       /// 执行sql语句 返回影响的行数
       /// </summary>
       /// <param name="SQLString"></param>
       /// <returns></returns>
       public static int ExecuteSql(string SQLString)
       {
           using(SqlConnection conn=new SqlConnection(_connectionString))
           {
               using(SqlCommand cmd=new SqlCommand(SQLString,conn))
               {
                   try 
                   {
                       conn.Open();
                       int rows = cmd.ExecuteNonQuery();
                       return rows;

                   }catch(System.Data.SqlClient.SqlException e)
                   {
                       conn.Close();
                       throw e;
                   }
               }
           }
       }
       /// <summary>
       /// 执行查询语句,返回DataSet
       /// </summary>
       /// <param name="SQLString">查询语句</param>
       /// <returns>DataSet</returns>
       public static DataSet Query(string SQLString)
       {
           using (SqlConnection connection = new SqlConnection(_connectionString))
           {
               DataSet ds = new DataSet();
               try
               {
                   connection.Open();
                   SqlDataAdapter command = new SqlDataAdapter(SQLString, connection);
                   command.Fill(ds, "ds");
               }
               catch (System.Data.SqlClient.SqlException ex)
               {
                   throw new Exception(ex.Message);
               }
               return ds;
           }
       }
    }
}
DAL层:
_menuManager类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace DAL
{
   public class _menuManager
    {
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.menu_id desc");
            }
            strSql.Append(")AS Row, T.*  from _menu T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DBUnitly.DBHelp.Query(strSql.ToString());
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select menu_id,menu_name");
            strSql.Append(" FROM _menu ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where menu_quanxian in (" + strWhere+")");
            }
            return DBUnitly.DBHelp.Query(strSql.ToString());
        }
    }
}
_menuChildrenManager类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace DAL
{
  public   class _menuChildrenManager
    {
      /// <summary>
      /// 获得数据列表
      /// </summary>
      /// <param name="strWhere"></param>
      /// <returns></returns>
      public DataSet GetAllList(string strWhere)
      {
          StringBuilder sb = new StringBuilder();
          sb.Append("select mc_name ");
          sb.Append(" from _menuChildren ");
          sb.Append(" where menu_type in ");
          if(strWhere.Trim()!="")
          {
              sb.Append("("+ Convert.ToInt32(strWhere)+")");
             
          }
         return DBUnitly.DBHelp.Query(sb.ToString());
        //  select mc_name from _menuChildren where menu_type in( select menu_id from _menu );
      }
    }
}
BLL层:
_menuService类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace BLL
{
   public class _menuService
    {
       /// <summary>
       /// 分页获得数据列表
       /// </summary>
       /// <param name="strWhere"></param>
       /// <param name="orderby"></param>
       /// <param name="startIndex"></param>
       /// <param name="endIndex"></param>
       /// <returns></returns>
       public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex) 
       {
           return new DAL._menuManager().GetListByPage(strWhere, orderby, startIndex, endIndex);
       }
       /// <summary>
       /// 获得数据列表
       /// </summary>
       /// <param name="strWhere"></param>
       /// <returns></returns>
       public DataSet GetList(string strWhere) 
       {
           return new DAL._menuManager().GetList(strWhere);
       }
    }
}
_menuChildrenService
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace BLL
{
   public class _menuChildrenService
    {
        /// <summary>
      /// 获得数据列表
      /// </summary>
      /// <param name="strWhere"></param>
      /// <returns></returns>
       public DataSet GetAllList(string strWhere) 
       {
           return new DAL._menuChildrenManager().GetAllList(strWhere);
       }
    }
}
Test层:
MenuTest.aspx
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Menu ID="Menu1" runat="server" Orientation="Horizontal" 
        DynamicEnableDefaultPopOutImage="False" DynamicVerticalOffset="10" 
        Height="24px" Width="126px">
                <StaticSelectedStyle HorizontalPadding="0px" />
                <StaticMenuItemStyle HorizontalPadding="5px" ForeColor="#3C3C3C" />
                <DynamicMenuStyle HorizontalPadding="0px" />
                <DynamicMenuItemStyle HorizontalPadding="0px" ItemSpacing="3px" ForeColor="#D96C01" />
                <StaticMenuStyle HorizontalPadding="0px" />
                 <DynamicHoverStyle ForeColor="#3D68A3" />
                 <StaticHoverStyle ForeColor="#D96C01" />
            </asp:Menu>
    </div>
    </form>
</body>

.cs文件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class MenuTest : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataTable dt = new BLL._menuService().GetList("0").Tables[0];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                MenuItem menuNode = new MenuItem();
                menuNode.Text = dt.Rows[i]["menu_name"].ToString();//一级菜单栏
                menuNode.Value = dt.Rows[i]["menu_id"].ToString();
                DataTable dt1 = new BLL._menuChildrenService().GetAllList(menuNode.Value).Tables[0];
                for (int j = 0; j < dt1.Rows.Count; j++)
                {
                    MenuItem menuChildNode = new MenuItem();
                    menuChildNode.Text = dt1.Rows[j]["mc_name"].ToString();//二级菜单
                    menuChildNode.Enabled = true;
                    menuNode.ChildItems.Add(menuChildNode);//将子菜单放在父菜单下
                }
                menuNode.Enabled = true;
                Menu1.Items.Add(menuNode);
            }

        }
    }
}

有源码:http://download.csdn.net/detail/zhao8912/4667911 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值