前台代码 注意 一定要加上Target="contentFrame"否则跳转的时候 跳不到内容框架 Page Language="C#" AutoEventWireup="true" CodeFile="Left.aspx.cs" Inherits="Left" %> <html> <head id="Head1" runat="server"> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <link href="../Style/Style.css" rel="stylesheet" type="text/css" /> </head> <body leftmargin="0" topmargin="0" bgcolor="#f7faff"> <form id="form1" runat="server"> <table width="200" border="0" cellpadding="0" cellspacing="0"> <tr> <td> <table cellpadding="0" cellspacing="0" border="0" style="color: White; font-family: 宋体, Arial; font-size: 12px;"> <tr> <td align="left" background="../images/czy.png" width="200" height="30"> 登录人: <asp:Label ID="lbLoginUser" runat="server"></asp:Label> </td> </tr> </table> </td> </tr> </table> <table width="200" border="0" cellpadding="0" cellspacing="0"> <tr> <td align="left" valign="top"> <div style="height: 480px; overflow:scroll; scrollbar-face-color: #DBEBFE; scrollbar-shadow-color: #B8D6FA; scrollbar-highlight-color: #FFFFFF; scrollbar-3dlight-color: #DBEBFE; scrollbar-darkshadow-color: #458CE4; scrollbar-track-color: #FFFFFF; scrollbar-arrow-color: #458CE4"> <asp:TreeView ID="tvMenu1" runat="server" EnableViewState="False" BackColor="#f7faff" ForeColor="Black" ShowLines="True" Target="contentFrame" ExpandDepth="1"> </form> </body> </html>
后台代码 using System; using System.Collections.Generic; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Configuration; using System.Data; using System.IO; using NetBuild.Common; using NetBuild.Models; using NetBuild.Bussiness; using System.Linq; public partial class Left : System.Web.UI.Page { /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { TreeNode tmpNode; lbLoginUser.Text = Session["LoginName"].ToString(); AddTreeNode(-1, (System.Web.UI.WebControls.TreeNode)null); } } public DataTable bind() { string str1 = "select * from Common_Menu "; DataSet myds = new DataSet(); myds = SQLHelper.GetDataSet(str1); return myds.Tables[0]; } protected void AddTreeNode(int ParentMenuID, System.Web.UI.WebControls.TreeNode pNode) { DataTable dt = bind(); DataView dv = new DataView(dt); //过滤parentId,得到当前节点的所有子节点 dv.RowFilter = "ParentMenuID=" + ParentMenuID; //foreach (DataRow b in dt.Rows) //{ // ParentMenuID = Convert.ToInt32(b["ParentMenuID"].ToString()); //} foreach (DataRowView drv in dv) { if (ParentMenuID == -1) { System.Web.UI.WebControls.TreeNode tn1 = new System.Web.UI.WebControls.TreeNode(); tn1.Text = drv["MenuName"].ToString(); //节点上要显示的名称 tn1.Value = drv["MenuID"].ToString(); tn1.ImageUrl = drv["Icon"].ToString(); tn1.NavigateUrl = drv["MenuUrl"].ToString(); //点击节点名称,跳转到指定url页面 tvMenu1.Nodes.Add(tn1); //将根节点加入到TreeView中去 tn1.Expanded = true; //tn1.SelectAction = TreeNodeSelectAction.Expand; tn1.ShowCheckBox = false; //递归调用 AddTreeNode(Int32.Parse(drv["MenuID"].ToString()), tn1); } else { System.Web.UI.WebControls.TreeNode tn2 = new System.Web.UI.WebControls.TreeNode(); tn2.Text = drv["MenuName"].ToString(); tn2.Value = drv["MenuID"].ToString(); tn2.ImageUrl = drv["Icon"].ToString(); tn2.NavigateUrl = drv["MenuUrl"].ToString(); pNode.ChildNodes.Add(tn2); tn2.ShowCheckBox = false; tn2.Expanded = true; //递归调用 AddTreeNode(Int32.Parse(drv["MenuID"].ToString()), tn2); } } } }