虽然TreeView很方便,但是不得不说,它生成的ViewState数量另人发指.....所以还请适当选择性的应用.
首先要有一个站点地图(必须放在UI层的根目录)
<?
xml version="1.0" encoding="utf-8"
?>
< siteMap xmlns ="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
< siteMapNode url ="~/TreeView/Default.aspx" title ="首页" description ="" >
< siteMapNode url ="http://www.baidu.com" title ="百度" description ="" >
< siteMapNode url ="http://zhidao.baidu.com" title ="百度知道" description ="百度知道" ></ siteMapNode >
< siteMapNode url ="http://mp3.baidu.com" title ="百度音乐" description ="百度音乐" ></ siteMapNode >
< siteMapNode url ="http://news.baidu.com" title ="百度新闻" description ="百度新闻" ></ siteMapNode >
</ siteMapNode >
< siteMapNode url ="http://www.google.com.hk" title ="谷歌" description ="" >
< siteMapNode url ="http://ditu.google.com" title ="谷歌地图" description ="谷歌地图" ></ siteMapNode >
</ siteMapNode >
< siteMapNode url ="http://www.cnblogs.com" title ="博客园" description ="博客园" >
< siteMapNode url ="http://zhuiyi.cnblogs.com" title ="我的博客" description ="" ></ siteMapNode >
</ siteMapNode >
</ siteMapNode >
</ siteMap >
< siteMap xmlns ="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
< siteMapNode url ="~/TreeView/Default.aspx" title ="首页" description ="" >
< siteMapNode url ="http://www.baidu.com" title ="百度" description ="" >
< siteMapNode url ="http://zhidao.baidu.com" title ="百度知道" description ="百度知道" ></ siteMapNode >
< siteMapNode url ="http://mp3.baidu.com" title ="百度音乐" description ="百度音乐" ></ siteMapNode >
< siteMapNode url ="http://news.baidu.com" title ="百度新闻" description ="百度新闻" ></ siteMapNode >
</ siteMapNode >
< siteMapNode url ="http://www.google.com.hk" title ="谷歌" description ="" >
< siteMapNode url ="http://ditu.google.com" title ="谷歌地图" description ="谷歌地图" ></ siteMapNode >
</ siteMapNode >
< siteMapNode url ="http://www.cnblogs.com" title ="博客园" description ="博客园" >
< siteMapNode url ="http://zhuiyi.cnblogs.com" title ="我的博客" description ="" ></ siteMapNode >
</ siteMapNode >
</ siteMapNode >
</ siteMap >
一个TreeView控件,并为其绑定数据源,一个按钮,并添加Click事件
<
asp:TreeView
ID
="TreeView1"
runat
="server"
DataSourceID
="SiteMapDataSource1"
ImageSet
="BulletedList4"
>
</ asp:TreeView >
< asp:SiteMapDataSource ID ="SiteMapDataSource1" runat ="server" />
< asp:Button ID ="btnAddNode" runat ="server" Text ="动态添加结点"
onclick ="btnAddNode_Click" />
</ asp:TreeView >
< asp:SiteMapDataSource ID ="SiteMapDataSource1" runat ="server" />
< asp:Button ID ="btnAddNode" runat ="server" Text ="动态添加结点"
onclick ="btnAddNode_Click" />
事件代码:
protected
void
btnAddNode_Click(
object
sender, EventArgs e)
{
TreeNode tn = new TreeNode();
tn.Text = " 我的博客 " ;
tn.NavigateUrl = " http://zhuiyi.cnblogs.com " ;
TreeNode mytreenode = TreeView1.FindNode( " 首页 " );
TreeView1.FindNode( " 首页/博客园 " ).ChildNodes.Add(tn);
}
{
TreeNode tn = new TreeNode();
tn.Text = " 我的博客 " ;
tn.NavigateUrl = " http://zhuiyi.cnblogs.com " ;
TreeNode mytreenode = TreeView1.FindNode( " 首页 " );
TreeView1.FindNode( " 首页/博客园 " ).ChildNodes.Add(tn);
}