[转帖]treeview的用法
TreeView和Menu都是用于导航,且与数据库绑定时的用法也极为相近,下面给出两个实例,请比较业务层的绑定方法。
1. Tree.aspx
1. Tree.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Tree.aspx.cs" Inherits="NavTree"
EnableViewState="false" %>
<!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>LeftMenu_Tree</title>
</head>
<body bgcolor="#DDEDFD">
<form id="form1" runat="server">
<asp:LinkButton ID="lbtExpand" runat="server" OnClick="lbtExpand_Click" ToolTip="全部展开">
全部展开
</asp:LinkButton>
<asp:LinkButton ID="lbtCollapse" runat="server" OnClick="lbtCollapse_Click" Visible="false">
全部收缩
</asp:LinkButton>
<asp:TreeView ID="tvMenu" SkinID="tvClass" runat="server" ShowLines="true" NodeWrap="false">
</asp:TreeView>
</form>
</body>
</html>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Tree.aspx.cs" Inherits="NavTree"
EnableViewState="false" %>
<!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>LeftMenu_Tree</title>
</head>
<body bgcolor="#DDEDFD">
<form id="form1" runat="server">
<asp:LinkButton ID="lbtExpand" runat="server" OnClick="lbtExpand_Click" ToolTip="全部展开">
全部展开
</asp:LinkButton>
<asp:LinkButton ID="lbtCollapse" runat="server" OnClick="lbtCollapse_Click" Visible="false">
全部收缩
</asp:LinkButton>
<asp:TreeView ID="tvMenu" SkinID="tvClass" runat="server" ShowLines="true" NodeWrap="false">
</asp:TreeView>
</form>
</body>
</html>
2. Tree.aspx.cs
using System;
using System.Data;
using System.Data.SqlClient;
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;
public partial class NavTree : System.Web.UI.Page
{
Sys.BLL.Navigation myBiz = new Sys.BLL.Navigation();
private void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
{
BindTree();
}
}
protected void BindTree()
{
//根据当前用户ID加载目录树
myBiz.BindTree(tvMenu, Framework.Globals.CurrentUser.UserID);
}
//全部展开节点
protected void lbtExpand_Click(object sender, EventArgs e)
{
tvMenu.ExpandAll();
lbtExpand.Visible = false;
lbtCollapse.Visible = true;
}
//全部折叠节点
protected void lbtCollapse_Click(object sender, EventArgs e)
{
tvMenu.CollapseAll();
lbtExpand.Visible = true;
lbtCollapse.Visible = false;
}
}
using System.Data;
using System.Data.SqlClient;
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;
public partial class NavTree : System.Web.UI.Page
{
Sys.BLL.Navigation myBiz = new Sys.BLL.Navigation();
private void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
{
BindTree();
}
}
protected void BindTree()
{
//根据当前用户ID加载目录树
myBiz.BindTree(tvMenu, Framework.Globals.CurrentUser.UserID);
}
//全部展开节点
protected void lbtExpand_Click(object sender, EventArgs e)
{
tvMenu.ExpandAll();
lbtExpand.Visible = false;
lbtCollapse.Visible = true;
}
//全部折叠节点
protected void lbtCollapse_Click(object sender, EventArgs e)
{
tvMenu.CollapseAll();
lbtExpand.Visible = true;
lbtCollapse.Visible = false;
}
}
3. Menu.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Menu.aspx.cs" Inherits="NavMenu" %>
<!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>Top</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Menu ID="Menu1" runat="server" BackColor="#f1f1f1" ForeColor="Black" Font-Size="12px"
Font-Names="宋体" BorderColor="background" BorderStyle="Solid" BorderWidth="0px"
StaticSubMenuIndent="10px" Height="20px" Orientation="Horizontal" StaticEnableDefaultPopOutImage="False">
<StaticMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" BorderWidth="1px"
BorderColor="#f1f1f1" />
<StaticHoverStyle BackColor="#cccccc" BorderColor="background" BorderStyle="Solid"
BorderWidth="1px" />
<DynamicHoverStyle BackColor="#C6DDFE" BorderColor="background" BorderStyle="Solid"
BorderWidth="0px" />
<DynamicMenuStyle BackColor="Menu" BorderStyle="Outset" BorderWidth="1px" HorizontalPadding="2px"
VerticalPadding="0px" BorderColor="MenuBar" />
<DynamicMenuItemStyle HorizontalPadding="2px" VerticalPadding="2px" />
</asp:Menu>
</form>
</body>
</html>
<!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>Top</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Menu ID="Menu1" runat="server" BackColor="#f1f1f1" ForeColor="Black" Font-Size="12px"
Font-Names="宋体" BorderColor="background" BorderStyle="Solid" BorderWidth="0px"
StaticSubMenuIndent="10px" Height="20px" Orientation="Horizontal" StaticEnableDefaultPopOutImage="False">
<StaticMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" BorderWidth="1px"
BorderColor="#f1f1f1" />
<StaticHoverStyle BackColor="#cccccc" BorderColor="background" BorderStyle="Solid"
BorderWidth="1px" />
<DynamicHoverStyle BackColor="#C6DDFE" BorderColor="background" BorderStyle="Solid"
BorderWidth="0px" />
<DynamicMenuStyle BackColor="Menu" BorderStyle="Outset" BorderWidth="1px" HorizontalPadding="2px"
VerticalPadding="0px" BorderColor="MenuBar" />
<DynamicMenuItemStyle HorizontalPadding="2px" VerticalPadding="2px" />
</asp:Menu>
</form>
</body>
</html>
4. Menu.aspx.cs
using System;
using System.Data;
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;
public partial class NavMenu : System.Web.UI.Page
{
Sys.BLL.Navigation myBiz = new Sys.BLL.Navigation();
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
myBiz.BindMenu(Menu1);
}
}
}
using System.Data;
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;
public partial class NavMenu : System.Web.UI.Page
{
Sys.BLL.Navigation myBiz = new Sys.BLL.Navigation();
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
myBiz.BindMenu(Menu1);
}
}
}
5. 业务层:App_Code/Sys.BLL.Navigation.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;
namespace Sys.BLL
{
/// <summary>
/// 系统导航 业务层
/// </summary>
public class Navigation : Framework.BLL.BizObject
{
#region 变量 构造函数
Sys.DAL.Navigation myData = new Sys.DAL.Navigation();
string ImagePath = Framework.Globals.WebRoot + "Images/";
/// <summary>
/// 系统导航
/// </summary>
public Navigation()
{ }
#endregion
#region 目录树(TreeView)
DataTable ds_TreeView;
/// <summary>
/// 根据当前用户ID加载目录树
/// </summary>
public void BindTree(TreeView tvMenu, int userID)
{
ds_TreeView = myData.BindTree(userID).Tables[0];
if (ds_TreeView.Rows.Count > 0)
BindTree(tvMenu, 0, (TreeNode)null);
}
private void BindTree(TreeView tvMenu, int ParentID, TreeNode pNode)
{
string NodeId = "MenuID";
string NodeName = "MenuName";
string ParentId = "ParentID";
string Url = "Url";
string Icon = "Icon";
DataView dvTree = new DataView(ds_TreeView);
//过滤ParentId,得到当前的所有子节点
dvTree.RowFilter = ParentId + " = " + ParentID;
foreach (DataRowView drv in dvTree)
{
TreeNode Node = new TreeNode();
if (pNode == null)
{ //添加根节点
string nodename = drv[NodeName].ToString().Trim();
string nodeid = drv[NodeId].ToString().Trim();
Node.Text = nodename;
Node.ToolTip = nodename;
Node.Value = nodeid;
Node.Target = "main";
if (drv[Icon].ToString().Trim() == string.Empty)
{ Node.ImageUrl = ImagePath + "menu/ie.gif"; }
else
{ Node.ImageUrl = ImagePath + "menu/" + drv[Icon].ToString().Trim(); }
//导航模式 & 选择模式
//要使一个节点处于导航模式,请将该节点的 NavigateUrl 属性值设置为空字符串 ("") 以外的值
//若要使节点处于选择模式,请将节点的 NavigateUrl 属性设置为空字符串。
Node.NavigateUrl = "";
Node.SelectAction = TreeNodeSelectAction.Expand;
Node.Expanded = false;
Node.PopulateOnDemand = false;
tvMenu.Nodes.Add(Node); //***注意区别:根节点
BindTree(null, Int32.Parse(nodeid), Node); //再次递归
}
else
{ //?添加当前节点的子节点
string nodename = drv[NodeName].ToString().Trim();
string nodeid = drv[NodeId].ToString().Trim();
Node.Text = nodename;
Node.ToolTip = nodename;
Node.Value = nodeid;
Node.Target = "main";
if (drv[Icon].ToString().Trim() == string.Empty)
{ Node.ImageUrl = ImagePath + "menu/ie.gif"; }//默认图标
else
{ Node.ImageUrl = ImagePath + "menu/" + drv[Icon].ToString().Trim(); }
Node.NavigateUrl = drv[Url].ToString();
Node.SelectAction = TreeNodeSelectAction.Expand;
Node.Expanded = false;
Node.PopulateOnDemand = false;
pNode.ChildNodes.Add(Node); //***注意区别:子节点
BindTree(null, Int32.Parse(nodeid), Node); //再次递归
}
}
}
#endregion
#region 导航菜单(Menu)
DataTable ds_BindMenu;
/// <summary>
/// 加载获取导航菜单
/// </summary>
public void BindMenu(Menu tvMenu)
{
ds_BindMenu = myData.BindMenu().Tables[0];
if (ds_BindMenu.Rows.Count > 0)
BindMenu(tvMenu, 0, (MenuItem)null);
}
/// <summary>
/// 加载获取导航菜单
/// </summary>
private void BindMenu(Menu tvMenu, int ParentID, MenuItem pNode)
{
DataView dvTree = new DataView(ds_BindMenu);
dvTree.RowFilter = "ParentId =" + ParentID;//过滤ParentId,得到当前的所有子节点
foreach (DataRowView drv in dvTree)
{
int id = Convert.ToInt32(drv["ID"]);
int parentID = Convert.ToInt32(drv["ParentID"]);
string text = drv["Text"].ToString();
string value = drv["Value"] == DBNull.Value ? string.Empty : drv["Value"].ToString();
string imageUrl = drv["ImageUrl"] == DBNull.Value ? string.Empty : drv["ImageUrl"].ToString();
string navigateUrl = drv["NavigateUrl"] == DBNull.Value ? string.Empty : drv["NavigateUrl"].ToString();
string target = drv["Target"] == DBNull.Value ? string.Empty : drv["Target"].ToString();
MenuItem item = new MenuItem();
if (pNode == null)
{ //添加根节点
item.Text = text;
item.Value = value;
item.Target = target;
if (imageUrl == string.Empty)
{
item.ImageUrl = ImagePath + "winXP/winxp_folder.gif";
}
else
{
item.ImageUrl = imageUrl;
}
item.NavigateUrl = navigateUrl;
item.Selectable = true;
tvMenu.Items.Add(item); //***注意区别:根节点
BindMenu(null, id, item); //再次递归
}//检索
else
{ //?添加当前节点的子节点
item.Text = text;
item.Value = value;
item.Target = target;
if (imageUrl == string.Empty)
{
item.ImageUrl = ImagePath + "winXP/winxp_folder.gif";
}
else
{
item.ImageUrl = imageUrl;
}
item.NavigateUrl = navigateUrl;
item.Selectable = true;
pNode.ChildItems.Add(item); //***注意区别:子节点
BindMenu(null, id, item); //再次递归
}
}
}
#endregion
}//class end
}
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;
namespace Sys.BLL
{
/// <summary>
/// 系统导航 业务层
/// </summary>
public class Navigation : Framework.BLL.BizObject
{
#region 变量 构造函数
Sys.DAL.Navigation myData = new Sys.DAL.Navigation();
string ImagePath = Framework.Globals.WebRoot + "Images/";
/// <summary>
/// 系统导航
/// </summary>
public Navigation()
{ }
#endregion
#region 目录树(TreeView)
DataTable ds_TreeView;
/// <summary>
/// 根据当前用户ID加载目录树
/// </summary>
public void BindTree(TreeView tvMenu, int userID)
{
ds_TreeView = myData.BindTree(userID).Tables[0];
if (ds_TreeView.Rows.Count > 0)
BindTree(tvMenu, 0, (TreeNode)null);
}
private void BindTree(TreeView tvMenu, int ParentID, TreeNode pNode)
{
string NodeId = "MenuID";
string NodeName = "MenuName";
string ParentId = "ParentID";
string Url = "Url";
string Icon = "Icon";
DataView dvTree = new DataView(ds_TreeView);
//过滤ParentId,得到当前的所有子节点
dvTree.RowFilter = ParentId + " = " + ParentID;
foreach (DataRowView drv in dvTree)
{
TreeNode Node = new TreeNode();
if (pNode == null)
{ //添加根节点
string nodename = drv[NodeName].ToString().Trim();
string nodeid = drv[NodeId].ToString().Trim();
Node.Text = nodename;
Node.ToolTip = nodename;
Node.Value = nodeid;
Node.Target = "main";
if (drv[Icon].ToString().Trim() == string.Empty)
{ Node.ImageUrl = ImagePath + "menu/ie.gif"; }
else
{ Node.ImageUrl = ImagePath + "menu/" + drv[Icon].ToString().Trim(); }
//导航模式 & 选择模式
//要使一个节点处于导航模式,请将该节点的 NavigateUrl 属性值设置为空字符串 ("") 以外的值
//若要使节点处于选择模式,请将节点的 NavigateUrl 属性设置为空字符串。
Node.NavigateUrl = "";
Node.SelectAction = TreeNodeSelectAction.Expand;
Node.Expanded = false;
Node.PopulateOnDemand = false;
tvMenu.Nodes.Add(Node); //***注意区别:根节点
BindTree(null, Int32.Parse(nodeid), Node); //再次递归
}
else
{ //?添加当前节点的子节点
string nodename = drv[NodeName].ToString().Trim();
string nodeid = drv[NodeId].ToString().Trim();
Node.Text = nodename;
Node.ToolTip = nodename;
Node.Value = nodeid;
Node.Target = "main";
if (drv[Icon].ToString().Trim() == string.Empty)
{ Node.ImageUrl = ImagePath + "menu/ie.gif"; }//默认图标
else
{ Node.ImageUrl = ImagePath + "menu/" + drv[Icon].ToString().Trim(); }
Node.NavigateUrl = drv[Url].ToString();
Node.SelectAction = TreeNodeSelectAction.Expand;
Node.Expanded = false;
Node.PopulateOnDemand = false;
pNode.ChildNodes.Add(Node); //***注意区别:子节点
BindTree(null, Int32.Parse(nodeid), Node); //再次递归
}
}
}
#endregion
#region 导航菜单(Menu)
DataTable ds_BindMenu;
/// <summary>
/// 加载获取导航菜单
/// </summary>
public void BindMenu(Menu tvMenu)
{
ds_BindMenu = myData.BindMenu().Tables[0];
if (ds_BindMenu.Rows.Count > 0)
BindMenu(tvMenu, 0, (MenuItem)null);
}
/// <summary>
/// 加载获取导航菜单
/// </summary>
private void BindMenu(Menu tvMenu, int ParentID, MenuItem pNode)
{
DataView dvTree = new DataView(ds_BindMenu);
dvTree.RowFilter = "ParentId =" + ParentID;//过滤ParentId,得到当前的所有子节点
foreach (DataRowView drv in dvTree)
{
int id = Convert.ToInt32(drv["ID"]);
int parentID = Convert.ToInt32(drv["ParentID"]);
string text = drv["Text"].ToString();
string value = drv["Value"] == DBNull.Value ? string.Empty : drv["Value"].ToString();
string imageUrl = drv["ImageUrl"] == DBNull.Value ? string.Empty : drv["ImageUrl"].ToString();
string navigateUrl = drv["NavigateUrl"] == DBNull.Value ? string.Empty : drv["NavigateUrl"].ToString();
string target = drv["Target"] == DBNull.Value ? string.Empty : drv["Target"].ToString();
MenuItem item = new MenuItem();
if (pNode == null)
{ //添加根节点
item.Text = text;
item.Value = value;
item.Target = target;
if (imageUrl == string.Empty)
{
item.ImageUrl = ImagePath + "winXP/winxp_folder.gif";
}
else
{
item.ImageUrl = imageUrl;
}
item.NavigateUrl = navigateUrl;
item.Selectable = true;
tvMenu.Items.Add(item); //***注意区别:根节点
BindMenu(null, id, item); //再次递归
}//检索
else
{ //?添加当前节点的子节点
item.Text = text;
item.Value = value;
item.Target = target;
if (imageUrl == string.Empty)
{
item.ImageUrl = ImagePath + "winXP/winxp_folder.gif";
}
else
{
item.ImageUrl = imageUrl;
}
item.NavigateUrl = navigateUrl;
item.Selectable = true;
pNode.ChildItems.Add(item); //***注意区别:子节点
BindMenu(null, id, item); //再次递归
}
}
}
#endregion
}//class end
}