asp.net treeview 总结
网上关于Treeview的代码虽然多 但是都是很乱 实用性和正确性也不是很好 只好自己写一套了,时间比较紧张 性能可能还需调整
以用户组织的一个实际例子来讲诉Treeview的用法吧
组织表结构:

用户组织表结构:

前台界面aspx
<form id="Form1" runat="server">
<table> <tr>
<td class="contentTD">
<div id="MyTreeDiv">
<asp:TreeView ShowCheckBoxes="All" ID="MyTreeView" ImageSet="Contacts" runat="server" ExpandDepth="" Width="400px" BorderStyle="Solid" BorderWidth="1px">
</asp:TreeView>
</div>
</td>
</tr>
<tr> <td class="content">
<asp:Button ID="btnSubmit" runat="server" Text="提 交" CssClass="dotNetButton" OnClick="btnSubmit_Click" /><input type="button" class="glbutton" onclick="history.go(-1);"
value="返 回" />
<asp:ListBox ID="ListBox1" Visible="false" runat="server"></asp:ListBox>
</td>
</tr>
</table>
</form>
js控制父节点选中子节点自动选中
<script>
function Davidovitz_HandleCheckbox() {
var element = event.srcElement;
if (element.tagName == "INPUT" && element.type == "checkbox") {
var checkedState = element.checked;
while (element.tagName != "TABLE") // Get wrapping table
{
element = element.parentElement;
} Davidovitz_UnCheckParents(element); // Uncheck all parents element = element.nextSibling; if (element == null) // If no childrens than exit
return; var childTables = element.getElementsByTagName("TABLE");
for (var tableIndex = ; tableIndex < childTables.length; tableIndex++) {
Davidovitz_CheckTable(childTables[tableIndex], checkedState);
}
}
} // Uncheck the parents of the given table, Can remove the recurse (redundant)
function Davidovitz_UnCheckParents(table) {
if (table == null || table.rows[].cells.length == ) // This is the root
{
return;
}
var parentTable = table.parentElement.previousSibling;
Davidovitz_CheckTable(parentTable, false);
Davidovitz_UnCheckParents(parentTable);
} // Handle the set of checkbox checked state
function Davidovitz_CheckTable(table, checked) {
var checkboxIndex = table.rows[].cells.length - ;
var cell = table.rows[].cells[checkboxIndex];
var checkboxes = cell.getElementsByTagName("INPUT");
if (checkboxes.length == ) {
checkboxes[].checked = checked;
}
} </script>
后台代码.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.MyTreeView.Attributes.Add("onclick", "Davidovitz_HandleCheckbox()"); bind();
}
}
//数据绑定
private void bind()
{
this.MyTreeView.Nodes.Clear();
//读取组织列表
DataTable dt = new T_GroupManager().GetDataTableBySQL(" and FState=1 and FCreatorID = " + Request.QueryString["id"]); this.ViewState["ds"] = dt;
//调用递归函数,完成树形结构的生成
AddTree(, (TreeNode)null); if (Request.QueryString["id"] != null)
{
//根据用户ID查找对应的用户组织结构,勾上
List<T_UserGroup> list = new T_UserGroupManager().GetAllBySQL(" and FUSerID=" + Request.QueryString["id"]).ToList();
ViewState["UserGroup"] = list;
foreach (T_UserGroup item in list)
{
for (int i = ; i < this.MyTreeView.Nodes.Count; i++)
{
if (MyTreeView.Nodes[i].ChildNodes.Count > ) //判断是否还有子节点
{
SetNode(MyTreeView.Nodes[i]);
}
if (MyTreeView.Nodes[i].Value == item.FGroupID.ToString()) //判断是否被选中
{
MyTreeView.Nodes[i].Checked = true;
}
}
}
}
}
//查找子节点
public void SetNode(TreeNode node)
{
if (Request.QueryString["id"] != null)
{
//根据ID查找对应的组织结构,勾上
List<T_UserGroup> list = ViewState["UserGroup"] as List<T_UserGroup>;
foreach (T_UserGroup item in list)
{
for (int i = ; i < node.ChildNodes.Count; i++)
{
if (node.ChildNodes[i].ChildNodes.Count > ) //判断是否还有子节点
{
SetNode(node.ChildNodes[i]); //递归查找
}
if (node.ChildNodes[i].Value == item.FGroupID.ToString()) //判断是否被选中
{
node.ChildNodes[i].Checked = true;
}
}
}
}
} //递归添加树的节点
public void AddTree(int ParentID, TreeNode pNode)
{
DataTable ds = (DataTable)this.ViewState["ds"];
DataView dvTree = new DataView(ds);
//过滤ParentID,得到当前的所有子节点
dvTree.RowFilter = "[FParentUserID] = " + ParentID; foreach (DataRowView Row in dvTree)
{
TreeNode Node = new TreeNode();
if (pNode == null)
{
//添加根节点
Node.Text = Row["FGroupName"].ToString();
Node.Value = Row["PID"].ToString();
this.MyTreeView.Nodes.Add(Node);
Node.Expanded = true;
//再次递归
AddTree(Int32.Parse(Row["PID"].ToString()), Node);
}
else
{
//添加当前节点的子节点
Node.Text = Row["FGroupName"].ToString();
Node.Value = Row["PID"].ToString();
pNode.ChildNodes.Add(Node);
Node.Expanded = true;
//再次递归
AddTree(Int32.Parse(Row["PID"].ToString()), Node);
}
}
}
/// <summary>
/// 保存
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnSubmit_Click(object sender, EventArgs e)
{
for (int i = ; i < MyTreeView.Nodes.Count; i++)
{
if (MyTreeView.Nodes[i].ChildNodes.Count > ) //判断是否还有子节点
{
GetNode(MyTreeView.Nodes[i]);
}
if (MyTreeView.Nodes[i].Checked == true) //判断是否被选中
{
string s = MyTreeView.Nodes[i].Value.ToString();
ListBox1.Items.Add(s);
}
}
T_UserGroupManager gm = new T_UserGroupManager();
//清空
List<T_UserGroup> list = ViewState["UserGroup"] as List<T_UserGroup>;
foreach (T_UserGroup item in list)
{
gm.DeleteByPID(item.PID);
}
int id = Convert.ToInt32(Request.QueryString["id"]);
//保存
foreach (var item in ListBox1.Items)
{
T_UserGroup ug = new T_UserGroup();
ug.FUSerID = id;
ug.FGroupID = Convert.ToInt32((item as ListItem).Value);
ug.FState = ;
gm.Add(ug);
}
ShowJS("<script>alert('保存成功!');location = 'UserGroupInfo.aspx'</script>");
}
//获取选中节点
public void GetNode(TreeNode node)
{
for (int i = ; i < node.ChildNodes.Count; i++)
{
if (node.ChildNodes[i].ChildNodes.Count > ) //判断是否还有子节点
{
GetNode(node.ChildNodes[i]); //递归查找
}
if (node.ChildNodes[i].Checked == true) //判断是否被选中
{
string s = node.ChildNodes[i].Value.ToString();
ListBox1.Items.Add(s);
}
}
}
}
基本上面就是全部代码了,实现了Treeview的读取和根据勾选保存,根据用户配置设置Treeview的勾选项
用了好一段时间才整理出来的,要转载的童鞋记得保留我的链接哦http://www.cnblogs.com/linyijia/p/3497503.html
asp.net treeview 总结的更多相关文章
- asp .Net TreeView实现数据绑定和事件响应
最近做了一个图书馆管理系统,其中要实现中图法分类号查询,因为初学asp ,感觉还有点难度, 第一步:数据库文件 第二步 向界面中拖进TreeView控件 第三步添加事件 下面是cs文件代码 //Tre ...
- asp 使用TreeView控件
这段代码为了使用 TreeNodeCheckChanged 事件,会有回刷新的效果: 不喜欢的可查看改进版,利用js控制选择操作,无界面刷新, “http://www.cnblogs.com/GoCi ...
- asp.net treeview 异步加载
在使用TreeView控件的时候,如果数据量太大,这个TreeView控件加载会很慢,有时甚至加载失败, 为了更好的使用TreeView控件加载大量的数据,采用异步延迟加载TreeView. 在Tre ...
- ASP.NET - TreeView 增删
效果: 前端代码: <%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Si ...
- ASP.NET - TreeView
设置节点图片 : Windows资源管理器左侧的树型资源结构图中,各节点都有图片连接,例如磁盘的图片.光盘的图片和文件夹的图片等,使资源的表现更加形象.IEWebControls的TreeView控件 ...
- 转:asp.net TreeView CheckChanged 事件浅谈
http://blog.csdn.net/xiage/article/details/5128755 在开发中经常可以碰到类似的问题: 想通过一个树父节点的TreeNodeCheckChanged 事 ...
- asp.net TreeView控件绑定数据库显示信息
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- asp.net TreeView与XML配合使用v1.1
刚我在做Tree view 绑定时自己摸索了一下,网上有人说TreeView绑定数据源,用什么递归绑定啥的,我不想看了,就自己试着写了一个 我是这样做的,如果有什么问题请大神指导,我是菜鸟额.. 1: ...
- ASP.NET - TreeView控件,只操作最后一级节点
效果: 使用母板页进行,左右页面进行跳转. 绑定TreeView控件:http://www.cnblogs.com/KTblog/p/4792302.html 主要功能: 点击节点的时候,只操作最后一 ...
随机推荐
- strcpy与strncpy
char aa[]="123456789123456789123456789"; char bb[4]={0}; 1.strcpy(bb,aa); bb的空间,不能存下aa的内容, ...
- FileUpload类中FileUpload1.FileName和FileUpload1.PostedFile.FileName的区别
FileUpload1.FileName 用来获取客户端上使用 FileUpload 控件上载的文件的名称.此属性返回的文件名不包含此文件在客户端上的路径.FileUpload1.PostedFile ...
- js 实现sleep函数
1.sleep函数 sleep函数作用是让线程休眠,等到指定时间在重新唤起. 2.sleep实现 <!DOCTYPE html> <html lang="zh"& ...
- android中的byte数组转换(转)
/** * 将一个单字节的byte转换成32位的int * * @param b * byte * @return convert result */ public static int unsign ...
- excel 如何为列添加指定内容(字符串)
excel 如何为列添加指定内容(字符串) CreateTime--2018年5月26日17:52:32 Author:Marydon 1.情景展示 D列的值需要获取B列的值并且在后面统一加上12 ...
- Inno Setup Pascal Script to search for running process
I am currently trying to do a validation at the uninstall moment. In a Pascal script function, in In ...
- linux之进程管理详解
|-进程管理 进程常用命令 |- w查看当前系统信息 |- ps进程查看命令 |- kill终止进程 |- 一个存放内存中的特殊目 ...
- C#:XML操作(简单)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.X ...
- C#位操作
一.原码与补码 在计算机系统中,数值一律用补码来存储(表示).主要原因:使用补码,可以将符号位和其他位统一处理:同时减法也可按加法来处理.另外,两个补码表示的数相加时,如果最高位(符号位)有进位,则进 ...
- C#-拷贝目录内容(文件和子目录)
/// <summary> /// 拷贝目录内容 /// </summary> /// <param name="source">源目录< ...