考试,考试,还是考试!无聊透顶的考试!但是不考又不行的考试!为了那一张薄薄的纸!我又白白浪费的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 >
如有写的不好的地方,请帮我指出; 如果您有更好的方法,也请不吝啬,共享下,谢谢了先!