感觉看起来有点晕!下面是我自己做了一个新闻发布系统的后台管理项目菜单,遇到动态treeview,我是这么做的:
说明:我采用简单的三层架构来写的,带model的那种
1.首先在数据库先建表,然后建视图
这是model层代码,由于时间有点久,一时没找到,相信亲根据model也可以建出表和视图:
using System;
using System.Collections.Generic;
using System.Text;
namespace Model
{
public class viewUserRightInfo
{
private string rolename;
private int roleid;
private int _funid;
public int funid
{
get { return _funid; }
set { _funid = value; }
}
private string _funname;
public string funname
{
get { return _funname; }
set { _funname = value; }
}
private string _funurl;
public string funurl
{
get { return _funurl; }
set { _funurl = value; }
}
private int _displayid;
public int displayid
{
get { return _displayid; }
set { _displayid = value; }
}
private int _fatherid;
public int fatherid
{
get { return _fatherid; }
set { _fatherid = value; }
}
private int userid;
public int UserId
{
get { return userid; }
set { userid = value; }
}
private string username;
public string UserName
{
get { return username; }
set { username = value; }
}
public int RoleId
{
get { return roleid; }
set { roleid = value; }
}
public string RoleName
{
get { return rolename; }
set { rolename = value; }
}
}
}
2.DAL层代码
public class viewUserRightInfoService
{
public static List<viewUserRightInfo> GetAllParentNodeInfoByUserId(int funid , int userid)
{
string sql = string.Format("select * from viewUserRightInfo where fatherid={0} and UserId ={1}",funid, userid);
return datatables(sql);
}
public static List<viewUserRightInfo> select_by_roleid(int roleid)
{
string sql = string.Format("select * from viewUserRightInfo where RoleId ={0}", roleid);
return datatables(sql);
}
public static List<viewUserRightInfo> select_by_userid(int userid)
{
string sql = string.Format("select * from viewUserRightInfo where UserId ={0}", userid);
return datatables(sql);
}
public static viewUserRightInfo select_by_username(string username)
{
string sql = string.Format("select * from viewUserRightInfo where UserName ='{0}'", username);
return datarow(sql);
}
public static List<viewUserRightInfo> datatables(string sql)
{
List<viewUserRightInfo> list = new List<viewUserRightInfo>();
viewUserRightInfo view = null;
DataTable dt = SqlHelper.GetDataSet(sql);
if (dt != null)
{
foreach (DataRow dr in dt.Rows)
{
view = new viewUserRightInfo();
view.RoleName = (string)dr["RoleName"];
view.RoleId = (int)dr["roleid"];
view.funid = (int)dr["funid"];
view.funname = (string)dr["funname"].ToString();
view.funurl = (string)dr["funurl"].ToString();
view.displayid = (int)dr["displayid"];
view.fatherid = (int)dr["fatherid"];
view.UserId = (int)dr["UserId"];
view.UserName = (string)dr["UserName"].ToString();
list.Add(view);
}
return list;
}
else
{
return null;
}
}
public static viewUserRightInfo datarow(string sql)
{
viewUserRightInfo view = null;
DataRow dr = SqlHelper.GetDataSet(sql).Rows[0];
if (dr != null)
{
view = new viewUserRightInfo();
view.RoleName = (string)dr["RoleName"];
view.RoleId = (int)dr["roleid"];
view.funid = (int)dr["funid"];
view.funname = (string)dr["funname"].ToString();
view.funurl = (string)dr["funurl"].ToString();
view.displayid = (int)dr["displayid"];
view.fatherid = (int)dr["fatherid"];
view.UserId = (int)dr["UserId"];
view.UserName = (string)dr["UserName"].ToString();
return view;
}
else
{
return null;
}
}
}
3.BLL层代码
public class viewUserRightInfoManager
{
public static List<viewUserRightInfo> GetAllParentNodeInfoByUserId(int funid, int userid)
{
return viewUserRightInfoService.GetAllParentNodeInfoByUserId(funid, userid);
}
public static List<viewUserRightInfo> select_by_roleid(int roleid)
{
return viewUserRightInfoService.select_by_roleid(roleid);
}
public static List<viewUserRightInfo> select_by_userid(int userid)
{
return viewUserRightInfoService.select_by_userid(userid);
}
public static viewUserRightInfo select_by_username(string username)
{
return viewUserRightInfoService.select_by_username(username);
}
}
4.后台代码
private TreeNode CreatTreaNode(string strText, int strId, string strUrl, string strImage)
{
TreeNode newNode = new TreeNode();
newNode.Text = strText.ToString();
newNode.Value = strId.ToString();
newNode.NavigateUrl = strUrl;
newNode.ImageUrl = strImage;
return newNode;
}
private void Display(User user)
{
int roleid = (int)user.RoleId;
TreeView1.Nodes.Clear();
List<viewUserRightInfo> parent =viewUserRightInfoManager.GetAllParentNodeInfoByUserId(0, (int)user.UserId);
foreach (viewUserRightInfo sf in parent)
{
int nodeid = sf.funid;
string funname = sf.funname;
string image = "../Image/plugin_add.png";
TreeNode fatherNode = this.CreatTreaNode(funname, nodeid, "", image);
CreateChildTree(nodeid, (int)user.UserId, fatherNode);
TreeView1.Nodes.Add(fatherNode);
}
}
private void CreateChildTree(int nodeid, int userid, TreeNode fatherNode)
{
List<viewUserRightInfo> child = viewUserRightInfoManager.GetAllParentNodeInfoByUserId(nodeid, userid);
foreach (viewUserRightInfo sfchild in child)
{
int id = sfchild.funid;
string childname = sfchild.funname;
string nodeurl = ResolveUrl(sfchild.funurl.Trim());
string image = "../Image/plugin_edit.png";
TreeNode childNode = this.CreatTreaNode(childname, nodeid, nodeurl, image);
CreateChildTree(id, userid, fatherNode);
AddTree(fatherNode, childNode);
}
}
private void AddTree(TreeNode fathernode, TreeNode chilenode)
{
fathernode.ChildNodes.Add(chilenode);
}
//List<Roleright> list = new List<Roleright>();
//list = RolerightManager.select_by_roleid(roleid);
//for (int i = 0; i < list.Count; i++)
//{
// int nodeid = list[i].Funid;
// List<Funinfo> funlist = Funinfomessage.select_by_funid(nodeid);
// for (int j = 0; j < funlist.Count; j++)
// {
// TreeNode fathernode = CreatNode(funlist[j].Funname.ToString(), "");
// int fatherid = funlist[j].Funid;
// List<Funinfo> fulist = Funinfomessage.select_by_fatherid(fatherid);
// for (int e = 0; e < fulist.Count; e++)
// {
// TreeNode chilenode = CreatNode(fulist[e].Funname.ToString(), fulist[e].Funurl.ToString());
// fathernode.ChildNodes.Add(chilenode);
// }
// this.TreeView1.Nodes.Add(fathernode);
// }
//}
//}
private TreeNode CreatNode(string strtext, string strurl)
{
TreeNode node = new TreeNode();
node.Text = strtext;
node.NavigateUrl = strurl;
return node;
}
} 以上有什么问题和建议请回复,我一直都用这个很好用的,还可以对这些节点进行管理,增删改
|