结合数据库获得父节点和子节点信息数值:
.aspx代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TreeView控件.aspx.cs" Inherits="TreeView控件" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <style type="text/css"> .fl{ float:left;} .fr{ float:right;} .clr{ clear:both;} </style> </head> <body> <form id="form1" runat="server"> <div> <div id="div_treeview" class="fl"> <%=DateTime.Now.ToLongTimeString() %> <asp:TreeView ID="TreeView1" runat="server" ExpandDepth="0" onselectednodechanged="TreeView1_SelectedNodeChanged"> </asp:TreeView> </div> <div id="others" class="fr"> <%--为了不使整个页面刷新,引入ajax局部刷新,提高显示速度。以下是标准程序: <asp:ScriptManager ID="ScriptManager2" runat="server"> </asp:ScriptManager> <asp:UpdatePanel ID="UpdatePanel2" runat="server"> <ContentTemplate> 此处填入内容 </ContentTemplate> <Triggers> <asp:AsyncPostBackTrigger ControlID="TreeView1" /> </Triggers> </asp:UpdatePanel>--%> <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> <%=DateTime.Now.ToLongTimeString() %> TEXT:<asp:Label ID="lbl_TEXT" runat="server" Text="Label"></asp:Label> VALUE:<asp:Label ID="lbl_VALUE" runat="server" Text="Label"></asp:Label> VALUEPATH:<asp:Label ID="lbl_VALUEPATH" runat="server" Text="Label"></asp:Label> </ContentTemplate> <Triggers> <asp:AsyncPostBackTrigger ControlID="TreeView1" /> </Triggers> </asp:UpdatePanel> </div> <div class="clr"> </div> </div> </form> </body> </html>
.aspx.cs代码如下:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data.sqlClient; public partial class TreeView控件 : System.Web.UI.Page { string con = "server=localhost\\sql2005 ;uid=sa;pwd=1111qq;database=DBPromary"; sqlConnection conn; sqlCommand cmd; sqlDataReader datar; string str_sql; protected void Page_Load(object sender,EventArgs e) { if (!IsPostBack) { maketree(); } } protected void maketree() { //省 TreeNode _tNode,_ParentNode; conn = new sqlConnection(con); conn.Open(); str_sql="select * from promary"; cmd = new sqlCommand(str_sql,conn); datar = cmd.ExecuteReader(); while (datar.Read()){ _tNode=new TreeNode();//实例化结点 _tNode.Text=datar["proName"].ToString(); _tNode.Value="p_"+datar["proID"].ToString(); TreeView1.Nodes.Add(_tNode);//结点加到TreeView上 } conn.Close(); //city conn = new sqlConnection(con); conn.Open(); str_sql = "select * from city"; cmd = new sqlCommand(str_sql,conn); datar = cmd.ExecuteReader(); while (datar.Read()) { _ParentNode = TreeView1.FindNode("p_"+datar["proID"].ToString());//寻找父节点 _tNode=new TreeNode();//实例化子节点 _tNode.Text=datar["cityName"].ToString();//子节点的显示 _tNode.Value="c_"+datar["cityID"].ToString();//子节点的数值 _ParentNode.ChildNodes.Add(_tNode);//将子节点加到找到的父节点上 } conn.Close(); } protected void TreeView1_SelectedNodeChanged(object sender,EventArgs e) { TreeNode _tnode = TreeView1.SelectedNode;//SelectedNode方法找到选中的结点 lbl_TEXT.Text = _tnode.Text; lbl_VALUE.Text = _tnode.Value; lbl_VALUEPATH.Text = _tnode.ValuePath; } }
效果如下图: