【小山】ASP.NET2.0 TreeView
待續
1.html程式碼
1.html程式碼
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Pagetitle>
<script language="javascript">
function chkAll()
{
//debugger;
var chkall= document.all["chkall"];
var chkother= document.getElementsByTagName("input");
for (var i=0;i<chkother.length;i++)
{
if( chkother[i].type=='checkbox')
{
if(chkother[i].id.indexOf('TreeView1')>-1)
{
if(chkall.checked==true)
{
chkother[i].checked=true;
}
else
{
chkother[i].checked=false;
}
}
}
}
}
script>
head>
<body>
<form id="form1" runat="server">
<table width=100% height=100%>
<tr height=10>
<td><input id="chkall" type="checkbox" onclick="chkAll();" />全選/取消td>
<td><asp:Button ID="Button1" runat="server" Text="Button" />td>
tr>
<tr valign=top>
<td><asp:TreeView ID="TreeView1" runat="server" >asp:TreeView>td>
<td><iframe id=fMain style="BORDER-TOP-STYLE.: none; BORDER-RIGHT-STYLE.: none; BORDER-LEFT-STYLE.: none; BORDER-BOTTOM-STYLE.: none"
src="" frameBorder="0" width="100%" scrolling="yes" height="100%">iframe>td>
tr>
<tr height=10>
<td>td>
tr>
table>
form>
body>
html>
2.cs程式碼<head runat="server">
<title>Untitled Pagetitle>
<script language="javascript">
function chkAll()
{
//debugger;
var chkall= document.all["chkall"];
var chkother= document.getElementsByTagName("input");
for (var i=0;i<chkother.length;i++)
{
if( chkother[i].type=='checkbox')
{
if(chkother[i].id.indexOf('TreeView1')>-1)
{
if(chkall.checked==true)
{
chkother[i].checked=true;
}
else
{
chkother[i].checked=false;
}
}
}
}
}
script>
head>
<body>
<form id="form1" runat="server">
<table width=100% height=100%>
<tr height=10>
<td><input id="chkall" type="checkbox" onclick="chkAll();" />全選/取消td>
<td><asp:Button ID="Button1" runat="server" Text="Button" />td>
tr>
<tr valign=top>
<td><asp:TreeView ID="TreeView1" runat="server" >asp:TreeView>td>
<td><iframe id=fMain style="BORDER-TOP-STYLE.: none; BORDER-RIGHT-STYLE.: none; BORDER-LEFT-STYLE.: none; BORDER-BOTTOM-STYLE.: none"
src="" frameBorder="0" width="100%" scrolling="yes" height="100%">iframe>td>
tr>
<tr height=10>
<td>td>
tr>
table>
form>
body>
html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class Page_Wonderful_TreeView_System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//BindTree();
InitTree();
}
}
主從表繫結#region 主從表繫結
private void BindTree()
{
DataSet dst = GetTreeViewData();
TreeView1.ShowCheckBoxes = TreeNodeTypes.All;
foreach (DataRow masterRow in dst.Tables["province"].Rows)
{
TreeNode masterNode = new TreeNode((string)masterRow["province"]);
TreeView1.Nodes.Add(masterNode);
foreach (DataRow childRow in masterRow.GetChildRows("Children"))
{
TreeNode childNode =new TreeNode((string)childRow["city"]);
masterNode.Expanded = false;
masterNode.ChildNodes.Add(childNode);
}
}
}
private DataSet GetTreeViewData()
{
string constring = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
SqlConnection con = new SqlConnection(constring);
SqlDataAdapter daprovince = new SqlDataAdapter("SELECT * FROM province", con);
SqlDataAdapter dacity = new SqlDataAdapter("SELECT * FROM city", con);
DataSet ds = new DataSet();
daprovince.Fill(ds, "province");
dacity.Fill(ds, "city");
ds.Relations.Add("Children", ds.Tables["province"].Columns["provinceid"], ds.Tables["city"].Columns["father"]);
return ds;
}
#endregion
遞迴繫結同一個表資料#region 遞迴繫結同一個表資料
private void InitTree()
{
DataTable dt = GetTreeViewTable();
DataView dv = new DataView(dt);
dv.RowFilter = "ParentID=0";
TreeView1.ShowCheckBoxes = TreeNodeTypes.All;
foreach (DataRowView drv in dv)
{
TreeNode node = new TreeNode();
node.Text = drv["text"].ToString();
node.Value = drv["ID"].ToString();
node.Expanded = false;
TreeView1.Nodes.Add(node);
AddReplies(dt,node);
}
}
private DataTable GetTreeViewTable()
{
string constring = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
SqlConnection con = new SqlConnection(constring);
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM treeview", con);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
private void AddReplies(DataTable dt, TreeNode node)
{
DataView dv = new DataView(dt);
dv.RowFilter = "ParentID='" + node.Value + "'";
foreach (DataRowView row in dv)
{
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class Page_Wonderful_TreeView_System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//BindTree();
InitTree();
}
}
主從表繫結#region 主從表繫結
private void BindTree()
{
DataSet dst = GetTreeViewData();
TreeView1.ShowCheckBoxes = TreeNodeTypes.All;
foreach (DataRow masterRow in dst.Tables["province"].Rows)
{
TreeNode masterNode = new TreeNode((string)masterRow["province"]);
TreeView1.Nodes.Add(masterNode);
foreach (DataRow childRow in masterRow.GetChildRows("Children"))
{
TreeNode childNode =new TreeNode((string)childRow["city"]);
masterNode.Expanded = false;
masterNode.ChildNodes.Add(childNode);
}
}
}
private DataSet GetTreeViewData()
{
string constring = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
SqlConnection con = new SqlConnection(constring);
SqlDataAdapter daprovince = new SqlDataAdapter("SELECT * FROM province", con);
SqlDataAdapter dacity = new SqlDataAdapter("SELECT * FROM city", con);
DataSet ds = new DataSet();
daprovince.Fill(ds, "province");
dacity.Fill(ds, "city");
ds.Relations.Add("Children", ds.Tables["province"].Columns["provinceid"], ds.Tables["city"].Columns["father"]);
return ds;
}
#endregion
遞迴繫結同一個表資料#region 遞迴繫結同一個表資料
private void InitTree()
{
DataTable dt = GetTreeViewTable();
DataView dv = new DataView(dt);
dv.RowFilter = "ParentID=0";
TreeView1.ShowCheckBoxes = TreeNodeTypes.All;
foreach (DataRowView drv in dv)
{
TreeNode node = new TreeNode();
node.Text = drv["text"].ToString();
node.Value = drv["ID"].ToString();
node.Expanded = false;
TreeView1.Nodes.Add(node);
AddReplies(dt,node);
}
}
private DataTable GetTreeViewTable()
{
string constring = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
SqlConnection con = new SqlConnection(constring);
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM treeview", con);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
private void AddReplies(DataTable dt, TreeNode node)
{
DataView dv = new DataView(dt);
dv.RowFilter = "ParentID='" + node.Value + "'";
foreach (DataRowView row in dv)
{
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-374437/,如需轉載,請註明出處,否則將追究法律責任。
請登入後發表評論
登入
全部評論
相關文章
- 【xh&hhh】ASP.NET2.0下利用javascript實現TreeView中的checkbox全選!ASP.NETJavaScriptView
- KendoUI系列:TreeViewUIView
- WPF TreeView BringIntoViewBehaviorView
- Delphi中的TreeViewView
- YUI TreeView搞明白了UIView
- TreeView樹狀結構View
- 禁止TreeView自動PostbackView
- WPF TreeView SelectedItemChanged called twiceView
- asp.net TreeView 的使用ASP.NETView
- C# List構造TreeViewC#View
- TreeView學習筆記 (轉)View筆記
- WPF TreeView級聯複選View
- 題解:P10380 「ALFR Round 1」D 小山的元力
- TreeView的基本使用 1205View
- tkinter treeview 背景色 問題.View
- delphi中Treeview的使用介紹View
- asp.net TreeView動態新增ASP.NETView
- Object TreeView簡要說明 (轉)ObjectView
- 控制元件treeview的使用 (轉)控制元件View
- ACCESS TreeView控制元件的使用View控制元件
- Asp.Net2.0實現自定義樣式ASP.NET
- PB TreeView 屬性,事件詳解(轉)View事件
- TreeView遞迴繫結地區列表View遞迴
- TreeView.cs source code in C# .NETViewC#
- 【小山】sql server通過查詢系統表得到縱向的表結構SQLServer
- Tkinter (39) 樹檢視部件 ttk.TreeviewView
- 動態menu導航條以及treeview樹View
- jQuery 樹形控制元件 TreeView 的 BugjQuery控制元件View
- ASP.NET2.0的URL對映的實現方法ASP.NET
- WPF TreeView IsExpanded 繫結不上的問題View
- 資料庫驅動的asp.net treeview資料庫ASP.NETView
- Asp.net利用Treeview實現樹形列表ASP.NETView
- ASP.NET2.0中CSS失效的問題總結(轉)ASP.NETCSS
- ASP.NET2.0開發中的認證與授權ASP.NET
- ASP.NET2.0中文驗證碼的實現ASP.NET
- 在 ASP.NET2.0 中使用計時器(Timer)ASP.NET
- ASP.NET2.0連線SQL Server資料庫詳解ASP.NETSQLServer資料庫
- 將Xml檔案遞迴載入到TreeView中XML遞迴View