【原】母版页、皮肤、导航 那点事 Master Pages Themes and Navigation Controls FAQ 【续一篇:导航树】
考试,考试,还是考试!无聊透顶的考试!但是不考又不行的考试!为了那一张薄薄的纸!我又白白浪费的3年宝贵的时间!强烈建议教育改革!最近我快被考试折磨的不行了!多次想过来写一篇,可是一想到考试,整个心情都没有了!不抱怨了,奉献点真东西给观众。
最近我遇到朋友问我,如何动态的在现有的TreeView控件上添加节点的问题,我找到了一些国外的站点,的确有些参考的代码,但是写的都不是太详细。于是我稍微花了点时间,整理了下,就算是和我以前写的那篇呼应一下吧,不过这篇有点单薄,就一篇,但是实用!如果没有读过上一篇的,强烈建议读一读!上一篇的地址是:http://www.cnblogs.com/OceanChen/archive/2009/02/18/1393291.html, 不罗嗦了,上代码,
页面部分【需要注意的是,为了方便大家的操作,已经按照规则填写的默认的值】
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void btnAdd_Click(object sender, System.EventArgs e)
{
TreeNode newNode = new TreeNode();
newNode.Text = txtChildNodeName.Text;
newNode.NavigateUrl = txtChildNodeURL.Text;
newNode.ToolTip = txtChildNodeName.Text;
tvDemo.FindNode(txtParentNode.Text).ChildNodes.Add(newNode);
lblMsg.Text = "Node Added succesfully! <br />Node Text: " + txtChildNodeName.Text +
"<br />NavigateUrl: " + txtChildNodeURL.Text +
"<br />ToolTip: " + txtChildNodeName.Text;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>How to add TreeNode at runtime?</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color: Red">
Dynamically adding TreeNode</h2>
<asp:Label ID="lblMsg" runat="server" Text="Type a Parent Node: "></asp:Label>
<asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" />
<asp:TreeView ID="tvDemo" runat="server" DataSourceID="SiteMapDataSource1">
</asp:TreeView>
<br />
<asp:Label ID="lblParentNode" runat="server" Text="Type a Parent Node: "></asp:Label><asp:TextBox
ID="txtParentNode" Text="Home/Login" runat="server"></asp:TextBox>
<br />
<asp:Label ID="lblChildNodeUrl" runat="server" Text="Type a new child node URL: "></asp:Label>
<asp:TextBox ID="txtChildNodeURL" Text="http://www.asp.net" runat="server"></asp:TextBox>
<br />
<asp:Label ID="lblChildNodeName" runat="server" Text="Type a new child node name: "></asp:Label>
<asp:TextBox ID="txtChildNodeName" Text="ASP.NET" runat="server"></asp:TextBox>
<asp:Button ID="btnAdd" runat="server" Text="Add Node" OnClick="btnAdd_Click" />
</div>
</form>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void btnAdd_Click(object sender, System.EventArgs e)
{
TreeNode newNode = new TreeNode();
newNode.Text = txtChildNodeName.Text;
newNode.NavigateUrl = txtChildNodeURL.Text;
newNode.ToolTip = txtChildNodeName.Text;
tvDemo.FindNode(txtParentNode.Text).ChildNodes.Add(newNode);
lblMsg.Text = "Node Added succesfully! <br />Node Text: " + txtChildNodeName.Text +
"<br />NavigateUrl: " + txtChildNodeURL.Text +
"<br />ToolTip: " + txtChildNodeName.Text;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>How to add TreeNode at runtime?</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color: Red">
Dynamically adding TreeNode</h2>
<asp:Label ID="lblMsg" runat="server" Text="Type a Parent Node: "></asp:Label>
<asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" />
<asp:TreeView ID="tvDemo" runat="server" DataSourceID="SiteMapDataSource1">
</asp:TreeView>
<br />
<asp:Label ID="lblParentNode" runat="server" Text="Type a Parent Node: "></asp:Label><asp:TextBox
ID="txtParentNode" Text="Home/Login" runat="server"></asp:TextBox>
<br />
<asp:Label ID="lblChildNodeUrl" runat="server" Text="Type a new child node URL: "></asp:Label>
<asp:TextBox ID="txtChildNodeURL" Text="http://www.asp.net" runat="server"></asp:TextBox>
<br />
<asp:Label ID="lblChildNodeName" runat="server" Text="Type a new child node name: "></asp:Label>
<asp:TextBox ID="txtChildNodeName" Text="ASP.NET" runat="server"></asp:TextBox>
<asp:Button ID="btnAdd" runat="server" Text="Add Node" OnClick="btnAdd_Click" />
</div>
</form>
</body>
</html>
Web.sitemap 文件:
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0">
<siteMapNode title="Home" description="Home" url="Default.aspx" >
<siteMapNode title="Login">
</siteMapNode>
<siteMapNode title="Members" description="Members" url="test/test2.aspx" >
<siteMapNode title="My Account" description="My Account" url="test/test3.aspx" />
<siteMapNode title="Products" description="Products" url="test/test4.aspx" />
</siteMapNode>
</siteMapNode>
</siteMap>
<siteMapNode title="Home" description="Home" url="Default.aspx" >
<siteMapNode title="Login">
</siteMapNode>
<siteMapNode title="Members" description="Members" url="test/test2.aspx" >
<siteMapNode title="My Account" description="My Account" url="test/test3.aspx" />
<siteMapNode title="Products" description="Products" url="test/test4.aspx" />
</siteMapNode>
</siteMapNode>
</siteMap>
如有写的不好的地方,请帮我指出; 如果您有更好的方法,也请不吝啬,共享下,谢谢了先!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述