ASP.NET TreeView讀取數(shù)據(jù)庫實(shí)例
前臺(tái):
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="TreeView._Default" %>
<!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></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TreeView ID="TreeView1" runat="server" ShowLines="True">
</asp:TreeView>
</div>
</form>
</body>
</html>
后臺(tái):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace TreeView
{
public partial class _Default : System.Web.UI.Page
{
public static string st = ConfigurationManager.ConnectionStrings["sql"].ToString();
private DataTable dts=new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
dts = CreateTable();
CreateNode();
}
}
public void CreateNode()
{
DataRow[] dr = dts.Select("ParentID=0");
if(dr.Length>0)
{
foreach(DataRow drr in dr)
{
TreeNode tn = new TreeNode();
tn.Value = drr["MenuID"].ToString();
tn.Text = drr["MenuName"].ToString();
tn.Expanded = false;
tn.SelectAction = TreeNodeSelectAction.Expand;
TreeView1.Nodes.Add(tn);
CreateChild(tn,dts);
}
}
else
{
TreeNode t=new TreeNode();
t.Value="空";
t.Text="空";
t.Expanded=false;
t.SelectAction=TreeNodeSelectAction.Expand;
TreeView1.Nodes.Add(t);
}
}
public void CreateChild(TreeNode tnn, DataTable dtt)
{
DataRow[] dr = dtt.Select("ParentID=" + tnn.Value);
if (dr.Length > 0)
{
foreach (DataRow drw in dr)
{
TreeNode ts = new TreeNode();
ts.Value = drw["MenuID"].ToString();
ts.Text = drw["MenuName"].ToString();
ts.SelectAction = TreeNodeSelectAction.Expand;
ts.Expanded = false;
tnn.ChildNodes.Add(ts);
CreateChild(ts, dtt);
}
}
}
public DataTable CreateTable()
{
DataTable d = new DataTable();
using(SqlConnection sql=new SqlConnection(st))
{
SqlCommand sq=new SqlCommand("select * from TreeViewName",sql);
SqlDataAdapter sda=new SqlDataAdapter();
sda.SelectCommand = sq;
sda.Fill(d);
}
return d;
}
}
}
相關(guān)文章
asp.net結(jié)合aspnetpager使用SQL2005的存儲(chǔ)過程分頁
項(xiàng)目中用到了,同事阿春寫了例子,并在實(shí)際項(xiàng)目中使用了,記錄下。感謝春哥的無私奉獻(xiàn)。2009-07-07
使用dotnet-dump 查找 .net core 3.0 占用CPU 100%的原因解析
這篇文章主要介紹了使用dotnet-dump 查找 .net core 3.0 占用CPU 100%的原因,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-06-06
Asp.Net MVC4通過id更新表單內(nèi)容的思路詳解
一個(gè)表單一旦創(chuàng)建完,其中大部分的字段便不可再編輯。只能編輯其中部分字段。下面通過本文給大家分享Asp.Net MVC4通過id更新表單內(nèi)容的思路詳解,需要的朋友參考下吧2017-07-07
Entity?Framework?Core基于數(shù)據(jù)模型創(chuàng)建數(shù)據(jù)庫
這篇文章介紹了Entity?Framework?Core基于數(shù)據(jù)模型創(chuàng)建數(shù)據(jù)庫的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04
asp.net 處理F5刷新頁面重復(fù)提交頁面的一個(gè)思路
當(dāng)提交完一個(gè)頁面后,如果我們?cè)俅吸c(diǎn)擊F5刷新該頁面的話,會(huì)彈出一個(gè)提示,提示我們?nèi)绻^續(xù),則會(huì)重新發(fā)送提交我們剛才提交的內(nèi)容,要是類似付款或一次性的操作,我們不應(yīng)該這樣操作,否則會(huì)造成重復(fù)提交的問題。2010-02-02
ASP.net(C#)實(shí)現(xiàn)簡易聊天室功能
這篇文章主要為大家詳細(xì)介紹了ASP.net實(shí)現(xiàn)簡易聊天室功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
基于.net core微服務(wù)的另一種實(shí)現(xiàn)方法
這篇文章主要給大家介紹了基于.net core微服務(wù)的另一種實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07
asp.net導(dǎo)出excel的簡單方法實(shí)例
這篇文章主要介紹了asp.net導(dǎo)出excel的簡單方法實(shí)例,需要的朋友可以參考下2014-02-02

