适用情况:TreeView+IFrame,点击某个叶子节点,IFrame中显示相应的页面。
要求:第一次加载时,IFrame中有默认页面;有子节点的树节点,其跳转页面设置为第一个叶子节点的跳转页面,否则不可点击。
准备:
1、获取数据集合,比如DataTable
2、动态加载节点,其子节点设置NavigateUrl、Target属性
相关代码:
1、前台
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
<
div id
=
"
treeview
"
style
=
"
overflow: auto;
"
>
< asp:TreeView ImageSet = " WindowsHelp " ID = " tvClass " runat = " server " ShowLines = " True " LineImagesFolder = " ~/TreeLineImages " >
</ asp:TreeView >
</ div >
< div id = " divFrame " >
< iframe id = " ifNewsSet " name = " ifNewsSet " frameborder = " 0 " scrolling = " no " style = " width: 100%; height: 100% "
< asp:TreeView ImageSet = " WindowsHelp " ID = " tvClass " runat = " server " ShowLines = " True " LineImagesFolder = " ~/TreeLineImages " >
</ asp:TreeView >
</ div >
< div id = " divFrame " >
< iframe id = " ifNewsSet " name = " ifNewsSet " frameborder = " 0 " scrolling = " no " style = " width: 100%; height: 100% "
src
=
'
<%= DefaultPageUrl %>
'
></
iframe
>
</ div >
</ div >
2、后台
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
public
string
DefaultPageUrl
=
string
.Empty;
//
IFrame的默认页面链接(页面加载事件前定义)
private void GetDataTree()
{
using (DataTable tClass = NewsClassInfo.GetAllClass())
{
if (tClass == null || tClass.Rows.Count == 0 )
{
return ;
}
// TreeView的第一个叶子节点的值
string strFirstLeatNodeValue = string .Empty;
// 动态添加节点
foreach (DataRow dr in tClass.Rows)
{
TreeNode root = new TreeNode(dr[ " ClassName " ].ToString(), dr[ " ClassId " ].ToString());
root.ImageUrl = Page.ResolveUrl( " ~/images/org-bg.gif " );
tvClass.Nodes.Add(root);
DataTable dtChildClass = ClassInfo.getChildClassById(dr[ " ClassId " ].ToString());
if (dtChildClass == null || dtChildClass.Rows.Count == 0 )
{
continue ;
}
foreach (DataRow drDetail in dtChildClass.Rows)
{
TreeNode tn = new TreeNode(drDetail[ " ClassName " ].ToString(), drDetail[ " ClassId " ].ToString());
tn.ImageUrl = Page.ResolveUrl( " ~/images/bgClass.gif " );
tn.NavigateUrl = Page.ResolveUrl( " ~/News/NewsSet.aspx?ClassId= " + dr[ " ClassId " ].ToString());
tn.Target = " ifNewsSet " ;
root.ChildNodes.Add(tn);
}
// 设置第一个叶子节点的值,并加载所有子表
if ( string .IsNullOrEmpty(strFirstLeatNodeValue))
{
strFirstLeatNodeValue = dtChildClass.Rows[ 0 ][ " ClassId " ].ToString();
}
}
// 遍历TreeView,凡是有叶子节点的父节点,选中后,Iframe默认为其第一个叶子节点指向的页面;否则,不可选。
WebCommon.SetNodeUrlByFirstLeafNode( this .tvClass);
// 设置IFrame的默认页面
TreeNode defautSelectNode = WebCommon.SelectNode( this .tvClass, strFirstLeatNodeValue);
DefaultPageUrl = defautSelectNode.NavigateUrl;
}
}
private void GetDataTree()
{
using (DataTable tClass = NewsClassInfo.GetAllClass())
{
if (tClass == null || tClass.Rows.Count == 0 )
{
return ;
}
// TreeView的第一个叶子节点的值
string strFirstLeatNodeValue = string .Empty;
// 动态添加节点
foreach (DataRow dr in tClass.Rows)
{
TreeNode root = new TreeNode(dr[ " ClassName " ].ToString(), dr[ " ClassId " ].ToString());
root.ImageUrl = Page.ResolveUrl( " ~/images/org-bg.gif " );
tvClass.Nodes.Add(root);
DataTable dtChildClass = ClassInfo.getChildClassById(dr[ " ClassId " ].ToString());
if (dtChildClass == null || dtChildClass.Rows.Count == 0 )
{
continue ;
}
foreach (DataRow drDetail in dtChildClass.Rows)
{
TreeNode tn = new TreeNode(drDetail[ " ClassName " ].ToString(), drDetail[ " ClassId " ].ToString());
tn.ImageUrl = Page.ResolveUrl( " ~/images/bgClass.gif " );
tn.NavigateUrl = Page.ResolveUrl( " ~/News/NewsSet.aspx?ClassId= " + dr[ " ClassId " ].ToString());
tn.Target = " ifNewsSet " ;
root.ChildNodes.Add(tn);
}
// 设置第一个叶子节点的值,并加载所有子表
if ( string .IsNullOrEmpty(strFirstLeatNodeValue))
{
strFirstLeatNodeValue = dtChildClass.Rows[ 0 ][ " ClassId " ].ToString();
}
}
// 遍历TreeView,凡是有叶子节点的父节点,选中后,Iframe默认为其第一个叶子节点指向的页面;否则,不可选。
WebCommon.SetNodeUrlByFirstLeafNode( this .tvClass);
// 设置IFrame的默认页面
TreeNode defautSelectNode = WebCommon.SelectNode( this .tvClass, strFirstLeatNodeValue);
DefaultPageUrl = defautSelectNode.NavigateUrl;
}
}
3、WebCommon类相关方法:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#region
FromNet: 默认选中TreeView的某个节点
/// <summary>
/// 选中treeview的某个节点,需要每个node的value不同
/// </summary>
/// <param name="sNodeValue"></param>
public static TreeNode SelectNode(TreeView tv, string sNodeValue)
{
TreeNode selectedNode = new TreeNode();
foreach (TreeNode tRoot in tv.Nodes)
{
if (tRoot.Value == sNodeValue)
{
selectedNode = tRoot;
break ;
}
else
{
if (tRoot.ChildNodes != null )
{
TreeNode tTmp = null ;
tTmp = WebCommon.FindNode(tRoot, sNodeValue);
if (tTmp != null )
{
selectedNode = tTmp;
break ;
}
}
}
}
selectedNode.Select();
return selectedNode;
}
/// <summary>
/// 递归查找父节点
/// </summary>
/// <param name="tnParent"> 指定一个根节点,然后遍历它 </param>
/// <param name="strValue"> 所要查找的节点的value </param>
public static TreeNode FindNode(TreeNode tnParent, string strValue)
{
if (tnParent == null ) return null ;
if (tnParent.Value == strValue) return tnParent;
TreeNode tnRet = null ;
foreach (TreeNode tn in tnParent.ChildNodes)
{
tnRet = FindNode(tn, strValue);
if (tnRet != null ) break ;
}
return tnRet;
}
#endregion
#region 遍历TreeView,设置节点的跳转页面和可选情况
/// <summary>
/// 遍历TreeView,设置节点的跳转方式,每个节点的跳转设置为其第一个叶子节点的跳转页面
/// </summary>
/// <param name="tvTree"> 遍历的树 </param>
public static void SetNodeUrlByFirstLeafNode(TreeView tvTree)
{
if (tvTree == null ) return ;
foreach (TreeNode tn in tvTree.Nodes)
{
// 无子节点,不可选
if (tn.ChildNodes.Count == 0 )
{
tn.SelectAction = TreeNodeSelectAction.None;
}
else
{
SetChildNodeUrl(tn);
}
}
}
/// <summary>
/// 递归设置子节点的跳转Url
/// </summary>
/// <param name="tnParent"> 指定一个根节点 </param>
private static void SetChildNodeUrl(TreeNode tnParent)
{
if (tnParent == null ) return ;
foreach (TreeNode tn in tnParent.ChildNodes)
{
// 当前节点为叶子节点
if (tn.ChildNodes.Count == 0 )
{
tnParent.NavigateUrl = tn.NavigateUrl;
tnParent.Target = tn.Target;
break ;
}
else
{
SetChildNodeUrl(tn);
}
}
}
#endregion
/// <summary>
/// 选中treeview的某个节点,需要每个node的value不同
/// </summary>
/// <param name="sNodeValue"></param>
public static TreeNode SelectNode(TreeView tv, string sNodeValue)
{
TreeNode selectedNode = new TreeNode();
foreach (TreeNode tRoot in tv.Nodes)
{
if (tRoot.Value == sNodeValue)
{
selectedNode = tRoot;
break ;
}
else
{
if (tRoot.ChildNodes != null )
{
TreeNode tTmp = null ;
tTmp = WebCommon.FindNode(tRoot, sNodeValue);
if (tTmp != null )
{
selectedNode = tTmp;
break ;
}
}
}
}
selectedNode.Select();
return selectedNode;
}
/// <summary>
/// 递归查找父节点
/// </summary>
/// <param name="tnParent"> 指定一个根节点,然后遍历它 </param>
/// <param name="strValue"> 所要查找的节点的value </param>
public static TreeNode FindNode(TreeNode tnParent, string strValue)
{
if (tnParent == null ) return null ;
if (tnParent.Value == strValue) return tnParent;
TreeNode tnRet = null ;
foreach (TreeNode tn in tnParent.ChildNodes)
{
tnRet = FindNode(tn, strValue);
if (tnRet != null ) break ;
}
return tnRet;
}
#endregion
#region 遍历TreeView,设置节点的跳转页面和可选情况
/// <summary>
/// 遍历TreeView,设置节点的跳转方式,每个节点的跳转设置为其第一个叶子节点的跳转页面
/// </summary>
/// <param name="tvTree"> 遍历的树 </param>
public static void SetNodeUrlByFirstLeafNode(TreeView tvTree)
{
if (tvTree == null ) return ;
foreach (TreeNode tn in tvTree.Nodes)
{
// 无子节点,不可选
if (tn.ChildNodes.Count == 0 )
{
tn.SelectAction = TreeNodeSelectAction.None;
}
else
{
SetChildNodeUrl(tn);
}
}
}
/// <summary>
/// 递归设置子节点的跳转Url
/// </summary>
/// <param name="tnParent"> 指定一个根节点 </param>
private static void SetChildNodeUrl(TreeNode tnParent)
{
if (tnParent == null ) return ;
foreach (TreeNode tn in tnParent.ChildNodes)
{
// 当前节点为叶子节点
if (tn.ChildNodes.Count == 0 )
{
tnParent.NavigateUrl = tn.NavigateUrl;
tnParent.Target = tn.Target;
break ;
}
else
{
SetChildNodeUrl(tn);
}
}
}
#endregion
总结
1、动态加载节点,使用方法:
tvClass.Nodes.Add(root);
//
加载根节点
root.ChildNodes.Add(tn); // 加载子节点
root.ChildNodes.Add(tn); // 加载子节点
2、当前节点不可选择
tn.SelectAction
=
TreeNodeSelectAction.None;
3、选择当前节点
selectedNode.Select();
4、使用SRC属性,设置IFrame默认页面