TreeView加载:
数据库部分:
TreeView 完全加载
显示:
代码清单:
//前台
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<asp:TreeView ID="TreeViewType" runat="server">
</asp:TreeView>
</form>
</body>
</html>
//====================================
后台
namespace WebFileUp
{
publicpartialclass WebFormTreeview : System.Web.UI.Page
{
DataTable dt =new DataTable();
protectedvoid Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DataInit();
LoadTree(this.TreeViewType, null, 0);
}
}
privatevoid DataInit()
{
string cmdTxt ="Select * From TreeView";
//打开数据库,获取类别信息
SqlConnection conn =new SqlConnection(@"Data Source=HAPPY-THINK\HAPPYMSSQLSERVER;Initial Catalog=test0530;Persist Security Info=True;User ID=sa;Password=happy");
SqlCommand comm =new SqlCommand(cmdTxt, conn);
SqlDataAdapter adap =new SqlDataAdapter(comm);
adap.Fill(dt);
conn.Close();
}
privatevoid LoadTree(TreeView tv, TreeNode tn, int parentid)
{
//读取ParentId等于传进来的Parentid的值
DataRow[] drs = dt.Select("ParentId="+ parentid);
foreach (DataRow dr in drs)
{
TreeNode tmptn =new TreeNode();
//将TypeName添加到节点上
tmptn.Text = dr["TypeName"].ToString();
//设置跳转页面
tmptn.NavigateUrl ="Default.aspx";
//如果tn为空,则没有子节点,将当前节点添加到树上
if (tn ==null)
{
tv.Nodes.Add(tmptn);
}
else
{
//否则则有子节点,将当前节点添加到子节点上
tn.ChildNodes.Add(tmptn);
}
//递归调用
LoadTree(tv, tmptn, Convert.ToInt32(dr["TypeId"]));
}
}
}
}
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<asp:TreeView ID="TreeViewType" runat="server">
</asp:TreeView>
</form>
</body>
</html>
//====================================
后台
namespace WebFileUp
{
publicpartialclass WebFormTreeview : System.Web.UI.Page
{
DataTable dt =new DataTable();
protectedvoid Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DataInit();
LoadTree(this.TreeViewType, null, 0);
}
}
privatevoid DataInit()
{
string cmdTxt ="Select * From TreeView";
//打开数据库,获取类别信息
SqlConnection conn =new SqlConnection(@"Data Source=HAPPY-THINK\HAPPYMSSQLSERVER;Initial Catalog=test0530;Persist Security Info=True;User ID=sa;Password=happy");
SqlCommand comm =new SqlCommand(cmdTxt, conn);
SqlDataAdapter adap =new SqlDataAdapter(comm);
adap.Fill(dt);
conn.Close();
}
privatevoid LoadTree(TreeView tv, TreeNode tn, int parentid)
{
//读取ParentId等于传进来的Parentid的值
DataRow[] drs = dt.Select("ParentId="+ parentid);
foreach (DataRow dr in drs)
{
TreeNode tmptn =new TreeNode();
//将TypeName添加到节点上
tmptn.Text = dr["TypeName"].ToString();
//设置跳转页面
tmptn.NavigateUrl ="Default.aspx";
//如果tn为空,则没有子节点,将当前节点添加到树上
if (tn ==null)
{
tv.Nodes.Add(tmptn);
}
else
{
//否则则有子节点,将当前节点添加到子节点上
tn.ChildNodes.Add(tmptn);
}
//递归调用
LoadTree(tv, tmptn, Convert.ToInt32(dr["TypeId"]));
}
}
}
}
TreeView按需加载
首先要设置ontreenodepopulate="TreeView1_TreeNodePopulate" ExpandDepth="0"
即,数据绑定是默认展开数的级别设置为0;双击事件TreeNodePopulat,即当数据填充是激发
注意,一定要在代码里设置节点的PopulateOnDemand属性为true,这样当点击展开时才能触发TreeNodePopulat事件,实现按需加载
代码清单:
View Code
//前台
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<asp:TreeView ID="TreeView1" runat="server"
ontreenodepopulate="TreeView1_TreeNodePopulate" ExpandDepth="0">
</asp:TreeView>
</form>
</body>
</html>
//====================================================
//后台代码
namespace WebFileUp
{
publicpartialclass WebFormTreeview2 : System.Web.UI.Page
{
/*
* 第一:设定目录树的展开深度为0
* 第二:当添加节点的时候,设定按需加载为真(判断是否有子节点,如果有的话,才设定为真)
* 第三:编写按需加载的事件,
* */
DataTable dt =new DataTable();
protectedvoid Page_Load(object sender, EventArgs e)
{
DataInit();
if (!Page.IsPostBack)
{
LoadTree(this.TreeView1, null, 0);
}
}
privatevoid DataInit()
{
string cmdTxt ="Select * From TreeView";
//打开数据库,获取类别信息
SqlConnection conn =new SqlConnection(@"Data Source=HAPPY-THINK\HAPPYMSSQLSERVER;Initial Catalog=test0530;Persist Security Info=True;User ID=sa;Password=happy");
SqlCommand comm =new SqlCommand(cmdTxt, conn);
SqlDataAdapter adap =new SqlDataAdapter(comm);
adap.Fill(dt);
conn.Close();
}
privatevoid DataLoad1()
{
}
privatevoid LoadTree(TreeView tv, TreeNode tn, int parentid)
{
DataRow[] drs = dt.Select("ParentId="+ parentid);
foreach (DataRow dr in drs)
{
TreeNode tmptn =new TreeNode();
tmptn.Text = dr["TypeName"].ToString();
tmptn.NavigateUrl ="Default.aspx";
tmptn.Target ="_blank";
//判断是否有子节点,如果有子节点,那就按需加载
//如果没有子节点,就不用设定
DataRow[] zzz = dt.Select("ParentId="+ dr["TypeId"]);
if (zzz.Length >0)
{
//启用按需加载
tmptn.PopulateOnDemand =true;
}
tmptn.Value = dr["TypeId"].ToString();
if (tn ==null)
{
tv.Nodes.Add(tmptn);
}
else
{
tn.ChildNodes.Add(tmptn);
}
}
}
protectedvoid TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
{
DataRow[] drs = dt.Select("ParentId="+ e.Node.Value);
foreach (DataRow dr in drs)
{
TreeNode tmptn =new TreeNode();
tmptn.Text = dr["TypeName"].ToString();
tmptn.NavigateUrl ="Default.aspx";
tmptn.Target ="_blank";
//当前节点的子节点数,如果有子节点,那么设定按需加载
DataRow[] zzz = dt.Select("ParentId="+ dr["TypeId"]);
if (zzz.Length >0)
{
//那么设定按需加载
tmptn.PopulateOnDemand =true;
}
tmptn.Value = dr["TypeId"].ToString();
//将当值添加到当前点击的子节点上
e.Node.ChildNodes.Add(tmptn);
}
}
}
}
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<asp:TreeView ID="TreeView1" runat="server"
ontreenodepopulate="TreeView1_TreeNodePopulate" ExpandDepth="0">
</asp:TreeView>
</form>
</body>
</html>
//====================================================
//后台代码
namespace WebFileUp
{
publicpartialclass WebFormTreeview2 : System.Web.UI.Page
{
/*
* 第一:设定目录树的展开深度为0
* 第二:当添加节点的时候,设定按需加载为真(判断是否有子节点,如果有的话,才设定为真)
* 第三:编写按需加载的事件,
* */
DataTable dt =new DataTable();
protectedvoid Page_Load(object sender, EventArgs e)
{
DataInit();
if (!Page.IsPostBack)
{
LoadTree(this.TreeView1, null, 0);
}
}
privatevoid DataInit()
{
string cmdTxt ="Select * From TreeView";
//打开数据库,获取类别信息
SqlConnection conn =new SqlConnection(@"Data Source=HAPPY-THINK\HAPPYMSSQLSERVER;Initial Catalog=test0530;Persist Security Info=True;User ID=sa;Password=happy");
SqlCommand comm =new SqlCommand(cmdTxt, conn);
SqlDataAdapter adap =new SqlDataAdapter(comm);
adap.Fill(dt);
conn.Close();
}
privatevoid DataLoad1()
{
}
privatevoid LoadTree(TreeView tv, TreeNode tn, int parentid)
{
DataRow[] drs = dt.Select("ParentId="+ parentid);
foreach (DataRow dr in drs)
{
TreeNode tmptn =new TreeNode();
tmptn.Text = dr["TypeName"].ToString();
tmptn.NavigateUrl ="Default.aspx";
tmptn.Target ="_blank";
//判断是否有子节点,如果有子节点,那就按需加载
//如果没有子节点,就不用设定
DataRow[] zzz = dt.Select("ParentId="+ dr["TypeId"]);
if (zzz.Length >0)
{
//启用按需加载
tmptn.PopulateOnDemand =true;
}
tmptn.Value = dr["TypeId"].ToString();
if (tn ==null)
{
tv.Nodes.Add(tmptn);
}
else
{
tn.ChildNodes.Add(tmptn);
}
}
}
protectedvoid TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
{
DataRow[] drs = dt.Select("ParentId="+ e.Node.Value);
foreach (DataRow dr in drs)
{
TreeNode tmptn =new TreeNode();
tmptn.Text = dr["TypeName"].ToString();
tmptn.NavigateUrl ="Default.aspx";
tmptn.Target ="_blank";
//当前节点的子节点数,如果有子节点,那么设定按需加载
DataRow[] zzz = dt.Select("ParentId="+ dr["TypeId"]);
if (zzz.Length >0)
{
//那么设定按需加载
tmptn.PopulateOnDemand =true;
}
tmptn.Value = dr["TypeId"].ToString();
//将当值添加到当前点击的子节点上
e.Node.ChildNodes.Add(tmptn);
}
}
}
}