3.msdn中nasp.net 2.0中TREEVIEW中动态增加结点
4TreeView.ImageSet 属性,换换treeview的皮肤
获取或设置用于
TreeView 控件的图像组
下面是一个应用的例子,如果有看不懂得可以到http://msdn2.microsoft.com/zh-cn/library/system.web.ui.webcontrols.treeview.hovernodestyle(VS.80).aspx
察看相关的属性4TreeView.ImageSet 属性,换换treeview的皮肤
<asp:TreeView ID="TreeView1" runat="server" ImageSet="Contacts"
Style="font-size: 13px; background-color:#CAD7F7" Width="160px" LineImagesFolder="~/TreeLineImages" NodeIndent="10" ShowLines="false">
<ParentNodeStyle Font-Bold="True" ForeColor="#5555DD" />
<HoverNodeStyle Font-Underline="False" />
<SelectedNodeStyle Font-Underline="True" HorizontalPadding="0px"
VerticalPadding="0px" />
<Nodes>
<asp:TreeNode Checked="True" NavigateUrl="~/adminmain.aspx" Text="管理首页" Value="管理首页">
<asp:TreeNode Text="寝室管理" Value="寝室管理" NavigateUrl="~/adminmain.aspx">
<asp:TreeNode NavigateUrl="~/homeadd.aspx" Text="寝室楼添加" Value="寝室楼添加"></asp:TreeNode>
<asp:TreeNode NavigateUrl="~/adminhomedelete.aspx" Text="寝室楼修改/删除" Value="寝室楼修改/删除">
</asp:TreeNode>
<asp:TreeNode NavigateUrl="~/admingoodhome.aspx" Text="文明寝室管理" Value="文明寝室管理"></asp:TreeNode>
<asp:TreeNode NavigateUrl="~/adminhomebad.aspx" Text="寝室损坏管理" Value="寝室损坏管理"></asp:TreeNode>
<asp:TreeNode NavigateUrl="~/adminhomeedit.aspx" Text="寝室信息管理" Value="寝室信息管理"></asp:TreeNode>
<asp:TreeNode NavigateUrl="~/adminnotics.aspx" Text="寝室公告栏管理" Value="寝室公告栏管理"></asp:TreeNode>
</asp:TreeNode>
<asp:TreeNode Text="班级管理" Value="班级管理" NavigateUrl="~/adminmain.aspx">
<asp:TreeNode Text="添加系别名称" Value="添加系别名称" NavigateUrl="~/adminxiname.aspx"></asp:TreeNode>
<asp:TreeNode Text="系别名称修改/删除" Value="系别名称修改/删除" NavigateUrl="~/adminxinameedit.aspx"></asp:TreeNode>
<asp:TreeNode Text="专业名称添加" Value="专业名称添加" NavigateUrl="~/adminzhuanyeadd.aspx"></asp:TreeNode>
<asp:TreeNode Text="专业名称修改/删除" Value="专业名称修改/删除" NavigateUrl="~/adminzhuanyeedit.aspx"></asp:TreeNode>
</asp:TreeNode>
<asp:TreeNode Text="用户管理" Value="用户管理" NavigateUrl="~/adminmain.aspx">
<asp:TreeNode NavigateUrl="~/adminadd.aspx" Text="添加管理员 " Value="添加管理员"></asp:TreeNode>
<asp:TreeNode NavigateUrl="~/adminpwdchang.aspx" Text="管理员密码修改" Value="管理员密码修改"></asp:TreeNode>
<asp:TreeNode NavigateUrl="~/adminuserinfo.aspx" Text="学生基本资料管理 " Value="学生基本资料管理 ">
</asp:TreeNode>
<asp:TreeNode NavigateUrl="~/adminuseradd.aspx" Text="添加学生基本信息" Value="添加学生基本信息"></asp:TreeNode>
</asp:TreeNode>
<asp:TreeNode Text="查询功能" Value="查询功能" NavigateUrl="~/adminmain.aspx">
<asp:TreeNode NavigateUrl="~/adminuseridfind.aspx" Text="学号查询" Value="学号查询"></asp:TreeNode>
<asp:TreeNode NavigateUrl="~/adminusernamefind.aspx" Text="姓名查询 " Value="姓名查询 "></asp:TreeNode>
<asp:TreeNode NavigateUrl="~/adminxinamefind.aspx" Text="系别名称搜索" Value="系别名称搜索"></asp:TreeNode>
<asp:TreeNode NavigateUrl="~/adminzhuanyefind.aspx" Text="专业名称搜索" Value="专业名称搜索"></asp:TreeNode>
<asp:TreeNode NavigateUrl="~/adminclassnamefind.aspx" Text="班级名称查找 " Value="班级名称查找 ">
</asp:TreeNode>
<asp:TreeNode NavigateUrl="~/adminteacherfind.aspx" Text="辅导老师查找" Value="辅导老师查找"></asp:TreeNode>
<asp:TreeNode NavigateUrl="~/adminhomefloodfind.aspx" Text="寝室楼查询 " Value="寝室楼查询 "></asp:TreeNode>
<asp:TreeNode NavigateUrl="~/adminhomefenfind.aspx" Text="评分查找 " Value="评分查找 "></asp:TreeNode>
</asp:TreeNode>
<asp:TreeNode Text="留言版管理" Value="留言版管理" NavigateUrl="~/adminmain.aspx">
<asp:TreeNode NavigateUrl="~/adminguestbook.aspx" Text="回复/删除留言 " Value="回复/删除留言 "></asp:TreeNode>
<asp:TreeNode NavigateUrl="~/admingbsou.aspx" Text="搜索留言" Value="搜索留言"></asp:TreeNode>
</asp:TreeNode>
</asp:TreeNode>
</Nodes>
<NodeStyle Font-Names="Verdana" Font-Size="8pt" ForeColor="Black" HorizontalPadding="5px"
NodeSpacing="0px" VerticalPadding="0px" />
</asp:TreeView>
Style="font-size: 13px; background-color:#CAD7F7" Width="160px" LineImagesFolder="~/TreeLineImages" NodeIndent="10" ShowLines="false">
<ParentNodeStyle Font-Bold="True" ForeColor="#5555DD" />
<HoverNodeStyle Font-Underline="False" />
<SelectedNodeStyle Font-Underline="True" HorizontalPadding="0px"
VerticalPadding="0px" />
<Nodes>
<asp:TreeNode Checked="True" NavigateUrl="~/adminmain.aspx" Text="管理首页" Value="管理首页">
<asp:TreeNode Text="寝室管理" Value="寝室管理" NavigateUrl="~/adminmain.aspx">
<asp:TreeNode NavigateUrl="~/homeadd.aspx" Text="寝室楼添加" Value="寝室楼添加"></asp:TreeNode>
<asp:TreeNode NavigateUrl="~/adminhomedelete.aspx" Text="寝室楼修改/删除" Value="寝室楼修改/删除">
</asp:TreeNode>
<asp:TreeNode NavigateUrl="~/admingoodhome.aspx" Text="文明寝室管理" Value="文明寝室管理"></asp:TreeNode>
<asp:TreeNode NavigateUrl="~/adminhomebad.aspx" Text="寝室损坏管理" Value="寝室损坏管理"></asp:TreeNode>
<asp:TreeNode NavigateUrl="~/adminhomeedit.aspx" Text="寝室信息管理" Value="寝室信息管理"></asp:TreeNode>
<asp:TreeNode NavigateUrl="~/adminnotics.aspx" Text="寝室公告栏管理" Value="寝室公告栏管理"></asp:TreeNode>
</asp:TreeNode>
<asp:TreeNode Text="班级管理" Value="班级管理" NavigateUrl="~/adminmain.aspx">
<asp:TreeNode Text="添加系别名称" Value="添加系别名称" NavigateUrl="~/adminxiname.aspx"></asp:TreeNode>
<asp:TreeNode Text="系别名称修改/删除" Value="系别名称修改/删除" NavigateUrl="~/adminxinameedit.aspx"></asp:TreeNode>
<asp:TreeNode Text="专业名称添加" Value="专业名称添加" NavigateUrl="~/adminzhuanyeadd.aspx"></asp:TreeNode>
<asp:TreeNode Text="专业名称修改/删除" Value="专业名称修改/删除" NavigateUrl="~/adminzhuanyeedit.aspx"></asp:TreeNode>
</asp:TreeNode>
<asp:TreeNode Text="用户管理" Value="用户管理" NavigateUrl="~/adminmain.aspx">
<asp:TreeNode NavigateUrl="~/adminadd.aspx" Text="添加管理员 " Value="添加管理员"></asp:TreeNode>
<asp:TreeNode NavigateUrl="~/adminpwdchang.aspx" Text="管理员密码修改" Value="管理员密码修改"></asp:TreeNode>
<asp:TreeNode NavigateUrl="~/adminuserinfo.aspx" Text="学生基本资料管理 " Value="学生基本资料管理 ">
</asp:TreeNode>
<asp:TreeNode NavigateUrl="~/adminuseradd.aspx" Text="添加学生基本信息" Value="添加学生基本信息"></asp:TreeNode>
</asp:TreeNode>
<asp:TreeNode Text="查询功能" Value="查询功能" NavigateUrl="~/adminmain.aspx">
<asp:TreeNode NavigateUrl="~/adminuseridfind.aspx" Text="学号查询" Value="学号查询"></asp:TreeNode>
<asp:TreeNode NavigateUrl="~/adminusernamefind.aspx" Text="姓名查询 " Value="姓名查询 "></asp:TreeNode>
<asp:TreeNode NavigateUrl="~/adminxinamefind.aspx" Text="系别名称搜索" Value="系别名称搜索"></asp:TreeNode>
<asp:TreeNode NavigateUrl="~/adminzhuanyefind.aspx" Text="专业名称搜索" Value="专业名称搜索"></asp:TreeNode>
<asp:TreeNode NavigateUrl="~/adminclassnamefind.aspx" Text="班级名称查找 " Value="班级名称查找 ">
</asp:TreeNode>
<asp:TreeNode NavigateUrl="~/adminteacherfind.aspx" Text="辅导老师查找" Value="辅导老师查找"></asp:TreeNode>
<asp:TreeNode NavigateUrl="~/adminhomefloodfind.aspx" Text="寝室楼查询 " Value="寝室楼查询 "></asp:TreeNode>
<asp:TreeNode NavigateUrl="~/adminhomefenfind.aspx" Text="评分查找 " Value="评分查找 "></asp:TreeNode>
</asp:TreeNode>
<asp:TreeNode Text="留言版管理" Value="留言版管理" NavigateUrl="~/adminmain.aspx">
<asp:TreeNode NavigateUrl="~/adminguestbook.aspx" Text="回复/删除留言 " Value="回复/删除留言 "></asp:TreeNode>
<asp:TreeNode NavigateUrl="~/admingbsou.aspx" Text="搜索留言" Value="搜索留言"></asp:TreeNode>
</asp:TreeNode>
</asp:TreeNode>
</Nodes>
<NodeStyle Font-Names="Verdana" Font-Size="8pt" ForeColor="Black" HorizontalPadding="5px"
NodeSpacing="0px" VerticalPadding="0px" />
</asp:TreeView>
asp.net 2.0中TREEVIEW中动态增加结点
在asp.net 2.0中,要动态从数据库中取出内容,动态增加结点,其实不难,比如以SQL SERVER 2000的PUBS数据库为例子,要以树型列表方式,取出作者,做为根结点,然后取出每位作者写过什么书,作为子结点,可以这样
<%@ Page Language="C#"%>
<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.Data.SqlClient"%>
<%@ Import Namespace="System.Configuration"%>
<!DOCTYPE htmlPUBLIC"-//W3C//DTD XHTML 1.1//EN""http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Dynamic Population of the TreeView Control</title>
<script runat=server>
void Node_Populate(object sender,
System.Web.UI.WebControls.TreeNodeEventArgs e)
{
if(e.Node.ChildNodes.Count == 0)
{
switch( e.Node.Depth )
{
case 0:
FillAuthors(e.Node);
break;
case 1:
FillTitlesForAuthors(e.Node);
break;
}
}
}
void FillAuthors(TreeNode node)
{
string connString = System.Configuration.ConfigurationSettings.
ConnectionStrings["NorthwindConnnection"].ConnectionString;
SqlConnection connection = new SqlConnection(connString);
SqlCommand command = new SqlCommand("Select * From
authors",connection);
SqlDataAdapter adapter = new SqlDataAdapter(command);
DataSet authors = new DataSet();
adapter.Fill(authors);
if (authors.Tables.Count > 0)
{
foreach (DataRow row in authors.Tables[0].Rows)
{
TreeNode newNode = new
TreeNode(row["au_fname"].ToString() + " " +
row["au_lname"].ToString(),
row["au_id"].ToString());
newNode.PopulateOnDemand = true;
newNode.SelectAction = TreeNodeSelectAction.Expand;
node.ChildNodes.Add(newNode);
}
}
}
void FillTitlesForAuthors(TreeNode node)
{
string authorID = node.Value;
string connString = System.Configuration.ConfigurationSettings.
ConnectionStrings["NorthwindConnnection"].ConnectionString;
SqlConnection connection = new SqlConnection(connString);
SqlCommand command = new SqlCommand("Select T.title,
T.title_id From titles T" +
" Inner Join titleauthor TA on
T.title_id = TA.title_id " +
" Where TA.au_id = '" + authorID + "'", connection);
SqlDataAdapter adapter = new SqlDataAdapter(command);
DataSet titlesForAuthors = new DataSet();
adapter.Fill(titlesForAuthors);
if (titlesForAuthors.Tables.Count > 0)
{
foreach (DataRow row in titlesForAuthors.Tables[0].Rows)
{
TreeNode newNode = new TreeNode(
row["title"].ToString(), row["title_id"].ToString());
newNode.PopulateOnDemand = false;
newNode.SelectAction = TreeNodeSelectAction.None;
node.ChildNodes.Add(newNode);
}
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TreeViewRunat="Server" ExpandImageUrl="Images/closed.gif"
CollapseImageUrl="Images/open.gif"
OnTreeNodePopulate="Node_Populate" ID="tvwauthors">
<Nodes>
<asp:TreeNodeText="Authors" PopulateOnDemand=true
Value="0"/>
</Nodes>
</asp:TreeView>
</div>
</form>
</body>
</html>
其中,注意ontreenodepopulate事件,是在展开树的结点时发生的,这里定义了自定义的NODE_POPULATE,在 node_populate中,检查当前结点的深度,如果是0,就是根结点,于是就调用FillAuthors过程,取出所有的作者,如果深度是1,则是叶子结点,调用FillTitlesForAuthors过程。其中,要注意它们中的动态建立树结点的过程,如:
TreeNode newNode = new TreeNode(row["au_fname"].ToString() + " " +
row["au_lname"].ToString(),
row["au_id"].ToString());
newNode.PopulateOnDemand = true;
newNode.SelectAction = TreeNodeSelectAction.Expand;
node.ChildNodes.Add(newNode);
其中, popluateondemand属性表明,该结点会动态扩展。
<%@ Page Language="C#"%>
<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.Data.SqlClient"%>
<%@ Import Namespace="System.Configuration"%>
<!DOCTYPE htmlPUBLIC"-//W3C//DTD XHTML 1.1//EN""http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Dynamic Population of the TreeView Control</title>
<script runat=server>
void Node_Populate(object sender,
System.Web.UI.WebControls.TreeNodeEventArgs e)
{
if(e.Node.ChildNodes.Count == 0)
{
switch( e.Node.Depth )
{
case 0:
FillAuthors(e.Node);
break;
case 1:
FillTitlesForAuthors(e.Node);
break;
}
}
}
void FillAuthors(TreeNode node)
{
string connString = System.Configuration.ConfigurationSettings.
ConnectionStrings["NorthwindConnnection"].ConnectionString;
SqlConnection connection = new SqlConnection(connString);
SqlCommand command = new SqlCommand("Select * From
authors",connection);
SqlDataAdapter adapter = new SqlDataAdapter(command);
DataSet authors = new DataSet();
adapter.Fill(authors);
if (authors.Tables.Count > 0)
{
foreach (DataRow row in authors.Tables[0].Rows)
{
TreeNode newNode = new
TreeNode(row["au_fname"].ToString() + " " +
row["au_lname"].ToString(),
row["au_id"].ToString());
newNode.PopulateOnDemand = true;
newNode.SelectAction = TreeNodeSelectAction.Expand;
node.ChildNodes.Add(newNode);
}
}
}
void FillTitlesForAuthors(TreeNode node)
{
string authorID = node.Value;
string connString = System.Configuration.ConfigurationSettings.
ConnectionStrings["NorthwindConnnection"].ConnectionString;
SqlConnection connection = new SqlConnection(connString);
SqlCommand command = new SqlCommand("Select T.title,
T.title_id From titles T" +
" Inner Join titleauthor TA on
T.title_id = TA.title_id " +
" Where TA.au_id = '" + authorID + "'", connection);
SqlDataAdapter adapter = new SqlDataAdapter(command);
DataSet titlesForAuthors = new DataSet();
adapter.Fill(titlesForAuthors);
if (titlesForAuthors.Tables.Count > 0)
{
foreach (DataRow row in titlesForAuthors.Tables[0].Rows)
{
TreeNode newNode = new TreeNode(
row["title"].ToString(), row["title_id"].ToString());
newNode.PopulateOnDemand = false;
newNode.SelectAction = TreeNodeSelectAction.None;
node.ChildNodes.Add(newNode);
}
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TreeViewRunat="Server" ExpandImageUrl="Images/closed.gif"
CollapseImageUrl="Images/open.gif"
OnTreeNodePopulate="Node_Populate" ID="tvwauthors">
<Nodes>
<asp:TreeNodeText="Authors" PopulateOnDemand=true
Value="0"/>
</Nodes>
</asp:TreeView>
</div>
</form>
</body>
</html>
其中,注意ontreenodepopulate事件,是在展开树的结点时发生的,这里定义了自定义的NODE_POPULATE,在 node_populate中,检查当前结点的深度,如果是0,就是根结点,于是就调用FillAuthors过程,取出所有的作者,如果深度是1,则是叶子结点,调用FillTitlesForAuthors过程。其中,要注意它们中的动态建立树结点的过程,如:
TreeNode newNode = new TreeNode(row["au_fname"].ToString() + " " +
row["au_lname"].ToString(),
row["au_id"].ToString());
newNode.PopulateOnDemand = true;
newNode.SelectAction = TreeNodeSelectAction.Expand;
node.ChildNodes.Add(newNode);
其中, popluateondemand属性表明,该结点会动态扩展。
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<script runat="server">
void PopulateNode(Object sender, TreeNodeEventArgs e)
{
// Call the appropriate method to populate a node at a particular level.
switch(e.Node.Depth)
{
case 0:
// Populate the first-level nodes.
PopulateCategories(e.Node);
break;
case 1:
// Populate the second-level nodes.
PopulateProducts(e.Node);
break;
default:
// Do nothing.
break;
}
}
void PopulateCategories(TreeNode node)
{
// Query for the product categories. These are the values
// for the second-level nodes.
DataSet ResultSet = RunQuery("Select CategoryID, CategoryName From Categories");
// Create the second-level nodes.
if(ResultSet.Tables.Count > 0)
{
// Iterate through and create a new node for each row in the query results.
// Notice that the query results are stored in the table of the DataSet.
foreach (DataRow row in ResultSet.Tables[0].Rows)
{
// Create the new node. Notice that the CategoryId is stored in the Value property
// of the node. This will make querying for items in a specific category easier when
// the third-level nodes are created.
TreeNode NewNode = new TreeNode(row["CategoryName"].ToString(), row["CategoryID"].ToString());
// Set the PopulateOnDemand property to true so that the child nodes can be
// dynamically populated.
NewNode.PopulateOnDemand = true;
// Set additional properties for the node.
NewNode.SelectAction = TreeNodeSelectAction.Expand;
// Add the new node to the ChildNodes collection of the parent node.
node.ChildNodes.Add(NewNode);
}
}
}
void PopulateProducts(TreeNode node)
{
// Query for the products of the current category. These are the values
// for the third-level nodes.
DataSet ResultSet = RunQuery("Select ProductName From Products Where CategoryID=" + node.Value);
// Create the third-level nodes.
if(ResultSet.Tables.Count > 0)
{
// Iterate through and create a new node for each row in the query results.
// Notice that the query results are stored in the table of the DataSet.
foreach (DataRow row in ResultSet.Tables[0].Rows)
{
// Create the new node.
TreeNode NewNode = new TreeNode(row["ProductName"].ToString(), row["ProductName"].ToString(), "76.gif", "http://blog.csdn.net/laolaowhn","_blank");
// Set the PopulateOnDemand property to false because these are leaf nodes and
// do not need to be populated.
NewNode.PopulateOnDemand = false;
// Set additional properties for the node.
//NewNode.SelectAction = TreeNodeSelectAction.None;//这句加上去的话上面给这个节点家的脸就不起作用了。
// Add the new node to the ChildNodes collection of the parent node.
node.ChildNodes.Add(NewNode);
}
}
}
DataSet RunQuery(String QueryString)
{
// Declare the connection string. This example uses Microsoft SQL Server and connects to the
// Northwind sample database.
String ConnectionString = "server=localhost;database=NorthWind;Integrated Security=SSPI";
SqlConnection DBConnection = new SqlConnection(ConnectionString);
SqlDataAdapter DBAdapter;
DataSet ResultsDataSet = new DataSet();
try
{
// Run the query and create a DataSet.
DBAdapter = new SqlDataAdapter(QueryString, DBConnection);
DBAdapter.Fill(ResultsDataSet);
// Close the database connection.
DBConnection.Close();
}
catch(Exception ex)
{
// Close the database connection if it is still open.
if(DBConnection.State == ConnectionState.Open)
{
DBConnection.Close();
}
Message.Text = "Unable to connect to the database.";
}
return ResultsDataSet;
}
</script>
<html>
<body>
<form id="Form1" runat="server">
<h3>TreeView TreeNodePopulate Example</h3>
<asp:TreeView id="LinksTreeView"
Font-Name= "Arial"
ForeColor="Blue"
EnableClientScript="false"
OnTreeNodePopulate="PopulateNode"
runat="server">
<Nodes>
<asp:TreeNode Text="Inventory"
SelectAction="Expand"
PopulateOnDemand="true"/>
</Nodes>
</asp:TreeView>
<br><br>
<asp:Label id="Message" runat="server"/>
</form>
</body>
</html>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<script runat="server">
void PopulateNode(Object sender, TreeNodeEventArgs e)
{
// Call the appropriate method to populate a node at a particular level.
switch(e.Node.Depth)
{
case 0:
// Populate the first-level nodes.
PopulateCategories(e.Node);
break;
case 1:
// Populate the second-level nodes.
PopulateProducts(e.Node);
break;
default:
// Do nothing.
break;
}
}
void PopulateCategories(TreeNode node)
{
// Query for the product categories. These are the values
// for the second-level nodes.
DataSet ResultSet = RunQuery("Select CategoryID, CategoryName From Categories");
// Create the second-level nodes.
if(ResultSet.Tables.Count > 0)
{
// Iterate through and create a new node for each row in the query results.
// Notice that the query results are stored in the table of the DataSet.
foreach (DataRow row in ResultSet.Tables[0].Rows)
{
// Create the new node. Notice that the CategoryId is stored in the Value property
// of the node. This will make querying for items in a specific category easier when
// the third-level nodes are created.
TreeNode NewNode = new TreeNode(row["CategoryName"].ToString(), row["CategoryID"].ToString());
// Set the PopulateOnDemand property to true so that the child nodes can be
// dynamically populated.
NewNode.PopulateOnDemand = true;
// Set additional properties for the node.
NewNode.SelectAction = TreeNodeSelectAction.Expand;
// Add the new node to the ChildNodes collection of the parent node.
node.ChildNodes.Add(NewNode);
}
}
}
void PopulateProducts(TreeNode node)
{
// Query for the products of the current category. These are the values
// for the third-level nodes.
DataSet ResultSet = RunQuery("Select ProductName From Products Where CategoryID=" + node.Value);
// Create the third-level nodes.
if(ResultSet.Tables.Count > 0)
{
// Iterate through and create a new node for each row in the query results.
// Notice that the query results are stored in the table of the DataSet.
foreach (DataRow row in ResultSet.Tables[0].Rows)
{
// Create the new node.
TreeNode NewNode = new TreeNode(row["ProductName"].ToString(), row["ProductName"].ToString(), "76.gif", "http://blog.csdn.net/laolaowhn","_blank");
// Set the PopulateOnDemand property to false because these are leaf nodes and
// do not need to be populated.
NewNode.PopulateOnDemand = false;
// Set additional properties for the node.
//NewNode.SelectAction = TreeNodeSelectAction.None;//这句加上去的话上面给这个节点家的脸就不起作用了。
// Add the new node to the ChildNodes collection of the parent node.
node.ChildNodes.Add(NewNode);
}
}
}
DataSet RunQuery(String QueryString)
{
// Declare the connection string. This example uses Microsoft SQL Server and connects to the
// Northwind sample database.
String ConnectionString = "server=localhost;database=NorthWind;Integrated Security=SSPI";
SqlConnection DBConnection = new SqlConnection(ConnectionString);
SqlDataAdapter DBAdapter;
DataSet ResultsDataSet = new DataSet();
try
{
// Run the query and create a DataSet.
DBAdapter = new SqlDataAdapter(QueryString, DBConnection);
DBAdapter.Fill(ResultsDataSet);
// Close the database connection.
DBConnection.Close();
}
catch(Exception ex)
{
// Close the database connection if it is still open.
if(DBConnection.State == ConnectionState.Open)
{
DBConnection.Close();
}
Message.Text = "Unable to connect to the database.";
}
return ResultsDataSet;
}
</script>
<html>
<body>
<form id="Form1" runat="server">
<h3>TreeView TreeNodePopulate Example</h3>
<asp:TreeView id="LinksTreeView"
Font-Name= "Arial"
ForeColor="Blue"
EnableClientScript="false"
OnTreeNodePopulate="PopulateNode"
runat="server">
<Nodes>
<asp:TreeNode Text="Inventory"
SelectAction="Expand"
PopulateOnDemand="true"/>
</Nodes>
</asp:TreeView>
<br><br>
<asp:Label id="Message" runat="server"/>
</form>
</body>
</html>
TreeView.ImageSet 属性
获取或设置用于 TreeView 控件的图像组。
命名空间: System.Web.UI.WebControls
程序集: System.Web(在 system.web.dll 中)
语法语法
Visual Basic(声明)
Public Property ImageSet As TreeViewImageSet
Visual Basic (用法)
Dim instance As TreeView
Dim value As TreeViewImageSet
value = instance.ImageSet
instance.ImageSet = value
C#
public TreeViewImageSet ImageSet { get; set; }
C++
public:
property TreeViewImageSet ImageSet {
TreeViewImageSet get ();
void set (TreeViewImageSet value);
}
J#
/** @property */
public TreeViewImageSet get_ImageSet ()
/** @property */
public void set_ImageSet (TreeViewImageSet value)
JScript
public function get ImageSet () : TreeViewImageSet
public function set ImageSet (value : TreeViewImageSet)
XAML
不适用。
属性值
TreeViewImageSet 值之一。默认值为 TreeViewImageSet.Custom。
异常异常
异常类型 条件
ArgumentOutOfRangeException
指定的图像集不是 TreeViewImageSet 值之一。
备注备注
使用 ImageSet 属性可指定用于 TreeView 控件的图像组。可以选择预定义的图像组为控件提供标准外观,也可以定义自己的自定义集。
说明注意:
ImageSet 属性不会影响节点的任何样式设置。如果要添加自定义的颜色和间距,请设置适当的样式属性,如 NodeStyle 属性。
下表列出了 ImageSet 属性的有效值。
图像集
说明
TreeViewImageSet.Arrows
使用箭头图像的预定义图像集。
TreeViewImageSet.BulletedList
使用菱形项目符号图像的预定义图像集。
TreeViewImageSet.BulletedList2
使用方形项目符号图像的预定义图像集。
TreeViewImageSet.BulletedList3
预定义的、分节的菱形项目符号图像集。
TreeViewImageSet.BulletedList4
预定义的、大小随级别不同而变化的、分节的备用菱形项目符号图像集。
TreeViewImageSet.Contacts
使用 MSN Messenger 图像的预定义图像集。
TreeViewImageSet.Custom
用户定义的图像集。
TreeViewImageSet.Events
使用事件日志样式的图像的预定义图像集。
TreeViewImageSet.Faq
使用 FAQ 样式的图像的预定义图像集。
TreeViewImageSet.Inbox
使用电子邮件样式的图像的预定义图像集。
TreeViewImageSet.News
使用新闻组样式的图像的预定义图像集。
TreeViewImageSet.Simple
使用简单空心形状的预定义图像集。
TreeViewImageSet.Simple2
使用简单实心形状的预定义图像集。
TreeViewImageSet.Msdn
使用 MSDN 图像的预定义图像集。
TreeViewImageSet.WindowsHelp
使用 Microsoft Windows 帮助图像的预定义图像集。
TreeViewImageSet.XPFileExplorer
使用 Microsoft Windows XP 中的 Windows 资源管理器图像的预定义图像集。
定义自定义图像集时,应为下表中的每个属性指定一个图像。如果不指定,则使用该属性的默认图像。
属性
说明
CollapseImageUrl
用于可折叠节点的指示符的自定义图像的 URL。
ExpandImageUrl
用于可展开节点的指示符的自定义图像的 URL。
LineImagesFolder
包含节点连线的自定义图像的文件夹的 URL。
NoExpandImageUrl
用于不可展开节点的指示符的自定义图像的 URL。
此属性的值存储在视图状态中。
示例示例
下面的代码示例演示如何使用 ImageSet 属性显示 TreeView 控件中的 Windows 资源管理器图像集。
Visual Basic
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html >
<head runat="server">
<title>TreeView ImageSet Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>TreeView ImageSet Example</h3>
<asp:TreeView id="LinksTreeView"
ImageSet="XPFileExplorer"
runat="server">
<Nodes>
<asp:TreeNode Value="Home"
NavigateUrl="Home.aspx"
Text="Home"
Target="_blank"
Expanded="True">
<asp:TreeNode Value="Page 1"
NavigateUrl="Page1.aspx"
Text="Page1"
Target="_blank">
<asp:TreeNode Value="Section 1"
NavigateUrl="Section1.aspx"
Text="Section 1"
Target="_blank"/>
</asp:TreeNode>
<asp:TreeNode Value="Page 2"
NavigateUrl="Page2.aspx"
Text="Page 2"
Target="_blank">
</asp:TreeNode>
</asp:TreeNode>
</Nodes>
</asp:TreeView>
</form>
</body>
</html>
C#
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html >
<head runat="server">
<title>TreeView ImageSet Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>TreeView ImageSet Example</h3>
<asp:TreeView id="LinksTreeView"
ImageSet="XPFileExplorer"
runat="server">
<Nodes>
<asp:TreeNode Value="Home"
NavigateUrl="Home.aspx"
Text="Home"
Target="_blank"
Expanded="True">
<asp:TreeNode Value="Page 1"
NavigateUrl="Page1.aspx"
Text="Page1"
Target="_blank">
<asp:TreeNode Value="Section 1"
NavigateUrl="Section1.aspx"
Text="Section 1"
Target="_blank"/>
</asp:TreeNode>
<asp:TreeNode Value="Page 2"
NavigateUrl="Page2.aspx"
Text="Page 2"
Target="_blank">
</asp:TreeNode>
</asp:TreeNode>
</Nodes>
</asp:TreeView>
</form>
</body>
</html>
获取或设置用于 TreeView 控件的图像组。
命名空间: System.Web.UI.WebControls
程序集: System.Web(在 system.web.dll 中)
语法语法
Visual Basic(声明)
Public Property ImageSet As TreeViewImageSet
Visual Basic (用法)
Dim instance As TreeView
Dim value As TreeViewImageSet
value = instance.ImageSet
instance.ImageSet = value
C#
public TreeViewImageSet ImageSet { get; set; }
C++
public:
property TreeViewImageSet ImageSet {
TreeViewImageSet get ();
void set (TreeViewImageSet value);
}
J#
/** @property */
public TreeViewImageSet get_ImageSet ()
/** @property */
public void set_ImageSet (TreeViewImageSet value)
JScript
public function get ImageSet () : TreeViewImageSet
public function set ImageSet (value : TreeViewImageSet)
XAML
不适用。
属性值
TreeViewImageSet 值之一。默认值为 TreeViewImageSet.Custom。
异常异常
异常类型 条件
ArgumentOutOfRangeException
指定的图像集不是 TreeViewImageSet 值之一。
备注备注
使用 ImageSet 属性可指定用于 TreeView 控件的图像组。可以选择预定义的图像组为控件提供标准外观,也可以定义自己的自定义集。
说明注意:
ImageSet 属性不会影响节点的任何样式设置。如果要添加自定义的颜色和间距,请设置适当的样式属性,如 NodeStyle 属性。
下表列出了 ImageSet 属性的有效值。
图像集
说明
TreeViewImageSet.Arrows
使用箭头图像的预定义图像集。
TreeViewImageSet.BulletedList
使用菱形项目符号图像的预定义图像集。
TreeViewImageSet.BulletedList2
使用方形项目符号图像的预定义图像集。
TreeViewImageSet.BulletedList3
预定义的、分节的菱形项目符号图像集。
TreeViewImageSet.BulletedList4
预定义的、大小随级别不同而变化的、分节的备用菱形项目符号图像集。
TreeViewImageSet.Contacts
使用 MSN Messenger 图像的预定义图像集。
TreeViewImageSet.Custom
用户定义的图像集。
TreeViewImageSet.Events
使用事件日志样式的图像的预定义图像集。
TreeViewImageSet.Faq
使用 FAQ 样式的图像的预定义图像集。
TreeViewImageSet.Inbox
使用电子邮件样式的图像的预定义图像集。
TreeViewImageSet.News
使用新闻组样式的图像的预定义图像集。
TreeViewImageSet.Simple
使用简单空心形状的预定义图像集。
TreeViewImageSet.Simple2
使用简单实心形状的预定义图像集。
TreeViewImageSet.Msdn
使用 MSDN 图像的预定义图像集。
TreeViewImageSet.WindowsHelp
使用 Microsoft Windows 帮助图像的预定义图像集。
TreeViewImageSet.XPFileExplorer
使用 Microsoft Windows XP 中的 Windows 资源管理器图像的预定义图像集。
定义自定义图像集时,应为下表中的每个属性指定一个图像。如果不指定,则使用该属性的默认图像。
属性
说明
CollapseImageUrl
用于可折叠节点的指示符的自定义图像的 URL。
ExpandImageUrl
用于可展开节点的指示符的自定义图像的 URL。
LineImagesFolder
包含节点连线的自定义图像的文件夹的 URL。
NoExpandImageUrl
用于不可展开节点的指示符的自定义图像的 URL。
此属性的值存储在视图状态中。
示例示例
下面的代码示例演示如何使用 ImageSet 属性显示 TreeView 控件中的 Windows 资源管理器图像集。
Visual Basic
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html >
<head runat="server">
<title>TreeView ImageSet Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>TreeView ImageSet Example</h3>
<asp:TreeView id="LinksTreeView"
ImageSet="XPFileExplorer"
runat="server">
<Nodes>
<asp:TreeNode Value="Home"
NavigateUrl="Home.aspx"
Text="Home"
Target="_blank"
Expanded="True">
<asp:TreeNode Value="Page 1"
NavigateUrl="Page1.aspx"
Text="Page1"
Target="_blank">
<asp:TreeNode Value="Section 1"
NavigateUrl="Section1.aspx"
Text="Section 1"
Target="_blank"/>
</asp:TreeNode>
<asp:TreeNode Value="Page 2"
NavigateUrl="Page2.aspx"
Text="Page 2"
Target="_blank">
</asp:TreeNode>
</asp:TreeNode>
</Nodes>
</asp:TreeView>
</form>
</body>
</html>
C#
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html >
<head runat="server">
<title>TreeView ImageSet Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>TreeView ImageSet Example</h3>
<asp:TreeView id="LinksTreeView"
ImageSet="XPFileExplorer"
runat="server">
<Nodes>
<asp:TreeNode Value="Home"
NavigateUrl="Home.aspx"
Text="Home"
Target="_blank"
Expanded="True">
<asp:TreeNode Value="Page 1"
NavigateUrl="Page1.aspx"
Text="Page1"
Target="_blank">
<asp:TreeNode Value="Section 1"
NavigateUrl="Section1.aspx"
Text="Section 1"
Target="_blank"/>
</asp:TreeNode>
<asp:TreeNode Value="Page 2"
NavigateUrl="Page2.aspx"
Text="Page 2"
Target="_blank">
</asp:TreeNode>
</asp:TreeNode>
</Nodes>
</asp:TreeView>
</form>
</body>
</html>
xx