数据结构如下:
CREATE TABLE dtree (
idint,
pid int,
name varchar(200),
url varchar(200),
title varchar(200),
target varchar(200),
iconvarchar(200),
iconopen varchar(200),
openedbit)
为了实现获取数据库变量功能,需要建立一个DTree类,并编译生成CLASS文件,放入\WEB-INF\classes文件夹下。
DTree类代码如下:
package work3
public class DTree {
private int id
private int pid
private String name
private String url
private String title
private String target
private String icon
private String iconOpen
private int opened
public int getId() {
return id
}
public void setId(int id) {
this.id = id
}
public int getPid() {
return pid
}
public void setPid(int pid) {
this.pid = pid
}
public String getName() {
return name
}
public void setName(String name) {
this.name = name
}
public String getUrl() {
return url
}
public void setUrl(String url) {
this.url = url
}
public String getTitle() {
return title
}
public void setTitle(String title) {
this.title = title
}
public String getTarget() {
return target
}
public void setTarget(String target) {
this.target = target
}
public String getIcon() {
return icon
}
public void setIcon(String icon) {
this.icon = icon
}
public String getIconOpen() {
return iconOpen
}
public void setIconOpen(String iconOpen) {
this.iconOpen = iconOpen
}
public int getOpened() {
return opened
}
public void setOpened(int opened) {
this.opened = opened
}
}
work3.jsp代码如下:
<%@ page language="java" contentType="text/htmlcharset=GB18030" pageEncoding="GB18030"%>
<%@ page import="java.sql.*"%>
<jsp:useBean id='settree' scope="application" class="work3.DTree" />
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/htmlcharset=GB18030">
<link rel="StyleSheet" href="dtree.css" type="text/css" />
<script type="text/javascript" src="dtree.js"></script>
<title>dTree in MySQL</title>
</head>
<body>
<h2>
Example
</h2>
<div class="dtree">
<p>
<a href="javascript: d.openAll()">open all</a>|
<a href="javascript: d.closeAll()">close all</a>
</p>
<script type="text/javascript">
<!--
d = new dTree('d')
<%
//驱动程序名
String driverName = "com.microsoft.jdbc.sqlserver.SQLServerDriver"
//数据库用户名
String userName = "sa"
//密码
String userPwd = "1"
//数据库名
String dbName = "master"
//表名
String tableName = "dtree"
//连接字符串
String url="jdbc:microsoft:sqlserver://localhost:1433DatabaseName="+dbName
//加载驱动
Class.forName(driverName).newInstance()
//连接数据库
java.sql.Connection conn = DriverManager.getConnection(url,userName,userPwd)
//得到Statement实例
java.sql.Statement statement = conn.createStatement()
//查询数据
String sql = "select * from " + tableName
//返回结果
java.sql.ResultSet rs = statement.executeQuery(sql)
//获取变量
while (rs.next()) {
settree.setId(rs.getInt(1))
settree.setPid(rs.getInt(2))
settree.setName(rs.getString(3))
settree.setUrl(rs.getString(4))
settree.setTitle(rs.getString(5))
settree.setTarget(rs.getString(6))
settree.setIcon(rs.getString(7))
settree.setIconOpen(rs.getString(8))
settree.setOpened(rs.getInt(9))
if(settree.getPid()==0)
settree.setOpened(1)
%>
d.add(<%=settree.getId()%>,<%=settree.getPid()%>,'<%=settree.getName()%>','<%=settree.getUrl()%>','<%=settree.getTitle()%>','<%=settree.getTarget()%>','','',<%=settree.getOpened()%>)
<%
}
%>
document.write(d)
//-->
</script>
</div>
</body>
</html>
来自csdn上面的例子我试过可以1.下载地址
http://msdn.microsoft.com/downloads/samples/internet/ASP_DOT_NET_ServerControls/WebControls/default.asp
安装后,通过“自定义工具箱”->“.net框架组件”把TreeView添加到工具箱里
2.运行时无法显示
一般是TreeView的版本问题,最好下载英文版自动安装版本重新安装,安装前应该先到添加删除程序里卸掉原版本
3.显示格式出错(非树状显示)
TreeView要求客户端浏览器版本为IE5.5及以上,最好要求客户端升级为IE6.0
4.框架里使用TreeView
设置NavigateUrl、Target属性,可更新另外的Frame
5.找不到TreeNode类
使用TreeView,最好添加namespace:using Microsoft.Web.UI.WebControls
6.遍历TreeView节点(递归算法)
private void Page_Load(object sender, System.EventArgs e)
{
GetAllNodeText(TreeView1.Nodes)
}
void GetAllNodeText(TreeNodeCollection tnc)
{
foreach(TreeNode node in tnc)
{
if(node.Nodes.Count!=0)
GetAllNodeText(node.Nodes)
Response.Write(node.Text + " ")
}
}
7.得到node结点的父节点
TreeNode pnode
if(node.Parent is TreeNode)
pnode=(TreeNode)node.Parent
else
//node is root node
8.修改TreeView样式(示例)
<iewc:TreeView id="TreeView1" runat="server" HoverStyle="color:bluebackground:#00ffCC"
DefaultStyle="background:redcolor:yellow" SelectedStyle="color:redbackground:#00ff00">
用代码:
TreeView1.DefaultStyle["font-size"] = "20pt"
9.TreeView展开无闪烁的解决方法
http://expert.csdn.net/Expert/topic/1300/1300576.xml
客户端控制TreeView
http://expert.csdn.net/Expert/topic/1382/1382892.xml
1.设置所选节点,如选中第二个节点
function SetSelNode()
{
TreeView1.selectedNodeIndex="1"
}
2.得到所选节点的Text,ID或NodeData
function GetAttribute()
{
alert(TreeView1.getTreeNode(TreeView1.selectedNodeIndex).getAttribute("Text"))
}
替换Text为ID或NodeData,可分别得到所选节点的ID或NodeData
3.修改节点属性,如修改第一个节点的Text
function ModifyNode()
{
var node=TreeView1.getTreeNode("0")
node.setAttribute("Text","hgknight")
}
4.得到点击节点
function TreeView1.onclick()
{
alert(TreeView1.getTreeNode(TreeView1.clickedNodeIndex).getAttribute("Text"))
}
5.添加节点
function AddNode()
{
var node=TreeView1.createTreeNode()
node.setAttribute("Text","hgknight")
TreeView1.add(node)
}
6.js遍历所有节点
//只针对根结点,大家把它可改为递归形式
var NodeArray=new Array()
NodeArray=TreeView1.getChildren()
for(i=0i<NodeArray.lengthi++)
{
cNode=NodeArray[i]
alert(cNode.getAttribute("Text"))
}
1、用代码在treeview web控件中,添加node的方法
表字段:编号,父编号,名称
数据:
1 0 中华人民共和国
2 1 湖南
3 1 湖北
void creattree(string text,TreeNode tnd,string pid)
{
TreeNode t2=new TreeNode()
t2.Text=text
tnd.Nodes.Add(t2)
生成
string str1 ="父编号 = '"+pid+"'"
DataRow[] dryl = dataSet31.Tables [0].Select(str1)
for(int i=0i<=dryl.GetUpperBound(0)i++)
{
creattree(dryl[i][2].ToString(),t2,dryl[i][0].ToString())
}
}
//
TreeNode t1=new TreeNode()
t1.Text="地区信息"
TreeView2.Nodes.Add(t1)
creattree("中华人民共和国",t1,"1")
2、如何为TreeView添加事件?
Private Sub TreeView1_indexChanged(ByVal sender As Object, ByVal e As
Microsoft.Web.UI.WebControls.TreeViewSelectEventArgs) Handles
TreeView1.SelectedIndexChange
Dim a As Integer
a = TreeView1.SelectedNodeIndex
TextBox1.Text = CStr(a)
End Sub
但是AutoPostBack设置为True的话,TreeView还要刷新,闪烁,我的TreeView比较庞大
,这可怎么办???苦恼中。。。
3、为什么TreeView 中的SelectedIndexChange为什么不执行???
AutoPostBack属性为真,SelectedIndexChange才能被执行。
不过这样的话你选择时会感到刷新得让你痛苦,根据你的具体实现的目的,采用变通的
方法,尽量不使用SelectedIndexChange事件吧。
selectedindexchange事件挂到控件上没有?
InitializeComponent方法中检查检查
4、如何判断 TreeView 的一个节点下是否有子节点???
if(selNode.Nodes.Count==0){
//该节点没有字节点
}
5、如何得到 TreeView 的当前选择节点???
1.Tree.GetNodeFromIndex(Tree.SelectedNodeIndex.ToString())
2.private void TreeView1_SelectedIndexChange(object sender,
Microsoft.Web.UI.WebControls.TreeViewSelectEventArgs e)
e.NewNode即为所选节点
6、treeview实现两层节点没问题,扩展到无限层就出问题了?
我的那个做法还不够完善,对于节点数较少的情况可以这样做,对于节点数较多的情况
,你就不能这样做了,你应该只加载一级,当点击节点展开时,再加载它的下一级子节
点。
数据库结构如下:
表名:Table1
ParentID varchar 20:父节点
ID varchar 20:节点
Name varchar 50:名称
来来来,这里给你个例程,再给个我的网页上的TreeViwe看看,一万个子节点都没问题。
http://dev1.eraserver.net/czc888/webform1.aspx
至于那三个图象的意义你看我的网页就知道了。分别表示一页纸(没有子节点)、一本
关闭的书(有子节点),一本打开的书(当前节点被展开)
void creat_node(string
name,Microsoft.Web.UI.WebControls.TreeNode t1,string number)
{
//定义一个新的结点
Microsoft.Web.UI.WebControls.TreeNode t2=new
Microsoft.Web.UI.WebControls.TreeNode()
t2.ID=number
t2.Text=name
//判断是否为根结点
TreeView1.ExpandedImageUrl="images/Depart03.bmp"
if(t1.ID.Trim()=="###")
{
TreeView1.Nodes.Add(t2)
t2.ImageUrl="images/Depart01.bmp"
}
else
{
t1.Nodes.Add(t2)
t2.ImageUrl="images/Depart01.bmp"
t1.ImageUrl="images/Depart02.bmp"
}
//取得这个结点的所有子结点,利用递归自动生成
string mySelectQuery = "SELECT ParentID, ID,Name FROM table1
where ParentID='" + number +"'"
string MyConnString="provider=Microsoft.Jet.OLEDB.4.0Data
Source="+Server.MapPath(".")+"/dt/mydata.mdb"
OleDbConnection myConnection = new
OleDbConnection(MyConnString)
OleDbCommand myCommand = new
OleDbCommand(mySelectQuery,myConnection)
myConnection.Open()
OleDbDataReader myReader
myReader = myCommand.ExecuteReader()
while (myReader.Read())
{
creat_node(myReader.GetString(2),t2,myReader.GetString(1))
}
myReader.Close()
myConnection.Close()
}
private void CreateProviderTreeBtn_Click(object sender,
System.EventArgs e)
{
Microsoft.Web.UI.WebControls.TreeNode t1=new
Microsoft.Web.UI.WebControls.TreeNode()
creat_node("",t1,"###")
7、怎样使用TreeView???
//初始化TreeView数据
private void InitTV(int intVOrderLen)
{
SqlDataReader dr
SqlCommand myCommand
string strSel = "select * from bas_enterprise where
len(vieworder)="+intVOrderLen+" order by vieworder"
myCommand = new SqlCommand (strSel, myConnection)
dr=myCommand.ExecuteReader()
while (dr.Read ())
{
TreeNode pNode=new TreeNode ()
pNode.Text =dr["EntpName"].ToString ()
pNode.ID =dr["ViewOrder"].ToString ()
TreeView1.Nodes .Add (pNode)
}
dr.Close ()
}
private void TreeView1_SelectedIndexChange(object sender,
Microsoft.Web.UI.WebControls.TreeViewSelectEventArgs e)
{
TreeNode NewNode=TreeView1.GetNodeFromIndex
(TreeView1.SelectedNodeIndex)
string strNodeId=NewNode.ID .ToString ()
SqlDataReader dr
SqlCommand myCommand
int intVOrderLen=strNodeId.Length+2
string strSel = "select * from bas_enterprise where
len(vieworder)="+intVOrderLen+" and vieworder like '%"+strNodeId+"__%'
order by vieworder"
myCommand = new SqlCommand (strSel, myConnection)
dr=myCommand.ExecuteReader()
NewNode.Nodes .Clear()
while (dr.Read ())
{
TreeNode pNode=new TreeNode ()
pNode.Text =dr["EntpName"].ToString ()
pNode.ID =dr["ViewOrder"].ToString ()
NewNode.Nodes.Add (pNode)
}
dr.Close ()
TreeView1.ExpandLevel=1
}
8、在treeview中如何查找一个值,并选中它?
问题:
各位,在已经建好的树中如何查找一个文本或者是treenodes的id,找到后选中它?
答案:
for(int i=0i<TreeView1.Nodes.Counti++)
if(TreeView1.Nodes[i]==TextBox1.Text)
{
string index1=TreeView1.Nodes[i].GetNodeIndex()//取得index
TreeView1.SelectedNodeIndex=index1
}
9、如何取得TreeView中各项的值??
private void TreeView1_SelectedIndexChange(object sender,
Microsoft.Web.UI.WebControls.TreeViewSelectEventArgs e)
{
Microsoft.Web.UI.WebControls.TreeNode
NewNode=TreeView1.GetNodeFromIndex(TreeView1.SelectedNodeIndex)
Label1.Text =NewNo
如果通过代码要在父结点中增加子节点,代码如何实现?
父节点代码如下:
TreeNode pNode = new TreeNode()
pNode.ID = "Root"
pNode.Text = strNodeName
TreeView1.Nodes.Add(pNode)
如果要在根结点"Root"下面再增加子节点,如何写代码?
TreeView1.Nodes.AddAt(pNode)这种方式好象不行.
TreeView1.Nodes[0].Nodes.Add(pNewNode)
/*************.Cs 中的代码****************************
string strScript="<script language=\"javascript\" >"
strScript+=" function GetNode() \n"+"{ \n"
strScript+=" alert(TreeView1.getTreeNode(TreeView1.clickedNodeIndex).getAttribute('Text'))\n"
strScript+=" alert(TreeView1.getTreeNode(TreeView1.clickedNodeIndex).getAttribute('CheckBox'))\n"
strScript+=" alert(TreeView1.getTreeNode(TreeView1.clickedNodeIndex).getAttribute('Checked'))\n"
strScript+=" if (TreeView1.getTreeNode(TreeView1.clickedNodeIndex).getAttribute('Checked'))\n"
strScript+=" TreeView1.getTreeNode(TreeView1.clickedNodeIndex).setAttribute('Checked','false')\n"
strScript+=" else \n"
strScript+=" TreeView1.getTreeNode(TreeView1.clickedNodeIndex).setAttribute('Checked','true')\n"
strScript+=" \n } \n"
strScript+="</script>"
if(! this.IsClientScriptBlockRegistered("myjs"))
{
this.RegisterClientScriptBlock("myjs",strScript)
}
***************************************************//
现在我想在客户端点击父节点的checkbox则下级也被选中,可是使用下面的代码,只能表面选中,在服务器端却得不到,请问有什么其它好法子
<script language=\"javascript\" >
function GetNode()
{
var choose
var indexid
indexid=TreeView1.clickedNodeIndex
if (TreeView1.getTreeNode(indexid).getAttribute('Checked'))
{
choose=false
}
else
{
choose=true
}
TreeView1.getTreeNode(indexid).setAttribute('Checked',choose)
var NodeArray=new Array()
NodeArray=TreeView1.getTreeNode(indexid).getChildren()
for(i=0i<NodeArray.lengthi++)
{
var tempindex=indexid+"."+i
TreeView1.getTreeNode(indexid).setAttribute('Checked',choose)
}
}
//-->
</script>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)