解决方案 »
- 动态添加linkbutton遇到这样的问题
- 为什么我用time控制按钮的enabled不行?求高手!!
- vs2005设置断点,然后F5,调试模式启动不了
- 我用C#做留言本的问题,给我看看谢谢
- .Dll组建是否可以反编译成为.VB文件
- WEB控间的textbox如何获得onclick()事件和onfouce()事件
- 我想把Request.Params["id"]得到的值赋给一个变量(char类型)
- 在定制控件中如何捕获DropDownList控件的SelectedIndexChanged事件?
- 做一个web版的IIS管理程序,很多不解!
- 写自定义控件时,能不能看见此控件?
- 【求助】ASP.NET中使用ADO
- 提问:Frame之间的网页跳转
//params : a dataset contains three columns (ID , NAME , PARENT_ID)
namespace Web.ASCX
{
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Telerik.WebControls; /// <summary>
/// Tree
/// </summary>
public class Tree : System.Web.UI.UserControl
{
protected Telerik.WebControls.RadTreeView RadTreeView1;
private DataSet oDsNodes = null; #region Disp
public void Disp()
{
if(oDsNodes == null)
{
throw new ASCXException("Nodes collection can not empty!");
}
oDsNodes.Tables[0].Columns.Add("COL1" , typeof(bool));
for(int i = 0 ; i < oDsNodes.Tables[0].Rows.Count ; i++)
{
int nOrgID = int.Parse(oDsNodes.Tables[0].Rows[i]["ID"].ToString().Trim());
oDsNodes.Tables[0].Rows[i]["COL1"] = false;
for(int j = 0 ; j < oDsNodes.Tables[0].Rows.Count ; j++)
{
int nParentID = int.Parse(oDsNodes.Tables[0].Rows[j]["PARENT_ID"].ToString().Trim());
if(nOrgID == nParentID)
{
oDsNodes.Tables[0].Rows[i]["COL1"] = true;
break;
}
}
}
for(int i = 0 ; i < oDsNodes.Tables[0].Rows.Count ; i++)
{
int nID = int.Parse(oDsNodes.Tables[0].Rows[i]["ID"].ToString().Trim());
string sName = oDsNodes.Tables[0].Rows[i]["NAME"].ToString().Trim();
int nParentID = int.Parse(oDsNodes.Tables[0].Rows[i]["PARENT_ID"].ToString().Trim());
if(nParentID == 0)
{
RadTreeView1.Nodes.Add(CreateNode(nID , sName));
}
}
RadTreeView1.ExpandAllNodes();
RadTreeView1.Skin = Application["TREE_STYLE"].ToString().Trim();
}
#endregion
#region CreateNode
private RadTreeNode CreateNode(int nID , string sName)
{
RadTreeNode tnRoot = new RadTreeNode();
tnRoot.Text = sName;
tnRoot.ID = nID.ToString();
for(int i = 0 ; i < oDsNodes.Tables[0].Rows.Count ; i++)
{
int nParentID = int.Parse(oDsNodes.Tables[0].Rows[i]["PARENT_ID"].ToString());
if(nID == nParentID)
{
int nTempID = int.Parse(oDsNodes.Tables[0].Rows[i]["ID"].ToString().Trim());
string sTempName = oDsNodes.Tables[0].Rows[i]["NAME"].ToString().Trim();
bool bHasChild = (bool)(oDsNodes.Tables[0].Rows[i]["COL1"]);
if(bHasChild)
{
tnRoot.Nodes.Add(CreateNode(nTempID , sTempName));
}
else
{
RadTreeNode tn = new RadTreeNode();
tn.Text = sTempName;
tn.ID = nTempID.ToString();
tnRoot.Nodes.Add(tn);
}
}
}
return tnRoot;
}
#endregion #region Web
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{ }
#endregion #region Attributes
public DataSet Nodes
{
get{return oDsNodes;}
set{oDsNodes = value;}
} public RadTreeNodeCollection RadTreeNodes
{
get{return RadTreeView1.Nodes;}
}
#endregion
}
}
<style>
<!--
.ptr{color:red;cursor:hand;font-family:webdings}
-->
</style>
<script>
function showandshade()
{
if(span1.innerText==3)
{
span1.innerText=4
document.all("td1").style.display="none"
}
else
{
span1.innerText=3
document.all("td1").style.display=""
}
}
</script>
<body leftMargin="0" topMargin="0" scroll=no>
<table width="100%" height="100%" border=0 cellSpacing="0" cellPadding="0">
<tr height="100%">
<td id="td1" width="50" height="100%" style="display:none"><iframe src=left.htm frameborder=0 scrolling=no width="70"></iframe></td>
<td bgColor=#a4b6d7 onclick="showandshade()" style="font-size:7pt;width:5pt;cursor:default;color=:#ffffff"><span id="span1" class="ptr">4</span><font size="1px" color=green><br/>点<br/>击<br/>展<br/>开</font></td>
<td><iframe name="main" src=right.htm frameborder=0 scrolling=yes height="100%" width="100%"></frame></td>
</tr>
</table>
</body>
<SCRIPT>
if(window.screen.width<'1024'){showandshade()}
</SCRIPT>
---------------------------------------------
.NET平台下Web树形结构程序设计
--------------------------------------------------
我的上篇文章《树形结构在开发中的应用》主要是在Windows Form下的实现,下面是Web Form下的实现。
数据库设计
首先,我们在SQL SERVER 2000里建立一个表tbTree,表的结构设计如下:
列名 数据类型 描述 长度 主键
ID Int 节点编号 4 是
ParentID Int 父节点编号 4
ConText Nvarchar 我们要显示的节点内容 50
在SQL SERVER 2000中建表的脚本:
CREATE TABLE [dbo].[tbTree] ( [ID] [int] IDENTITY (1, 1) NOT NULL , [Context] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL , [ParentID] [int] NULL ) ON [PRIMARY]
在表中添加如下记录:
SET IDENTITY_INSERT tbtree ONinsert tbtree (ID,Context,ParentID) values ( 1,'中国',0)insert tbtree (ID,Context,ParentID) values ( 2,'北京',1)insert tbtree (ID,Context,ParentID) values ( 3,'天津',1)insert tbtree (ID,Context,ParentID) values ( 4,'河北省',1)insert tbtree (ID,Context,ParentID) values ( 5,'广东省',1)insert tbtree (ID,Context,ParentID) values ( 6,'广州',5)insert tbtree (ID,Context,ParentID) values ( 7,'四川省',1)insert tbtree (ID,Context,ParentID) values ( 8,'成都',7)insert tbtree (ID,Context,ParentID) values ( 9,'深圳',5)insert tbtree (ID,Context,ParentID) values ( 10,'石家庄',4)insert tbtree (ID,Context,ParentID) values ( 11,'辽宁省',1)insert tbtree (ID,Context,ParentID) values ( 12,'大连',11)insert tbtree (ID,Context,ParentID) values ( 13,'上海',1)insert tbtree (ID,Context,ParentID) values ( 14,'天河软件园',6)insert tbtree (ID,Context,ParentID) values ( 15,'汕头',5)SET IDENTITY_INSERT tbtree off
下载Treeview控件地址
http://msdn.microsoft.com/downloads/samples/internet/ASP_DOT_NET_ServerControls/WebControls/default.asp
安装后,通过“自定义工具箱”->“.net框架组件”把TreeView添加到工具箱里。
新建一个项目,选择Visual Basic.Net 工程Asp.net Web应用程序,在页面上拖画一个TreeView控件。
Html页:
<%@ Register TagPrefix="iewc" Namespace="Microsoft.Web.UI.WebControls" Assembly="Microsoft.Web.UI.WebControls, Version=1.0.2.226, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %><%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="Tree.WebForm1"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML> <HEAD> <title>WebForm1</title> <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.0"> <meta name="CODE_LANGUAGE" content="Visual Basic 7.0"> <meta name="vs_defaultClientScript" content="JavaScript"> <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5"> </HEAD> <body MS_POSITIONING="GridLayout"> <form id="Form1" method="post" runat="server"> <FONT face="宋体"> <iewc:TreeView id="TreeView1" style="Z-INDEX: 101; LEFT: 39px; TOP: 68px" runat="server"></iewc:TreeView></FONT> </form> </body></HTML>
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim ds As New DataSet() Dim CN As New SqlConnection() Try '初始化连接字符串 CN.ConnectionString = "data source=pmserver;initial catalog=Bench;persist security info=False;user id=sa;Password=sa;" CN.Open() Dim adp As SqlDataAdapter = New SqlDataAdapter("select * from tbTree", CN) adp.Fill(ds) Me.ViewState("ds") = ds Catch ex As Exception#If DEBUG Then Session("Error") = ex.ToString() Response.Redirect("error.aspx") '̀跳转程序的公共错误处理页面#End If Finally '关闭连接 CN.Close() End Try '调用递归函数,完成树形结构的生成 AddTree(0, Nothing) End Sub '递归添加树的节点 Private Sub AddTree(ByVal ParentID As Integer, ByVal pNode As TreeNode) Dim ds As DataSet ds = Me.ViewState("ds") Dim dvTree As New DataView() dvTree = New DataView(ds.Tables(0)) '过滤ParentID,得到当前的所有子节点 dvTree.RowFilter = "PARENTID = " + ParentID.ToString Dim Row As DataRowView For Each Row In dvTree Dim Node As New TreeNode() If pNode Is Nothing Then '判断是否根节点 '添加根节点 Node.Text = Row("ConText").ToString() TreeView1.Nodes.Add(Node) Node.Expanded = True '再次递归 AddTree(Int32.Parse(Row("ID").ToString()), Node) Else '̀添加当前节点的子节点 Node.Text = Row("ConText").ToString() pNode.Nodes.Add(Node) Node.Expanded = True '再次递归 AddTree(Int32.Parse(Row("ID").ToString()), Node) End If Next End Sub
C#版本:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Microsoft.Web.UI.WebControls;
using System.Data.SqlClient;
namespace TreeCS
{
/// <summary>
/// WebForm1 的摘要说明
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected Microsoft.Web.UI.WebControls.TreeView TreeView1;
private void Page_Load(object sender, System.EventArgs e)
{
// 定义数据库连接
SqlConnection CN = new SqlConnection();
try
{
//初始化连接字符串
CN.ConnectionString= "data source=pmserver;initial catalog=Bench;persist security info=False;user id=sa;Password=sa;";
CN.Open();
SqlDataAdapter adp = new SqlDataAdapter("select * from tbTree",CN);
DataSet ds=new DataSet();
adp.Fill(ds);
this.ViewState["ds"]=ds;
}
catch (Exception ex)
{
Session["Error"] = ex.ToString();
Response.Redirect("error.aspx"); //̀跳转程序的公共错误处理页面
}
finally
{
CN.Close();
}
//调用递归函数,完成树形结构的生成
AddTree(0, (TreeNode)null);
}
//递归添加树的节点
public void AddTree(int ParentID,TreeNode pNode)
{
DataSet ds=(DataSet) this.ViewState["ds"];
DataView dvTree = new DataView(ds.Tables[0]);
//过滤ParentID,得到当前的所有子节点
dvTree.RowFilter = "[PARENTID] = " + ParentID;
foreach(DataRowView Row in dvTree)
{
TreeNode Node=new TreeNode() ;
if(pNode == null)
{ //添加根节点
Node.Text = Row["ConText"].ToString();
TreeView1.Nodes.Add(Node);
Node.Expanded=true;
AddTree(Int32.Parse(Row["ID"].ToString()), Node); //再次递归
}
else
{ //̀添加当前节点的子节点
Node.Text = Row["ConText"].ToString();
pNode.Nodes.Add(Node);
Node.Expanded = true;
AddTree(Int32.Parse(Row["ID"].ToString()),Node); //再次递归
}
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
///设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}