因为客户需要用树型的数据查询模式,我就想到了用TreeView,以前从来没用过,要求通过编码来自动生成树,比如01,0101,010001,01011101,这样的编码,就生成三级树,根是01,0101和010001这两个编号是01的子叶,01011101是0101下的子叶,算法这些都能做出来,就是不知道怎么来绑定数据,如何自动生成这棵树,请高手指点一下,谢谢!
调试欢乐多
<head runat="server">
<title>无标题页</title>
</head>
<script language="javascript">
function focus()
{
document.getElementById('TreeView1').focus();
}
</script>
<body onload="focus()">
<form id="form1" runat="server">
<div>
<asp:TreeView ID="TreeView1" runat="server" ExpandDepth="0" ShowLines ="true"
Height="300px" NoExpandImageUrl="~/images/folder.small.png" ImageSet="Msdn" NodeIndent="10">
<RootNodeStyle BorderStyle="Dotted" />
<LeafNodeStyle BorderStyle="Dotted" />
<NodeStyle BorderStyle="Dotted" Font-Names="Verdana" Font-Size="8pt" ForeColor="Black" HorizontalPadding="5px" NodeSpacing="1px" VerticalPadding="1px" />
<HoverNodeStyle BorderStyle="Solid" BackColor="#CCCCCC" BorderColor="#888888" Font-Underline="True" />
<SelectedNodeStyle BorderStyle="Solid" BackColor="White" BorderColor="#888888" BorderWidth="1px" Font-Underline="False" HorizontalPadding="3px" VerticalPadding="1px" />
<ParentNodeStyle Font-Bold="False" />
</asp:TreeView>
<iframe name="ModiDepart" id="ModiDepart" src="room_fee_item_index.asp" target="_self" style="height:575;width:100%">浏览器不支持嵌入式框架,或被配置为不显示嵌入式框架。 </iframe>
</div>
</form>
</body>
</html>
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.Data.Sql
Imports System.Data.SqlClient
Partial Class tree_tree
Inherits System.Web.UI.Page Private ConnStr As String
Private MyConnection As SqlConnection
Sub AddAreaTreeNodes() '添加小区节点
Dim sql_yhmc As String
sql_yhmc = "select * from t_area "
Dim insert_yhmc As SqlCommand = New SqlCommand(sql_yhmc, MyConnection) Dim rs_yhmc As SqlDataReader = insert_yhmc.ExecuteReader Do While rs_yhmc.Read
Dim root As New TreeNode()
root = New TreeNode(rs_yhmc.Item("a_name"))
root.Value = "Root" + rs_yhmc.Item("a_id") root.ChildNodes.Add(New TreeNode("")) '必须先添加一个子节点才能出现+号,(待解决)
TreeView1.Nodes.Add(root)
Loop
End Sub Sub AddBuildTreeNodes_root() '添加小区节点
Dim sql_yhmc As String
sql_yhmc = "select * from t_building where a_id='" & Session("a_id") & "' "
Dim insert_yhmc As SqlCommand = New SqlCommand(sql_yhmc, MyConnection) Dim rs_yhmc As SqlDataReader = insert_yhmc.ExecuteReader Do While rs_yhmc.Read
Dim root As New TreeNode()
root = New TreeNode(rs_yhmc.Item("b_name")) root.Value = "Root" + rs_yhmc.Item("b_id").ToString root.ChildNodes.Add(New TreeNode("")) '必须先添加一个子节点才能出现+号,(待解决)
TreeView1.Nodes.Add(root)
Loop
End Sub Sub AddBuildingTreeNodes(ByVal parentNode As TreeNode, ByVal strSql As String)
Dim insert_yhmc As SqlCommand = New SqlCommand(strSql, MyConnection) Dim rs_yhmc As SqlDataReader = insert_yhmc.ExecuteReader
Dim node As New TreeNode
Do While rs_yhmc.Read node = New TreeNode(rs_yhmc.Item("b_name"))
node.Value = "Buil" + rs_yhmc.GetInt32(1).ToString() + "," + parentNode.Value.Substring(4) 'Substing方法用来去掉Buil
parentNode.ChildNodes.Add(node)
node.ChildNodes.Add(New TreeNode(""))
Loop
node.NavigateUrl = "postinfo.html" node.Target = "ModiDepart"
End Sub
Sub AddModuleTreeNodes(ByVal parentNode As TreeNode, ByVal strSql As String, ByVal strSql_lc As String, ByVal strSql_lc1 As String) '添加单元节点 '楼层
Dim ConnStr_lc As String ConnStr_lc = System.Configuration.ConfigurationSettings.AppSettings("kingseer_dbConnectionString").ToString
Dim MyConnection_lc As SqlConnection MyConnection_lc = New SqlConnection(ConnStr_lc)
MyConnection_lc.Open() Dim insert_yhmc1 As SqlCommand = New SqlCommand(strSql_lc, MyConnection_lc) Dim rs_yhmc1 As SqlDataReader = insert_yhmc1.ExecuteReader If rs_yhmc1.Read And Convert.ToDouble(rs_yhmc1.Item("b_unit_num")) > 1 Then Dim insert_yhmc As SqlCommand = New SqlCommand(strSql, MyConnection) Dim rs_yhmc As SqlDataReader = insert_yhmc.ExecuteReader
Dim node As New TreeNode Do While rs_yhmc.Read
node = New TreeNode(rs_yhmc.Item("r_dy") & "单元")
node.Value = "Modu" + rs_yhmc.GetInt32(0).ToString + "," + parentNode.Value.Substring(4)
parentNode.ChildNodes.Add(node)
node.ChildNodes.Add(New TreeNode(""))
Loop
Else Dim insert_yhmc As SqlCommand = New SqlCommand(strSql_lc1, MyConnection) Dim rs_yhmc As SqlDataReader = insert_yhmc.ExecuteReader
Dim node As New TreeNode Do While rs_yhmc.Read
node = New TreeNode(rs_yhmc.Item("r_lc") & "层")
node.Value = "LCLC" + rs_yhmc.GetInt32(0).ToString + "," + parentNode.Value.Substring(4)
parentNode.ChildNodes.Add(node)
node.ChildNodes.Add(New TreeNode(""))
Loop
End If End Sub Sub AddRoomTreeNodes(ByVal parentNode As TreeNode, ByVal strSql As String) '添加房间节点
Dim insert_yhmc As SqlCommand = New SqlCommand(strSql, MyConnection) Dim rs_yhmc As SqlDataReader = insert_yhmc.ExecuteReader Do While rs_yhmc.Read
Dim node As New TreeNode
node = New TreeNode(rs_yhmc.Item("r_id"))
node.Value = "Modu" + rs_yhmc.GetInt32(3).ToString + "," + parentNode.Value.Substring(4)
parentNode.ChildNodes.Add(node)
' node.ChildNodes.Add(New TreeNode(""))
Loop End Sub Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load ConnStr = System.Configuration.ConfigurationSettings.AppSettings("kingseer_dbConnectionString").ToString MyConnection = New SqlConnection(ConnStr)
MyConnection.Open()
If Not IsPostBack And Session("a_id") = "" Then
AddAreaTreeNodes()
ElseIf Not IsPostBack And Session("a_id") <> "" Then
AddBuildTreeNodes_root()
End If End Sub
Protected Sub TreeView1_TreeNodeExpanded(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.TreeNodeEventArgs) Handles TreeView1.TreeNodeExpanded
e.Node.ChildNodes.Clear()
AddNodes(e.Node)
End Sub
Sub AddNodes(ByVal parentNode As TreeNode)
'在此方法中判断点击的是哪一个节点(分别用四个单词区分)
Dim strValue As String
strValue = parentNode.Value
Dim nodeId As String
nodeId = strValue.Substring(4)
Dim strSql, sqlSql_lc, strSql_lc1 As String
Dim ids() As String
If strValue.StartsWith("Root") Then '去掉父节点的头 If Session("a_id") <> "" Then
ids = Split(nodeId, ",")
strSql = "select distinct r_dy from t_room where b_id=" + ids(0) + "and a_id=" + Session("a_id") + "order by r_dy" '添加单元 sqlSql_lc = "Select * from t_building where b_id=" + ids(0) + "and a_id=" + Session("a_id") + "order by b_id" '添加单元
strSql_lc1 = "select distinct r_lc from t_room where b_id=" + ids(0) + "and a_id=" + Session("a_id") + "order by r_lc" '添加单元
AddModuleTreeNodes(parentNode, strSql, sqlSql_lc, strSql_lc1)
Else strSql = "select * from t_building where a_id=" + nodeId '添加楼座 AddBuildingTreeNodes(parentNode, strSql) End If ElseIf strValue.StartsWith("Buil") Then ids = Split(nodeId, ",")
If Session("a_id") <> "" Then strSql = "select distinct r_dy from t_room where b_id=" + ids(0) + "and a_id=" + Session("a_id") + "order by r_dy" '添加单元
sqlSql_lc = "Select * from t_building where b_id=" + ids(0) + "and a_id=" + Session("a_id") + "order by b_id" '添加单元
strSql_lc1 = "select distinct r_lc from t_room where b_id=" + ids(0) + "and a_id=" + Session("a_id") + " order by r_lc" '添加单元 Else
strSql = "select distinct r_dy from t_room where b_id=" + ids(0) + "and a_id=" + ids(1) + " order by r_dy" '添加单元 sqlSql_lc = "Select * from t_building where b_id=" + ids(0) + "and a_id=" + ids(1) + "order by b_id" '添加单元
strSql_lc1 = "select distinct r_lc from t_room where b_id=" + ids(0) + "and a_id=" + ids(1) + " order by r_lc" '添加单元 End If AddModuleTreeNodes(parentNode, strSql, sqlSql_lc, strSql_lc1) ElseIf strValue.StartsWith("Modu") Then ids = Split(nodeId, ",")
If Session("a_id") <> "" Then
strSql = "select * from t_room where r_dy='" & ids(0) & "' and b_id=" + ids(1) + " and a_id=" + Session("a_id") + "order by r_id"
Else
strSql = "select * from t_room where r_dy='" & ids(0) & "' and b_id=" + ids(1) + " and a_id=" + ids(2) + "order by r_id"
End If
AddRoomTreeNodes(parentNode, strSql) ElseIf strValue.StartsWith("LCLC") Then ids = Split(nodeId, ",")
If Session("a_id") <> "" Then
strSql = "select * from t_room where r_lc='" & ids(0) & "' and b_id=" + ids(1) + " and a_id=" + Session("a_id") + "order by r_id"
Response.Write(strSql)
Else
strSql = "select * from t_room where r_lc='" & ids(0) & "' and b_id=" + ids(1) + " and a_id=" + ids(2) + "order by r_id"
End If
AddRoomTreeNodes(parentNode, strSql) ElseIf strValue.StartsWith("Room") Then End If
End Sub
End Class
借鉴一下,小区 - 楼座 - 单元(如果一个单元就显示楼层)-房间