<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Untitled Page</title>
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<script language="javascript">![](https://www.cnblogs.com/Images/dot.gif)
function chkAll()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//debugger;
var chkall= document.all["chkall"];
var chkother= document.getElementsByTagName("input");
for (var i=0;i<chkother.length;i++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if( chkother[i].type=='checkbox')
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(chkother[i].id.indexOf('TreeView1')>-1)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(chkall.checked==true)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
chkother[i].checked=true;
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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" OnClick="Button1_Click" /></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>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</form>
</body>
</html>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
cs:
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;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
public partial class TreeView2 : System.Web.UI.Page
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
protected void Page_Load(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (!Page.IsPostBack)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//BindTree();
InitTree();
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
主从表绑定#region 主从表绑定
private void BindTree()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
DataSet dst = GetTreeViewData();
TreeView1.ShowCheckBoxes = TreeNodeTypes.All;
foreach (DataRow masterRow in dst.Tables["province"].Rows)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
TreeNode masterNode = new TreeNode((string)masterRow["province"]);
TreeView1.Nodes.Add(masterNode);
foreach (DataRow childRow in masterRow.GetChildRows("Children"))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
TreeNode childNode =new TreeNode((string)childRow["city"]);
masterNode.Expanded = false;
masterNode.ChildNodes.Add(childNode);
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private DataSet GetTreeViewData()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
递归绑定同一个表数据#region 递归绑定同一个表数据
private void InitTree()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
DataTable dt = GetTreeViewTable();
DataView dv = new DataView(dt);
dv.RowFilter = "ParentID=0";
TreeView1.ShowCheckBoxes = TreeNodeTypes.All;
foreach (DataRowView drv in dv)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
TreeNode node = new TreeNode();
node.Text = drv["subject"].ToString();
node.Value = drv["ID"].ToString();
node.NavigateUrl = drv["url"].ToString();
node.Expanded = false;
TreeView1.Nodes.Add(node);
AddReplies(dt,node);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private DataTable GetTreeViewTable()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private void AddReplies(DataTable dt, TreeNode node)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
DataView dv = new DataView(dt);
dv.RowFilter = "ParentID='" + node.Value + "'";
foreach (DataRowView row in dv)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
TreeNode replyNode = new TreeNode();
replyNode.Text = row["subject"].ToString();
replyNode.Value = row["ID"].ToString();
node.NavigateUrl = row["url"].ToString();
replyNode.Expanded = false;
node.ChildNodes.Add(replyNode);
AddReplies(dt,replyNode);
}
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
protected void Button1_Click(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Data :
/Files/ipusr/area1.rar /Files/ipusr/treeview.rar
posted on
2007-03-03 15:40
ipusr
阅读(
253)
评论()
编辑
收藏
举报