前台.aspx文件:
<asp:TreeView ID="tvMenu" runat="server" ImageSet="News" ShowLines="true" ExpandDepth="1" Target="middle" Height="295px" width="145px">
<ParentNodeStyle Font-Bold="False" ForeColor="Black" />
<HoverNodeStyle Font-Underline="True" ForeColor="Blue" />
<SelectedNodeStyle Font-Underline="True" HorizontalPadding="0px" VerticalPadding="0px" ForeColor="Green" ImageUrl="~/images/login_bg.gif" />
<NodeStyle Font-Names="Tahoma" Font-Size="8pt" ForeColor="DarkBlue" HorizontalPadding="5px" NodeSpacing="0px" VerticalPadding="0px" />
<RootNodeStyle BorderStyle="Dashed" />
</asp:TreeView>
表:
CREATE TABLE [sa_sysmenu] (
[id] [int] IDENTITY (1, 1) NOT NULL ,
[parent_id] [int] NULL ,
[position] [int] NULL ,
[step] [int] NULL ,
[name] [nvarchar] (255) COLLATE Chinese_PRC_CI_AS NULL ,
[path] [nvarchar] (255) COLLATE Chinese_PRC_CI_AS NULL ,
[description] [nvarchar] (255) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO
后台.aspx.cs文件:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
public partial class admbhbk_Power_PowerInfo : System.Web.UI.Page
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLConn"].ConnectionString);
DataSet ds;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
conn.Open();
this.createDataSet();
conn.Close();
this.InitTree(tvMenu.Nodes, "0");
}
}
private DataSet createDataSet()
{
ds = new DataSet();
string sqlstr = "select * from sa_sysmenu";
SqlDataAdapter cmdSelect = new SqlDataAdapter(sqlstr, conn);
cmdSelect.Fill(ds, "sa_sysmenu");
return ds;
}
protected void InitTree(TreeNodeCollection Nds, string parentID) //用递归方法动态生成节点
{
DataView dv = new DataView();
TreeNode tmpNode;
dv.Table = ds.Tables["sa_sysmenu"];
dv.RowFilter = "parent_id=" + "'" + parentID + "'";
foreach (DataRowView drv in dv)
{
tmpNode = new TreeNode();
tmpNode.Value = drv["id"].ToString();
tmpNode.Text = drv["name"].ToString();
tmpNode.NavigateUrl = drv["path"].ToString();
Nds.Add(tmpNode);
this.InitTree(tmpNode.ChildNodes, tmpNode.Value);
}
}
}
asp.net TreeView绑定数据库(测试成功)
最新推荐文章于 2022-07-25 13:14:02 发布