TreeView 和 Menu 的用法
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
{
变量 构造函数
目录树(TreeView)
导航菜单(Menu)
}//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
{
变量 构造函数
目录树(TreeView)
导航菜单(Menu)
}//class end
}