获取菜单项在asp.net数据库 [英] Getting Menu Items from database in asp.net
问题描述
我试图让从SQL Server中的菜单项2008数据库。
香港专业教育学院尝试这样的事情,因为我用Google搜索了一圈,发现这些教程
第一和<一个href=\"http://www.$c$cproject.com/Articles/366450/Permissions-and-Levels-in-ASP-Menu?msg=4361548#xx4361548xx\"相对=nofollow>第二:
使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用的System.Web;
使用System.Web.UI程序;
使用System.Web.UI.WebControls;
使用System.Data这;
使用System.Data.SqlClient的;
使用System.Configuration;
使用System.Text;菜单驱动的命名空间
{
公共部分类MenuDB:System.Web.UI.Page
{
保护无效的Page_Load(对象发件人,EventArgs的发送)
{
menuBar.MaximumDynamicDisplayLevels = 3; 如果(!的IsPostBack)
{ SqlConnection的CON =新的SqlConnection(ConfigurationManager.ConnectionStrings [MyConnection的]的ConnectionString); con.Open();
DataSet的DS =新的DataSet();
DataTable的DT =新的DataTable();
字符串SQL =选择的MenuItems *;
SqlDataAdapter的大=新SqlDataAdapter的(SQL,CON);
da.Fill(DS);
dt的= ds.Tables [0];
的DataRow [] = drowpar dt.Select(PARENTID =+ 0); 的foreach(在drowpar的DataRow博士)
{
menuBar.Items.Add(新菜单项(DR [菜单名称]。的ToString()
博士[MENUID]。的ToString(),,
博士[MenuLocation]的ToString()))。
} 的foreach(DataRow的医生在dt.Select(PARENTID&gt;中+ 0))
{
菜单项MNU =新菜单项(DR [菜单名称]。的ToString(),博士[MENUID]。的ToString()
博士[MenuLocation]的ToString()); // code为多个菜单级别
字符串valuePath = getValuePath(Convert.ToInt32(DR [PARENTID]的ToString()),DT);
//menuBar.FindItem(dr[\"ParentID\"].ToString()).ChildItems.Add(mnu);
menuBar.FindItem(valuePath).ChildItems.Add(MNU); **的NullReferenceException是由code处理**
//结束code为多个菜单级别
}
con.Close();
} }
私人字符串getValuePath(的Int32家长,DataTable的DT)
{
INT predecessor =父;
StringBuilder的valuePath =新的StringBuilder();
valuePath.Append(Parent.ToString());
的DataRow [] drPar;
而(真)
{
drPar = dt.Select(MENUID =+ predecessor);
如果(drPar [0] [PARENTID]。的ToString()。等于(0))// **索引超出范围异常**
打破;
valuePath.Insert(0,'/');
valuePath.Insert(0,drPar [0] [PARENTID]的ToString());
predecessor = Convert.ToInt32(drPar [0] [PARENTID]的ToString());
}
返回valuePath.ToString();
} }
}
我有两个错误从这code:
- 索引超出范围异常
-
的NullReferenceException
由code处理
我已经指出,产生错误的code。
这是我的数据库标头。
MENUID MENUNAME MenuLocation PARENTID价值
1 Parent1 NULL 0 P1
2 Parent2 NULL 0 P2
3 Parent3 NULL 0 P3
11 SubMenuItem1 NULL 1 S1
12 SubMenuItem2 NULL 1 S1
21 SubMenuItem3 NULL 2 S1
111 SubSubMenuItem4 NULL 1 SS1
211 SubSubMenuItem5 NULL 2 SS1
我已经得到了菜单中的项目数量的任何级别的解决方案。
使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用的System.Web;
使用System.Web.UI程序;
使用System.Web.UI.WebControls;
使用System.Data这;
使用System.Data.SqlClient的;
使用System.Configuration;菜单驱动的命名空间
{
公共部分类AnotherMenuTest:System.Web.UI.Page
{
保护无效的Page_Load(对象发件人,EventArgs的发送)
{
populateMenuItem();
}
私人无效populateMenuItem()
{ 数据表menuData = GetMenuData();
AddTopMenuItems(menuData); } 私人数据表GetMenuData()
{
使用(SqlConnection的CON =新的SqlConnection(ConfigurationManager.ConnectionStrings [ServerString]。的ConnectionString))
{
使用(CMD的SqlCommand =新的SqlCommand(SELECT MENUID,MENUNAME,PARENTID FROM的MenuItemsCON))
{
SqlDataAdapter的大=新SqlDataAdapter的(CMD);
DataTable的DT =新的DataTable();
da.Fill(DT);
返回DT;
} }
} ///过滤数据以只获得具有行
///空PARENTID(这将亮起顶级菜单项) 私人无效AddTopMenuItems(数据表menuData)
{
数据视图查看=新的数据视图(menuData);
view.RowFilter =PARENTID = 0;
的foreach(鉴于DataRowView的行)
{
菜单项newMenuItem =新菜单项(行[菜单名称]的ToString(),行[MENUID]的ToString());
menuBar.Items.Add(newMenuItem);
AddChildMenuItems(menuData,newMenuItem);
} } //这个code用于递归由PARENTID过滤添加子菜单项 私人无效AddChildMenuItems(数据表menuData,菜单项parentMenuItem)
{
数据视图查看=新的数据视图(menuData);
view.RowFilter =PARENTID =+ parentMenuItem.Value;
的foreach(鉴于DataRowView的行)
{
菜单项newMenuItem =新菜单项(行[菜单名称]的ToString(),行[MENUID]的ToString());
parentMenuItem.ChildItems.Add(newMenuItem);
AddChildMenuItems(menuData,newMenuItem);
}
}
}
}
感谢KnowledgeSeeker和安东尼奥您的答复。
和数据库是这样的。
这是我的数据库标头。
MENUID MENUNAME MenuLocation PARENTID
1 Parent1 NULL 0
2 Parent2 NULL 0
3 Parent3 NULL 0
11 SubMenuItem1 NULL 1
12 SubMenuItem2 NULL 1
21 SubMenuItem3 NULL 2
111 SubSubMenuItem4 NULL 1
211 SubSubMenuItem5 NULL 2
解决办法是:
设置数据库自身内的父节点。
例如:插入dbo.MenuItems值(333,'SubSubMenuItem8',NULL,21)
这将设置 SubSubMenuItem8
作为子父节点 SubMenuItem3
I'm trying to get the menu items from the SQL Server 2008 database.
Ive tried something like this as I Googled around and found these tutorials first and second:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Text;
namespace MenuDriven
{
public partial class MenuDB : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
menuBar.MaximumDynamicDisplayLevels = 3;
if (!IsPostBack)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString);
con.Open();
DataSet ds = new DataSet();
DataTable dt = new DataTable();
string sql = "Select * from MenuItems";
SqlDataAdapter da = new SqlDataAdapter(sql, con);
da.Fill(ds);
dt = ds.Tables[0];
DataRow[] drowpar = dt.Select("ParentID=" + 0);
foreach (DataRow dr in drowpar)
{
menuBar.Items.Add(new MenuItem(dr["MenuName"].ToString(),
dr["MenuID"].ToString(), "",
dr["MenuLocation"].ToString()));
}
foreach (DataRow dr in dt.Select("ParentID >" + 0))
{
MenuItem mnu = new MenuItem(dr["MenuName"].ToString(), dr["MenuID"].ToString(),
"", dr["MenuLocation"].ToString());
//Code for Multiple Menu Levels
string valuePath = getValuePath(Convert.ToInt32(dr["ParentID"].ToString()), dt);
//menuBar.FindItem(dr["ParentID"].ToString()).ChildItems.Add(mnu);
menuBar.FindItem(valuePath).ChildItems.Add(mnu);**NullReferenceException was handled by the code**
//End Code for Multiple Menu Levels
}
con.Close();
}
}
private string getValuePath(Int32 Parent, DataTable dt)
{
int predecessor = Parent;
StringBuilder valuePath = new StringBuilder();
valuePath.Append(Parent.ToString());
DataRow[] drPar;
while (true)
{
drPar = dt.Select("MenuID=" + predecessor);
if (drPar[0]["ParentID"].ToString().Equals("0"))**//Index out of range exception**
break;
valuePath.Insert(0, '/');
valuePath.Insert(0, drPar[0]["ParentID"].ToString());
predecessor = Convert.ToInt32(drPar[0]["ParentID"].ToString());
}
return valuePath.ToString();
}
}
}
I've two errors from this code:
- Index out of range exception
NullReferenceException
was handled by the code
I've pointed out the code that generated the error.
This is my database along with headers.
MenuID MenuName MenuLocation ParentID Value
1 Parent1 NULL 0 p1
2 Parent2 NULL 0 p2
3 Parent3 NULL 0 p3
11 SubMenuItem1 NULL 1 s1
12 SubMenuItem2 NULL 1 s1
21 SubMenuItem3 NULL 2 s1
111 SubSubMenuItem4 NULL 1 ss1
211 SubSubMenuItem5 NULL 2 ss1
I've got solution for any number of levels in the Menu Items.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace MenuDriven
{
public partial class AnotherMenuTest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
populateMenuItem();
}
private void populateMenuItem()
{
DataTable menuData = GetMenuData();
AddTopMenuItems(menuData);
}
private DataTable GetMenuData()
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ServerString"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("SELECT MenuID,MenuName,ParentID FROM MenuItems", con))
{
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
}
}
/// Filter the data to get only the rows that have a
/// null ParentID (This will come on the top-level menu items)
private void AddTopMenuItems(DataTable menuData)
{
DataView view = new DataView(menuData);
view.RowFilter = "ParentID = 0";
foreach (DataRowView row in view)
{
MenuItem newMenuItem = new MenuItem(row["MenuName"].ToString(), row["MenuID"].ToString());
menuBar.Items.Add(newMenuItem);
AddChildMenuItems(menuData, newMenuItem);
}
}
//This code is used to recursively add child menu items by filtering by ParentID
private void AddChildMenuItems(DataTable menuData, MenuItem parentMenuItem)
{
DataView view = new DataView(menuData);
view.RowFilter = "ParentID=" + parentMenuItem.Value;
foreach (DataRowView row in view)
{
MenuItem newMenuItem = new MenuItem(row["MenuName"].ToString(), row["MenuID"].ToString());
parentMenuItem.ChildItems.Add(newMenuItem);
AddChildMenuItems(menuData, newMenuItem);
}
}
}
}
Thanks for KnowledgeSeeker and Antonio for your replies.
and the Database is like this.
This is my database along with headers.
MenuID MenuName MenuLocation ParentID
1 Parent1 NULL 0
2 Parent2 NULL 0
3 Parent3 NULL 0
11 SubMenuItem1 NULL 1
12 SubMenuItem2 NULL 1
21 SubMenuItem3 NULL 2
111 SubSubMenuItem4 NULL 1
211 SubSubMenuItem5 NULL 2
The Solution is : Set the parent nodes within the database itself.
Ex: insert into dbo.MenuItems values(333,'SubSubMenuItem8',NULL,21)
This will set the SubSubMenuItem8
as a child to Parent Node SubMenuItem3
这篇关于获取菜单项在asp.net数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!