下面的树形结构代码需要GridVIew中的数据要求是按照上下级关系已经排列好的顺序,比如:

GridView

ID ParentID Name 1 0 父1 2 1 父1子1 3 1 父1子2 4 3 父1子2孙1 5 3 父1子2孙2 6 0 父2 7 6 父2子

父子项关系用递归函数实现 

protected void gvList_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType==DataControlRowType.DataRow)
{
#region 数据绑定
DataRowView rowView = (DataRowView) e.Row.DataItem;

Label lblParentName=e.Row.FindControl("lblParentName") as Label;
lblParentName.Text = GetParentName(Convert.ToInt32(rowView["ParentID"]));

Button btnDel = e.Row.FindControl("btnDel") as Button;
btnDel.Visible = (Convert.ToInt32(rowView["ParentID"]) != 0);
#endregion

#region 树形结构

string id = rowView["ID"].ToString();
string pid = rowView["ParentID"].ToString();
string level = "1";
e.Row.Attributes.Add("id",id);
e.Row.Attributes.Add("pid",pid);
e.Row.Attributes.Add("expand","1");

if (rowView["ParentID"].ToString() == "0")
{
var img = new HtmlImage {Src = "../../Image/Tree/minimize.gif"};
img.Attributes.Add("onclick","setExpand(this)");
img.Style.Add("cursor","pointer");
e.Row.Cells[1].Controls.Add(img);
}
else
{
e.Row.Cells[1].Style["padding-left"] = "20px";
level = "2";
}
e.Row.Attributes.Add("level",level);
#endregion
}
}

以下是前台js控制节点电机事件中的显示和隐藏效果(判断代码“nextNode.nodeType==1”部分是因为不同浏览器可能会产生多余的空白换行而使nextNode不为空,但是nodeType为其他值)

<script type="text/javascript">
//树节点相关控制
        function hideChildren(node) {
            var level = parseInt(node.getAttribute("level"));
            var nextNode = node.nextSibling;
            while (nextNode != null && nextNode.nodeType==1&& parseInt(nextNode.getAttribute("level")) > level) {
                nextNode.style.display = "none"; nextNode = nextNode.nextSibling;
            }
        }
        function showChildren(node) {
            var level = parseInt(node.getAttribute("level"));
            var id = node.getAttribute("id");
            var nextNode = node.nextSibling;
            while (nextNode != null && nextNode.nodeType==1&& parseInt(nextNode.getAttribute("level")) > level) {
                if (nextNode.getAttribute("pid") == id) {
                    if (node.getAttribute("expand") == "1") {
                        nextNode.style.display = "";
                        showChildren(nextNode);
                    }
                    else {
                        nextNode.style.display = "none";
                    }
                }
                nextNode = nextNode.nextSibling;
            }
        }

function setExpand(sender) {
var curNode = sender.parentNode.parentNode;
if (curNode.getAttribute("expand") == "1") {
curNode.setAttribute("expand", "0");
sender.src = "../../Image/Tree/maximize.gif";
hideChildren(curNode);
}
else {
curNode.setAttribute("expand", "1");
sender.src = "../../Image/Tree/minimize.gif";
showChildren(curNode);
}
}
</script>

<asp:gridview id="gvList" onrowcommand="gvList_RowCommand" onrowdatabound="gvList_RowDataBound" emptydatatext="暂无记录!" datakeynames="ID" cssclass="grid" autogeneratecolumns="false" runat="server">
<columns>
<asp:boundfield itemstyle-horizontalalign="Center" visible="false" datafield="ID" itemstyle-width="20" headertext="ID">
<asp:boundfield itemstyle-horizontalalign="left" datafield="DeptNo" itemstyle-width="200px" headertext="编号">
<asp:boundfield itemstyle-horizontalalign="Center" datafield="DeptName" itemstyle-width="200px" headertext="名称">
<asp:templatefield headertext="上级" headerstyle-width="200px">
<itemstyle wrap="false" horizontalalign="Center" width="100px">
<itemtemplate>
<asp:label id="lblParentName" runat="server"></asp:label>
</itemtemplate>
</itemstyle></asp:templatefield>

<asp:templatefield headertext="操作" headerstyle-width="200px">
<itemstyle horizontalalign="center" width="200px">
<itemtemplate>
<table><tbody><tr><td style="width: 50px;"> <input class="btnGrid" title="修改信息" value="编 辑" type="button" />
  </td><td style="width: 50px;"><asp:button id="btnDel" cssclass="btnGrid" runat="server" commandargument="<%#Eval('ID') %>" tooltip="删除信息" commandname="Delet" onclientclick="return confirm('确定删除选中记录吗?')" text="删 除">
</asp:button></td></tr></tbody></table>
</itemtemplate>
</itemstyle></asp:templatefield>
</asp:boundfield></asp:boundfield></asp:boundfield></columns>
</asp:gridview>

GridView树状结构显示的更多相关文章

  1. C#使用Jquery zTree实现树状结构显示_异步数据加载

    JQuery-Ztree下载地址:https://github.com/zTree/zTree_v3 JQuery-Ztree数结构演示页面:  http://www.treejs.cn/v3/dem ...

  2. 由简入繁实现Jquery树状结构

    在项目中,我们经常会需要一些树状结构的样式来显示层级结构等,比如下图的样式,之前在学.net的时候可以直接拖个服务端控件过来直接使用非常方便.但是利用Jquery的一些插件,也是可以实现这些效果的,比 ...

  3. 浅谈oracle树状结构层级查询之start with ....connect by prior、level及order by

    浅谈oracle树状结构层级查询 oracle树状结构查询即层次递归查询,是sql语句经常用到的,在实际开发中组织结构实现及其层次化实现功能也是经常遇到的,虽然我是一个java程序开发者,我一直觉得只 ...

  4. JQuery 树状结构 jQuery-treeview.js 插件

    由简入繁实现Jquery树状结构 在项目中,我们经常会需要一些树状结构的样式来显示层级结构等,比如下图的样式,之前在学.net的时候可以直接拖个服务端控件过来直接使用非常方便.但是利用Jquery的一 ...

  5. oracle 树状结构递归 PL/SQL输出控制 包括空格输出控制

    树状结构 存储过程中通过递归构建,类似BBS回帖显示,代码共三段: 建表,插入数据,创建存储过程显示: 1.create table article(id number primary key,con ...

  6. openerp学习笔记 对象间关系【多对一(一对一)、一对多(主细结构)、多对多关系、自关联关系(树状结构)】

    1.多对一(一对一)关系:采购单与供应商之间的关系 'partner_id':fields.many2one('res.partner', 'Supplier', required=True, sta ...

  7. 树状结构Java模型、层级关系Java模型、上下级关系Java模型与html页面展示

    树状结构Java模型.层级关系Java模型.上下级关系Java模型与html页面展示 一.业务原型:公司的组织结构.传销关系网 二.数据库模型 很简单,创建 id 与 pid 关系即可.(pid:pa ...

  8. 分享使用NPOI导出Excel树状结构的数据,如部门用户菜单权限

    大家都知道使用NPOI导出Excel格式数据 很简单,网上一搜,到处都有示例代码. 因为工作的关系,经常会有处理各种数据库数据的场景,其中处理Excel 数据导出,以备客户人员确认数据,场景很常见. ...

  9. php实现树状结构无级分类

    php实现树状结构无级分类   ).",'树2-1-1-2')";mysql_query($sql);?>

随机推荐

  1. 浅谈C++编译原理 ------ C++编译器与链接器工作原理

    原文:https://blog.csdn.net/zyh821351004/article/details/46425823 第一篇:      首先是预编译,这一步可以粗略的认为只做了一件事情,那就 ...

  2. S1_搭建分布式OpenStack集群_07 nova服务配置 (计算节点)

    一.服务安装(计算节点)安装软件:# yum install openstack-nova-compute -y 编辑/etc/nova/nova.conf文件并设置如下内容:# vim /etc/n ...

  3. 微信小程序弹窗

    wxml <view class="content"> <button bindtap="popSuccessTest">成功提示弹窗& ...

  4. Selenium ChromeDriver与Chrome版本映射表(更新到v77)

    ChromeDriver版本 支持的Chrome版本 v77.0.3865.40 v77 v76.0.3809.126 v76 v75.0.3770.140 v75 v74 v74 v73 v73 v ...

  5. Using HAProxy as an API Gateway, Part 2 [Authentication]

    转自:https://www.haproxy.com/blog/using-haproxy-as-an-api-gateway-part-2-authentication/ HAProxy is a ...

  6. java 数据库迁移工具 flyway

    官方 https://github.com/flyway/flyway 简易demo https://github.com/deadzq/flyway-demo 主要在配置文件上做改动

  7. [codewars] - int32 to IPv4 二进制十进制 ip地址转换

    原题 https://www.codewars.com/kata/int32-to-ipv4/train/java Take the following IPv4 address: 128.32.10 ...

  8. PHP-FPM config 文件生产环境

    ;;;;;;;;;;;;;;;;;; ; Global Options ; ;;;;;;;;;;;;;;;;;; [global] pid = run/php-fpm.pid error_log = ...

  9. Android入门教程(四)

    关注我,每天都有优质技术文章推送,工作,学习累了的时候放松一下自己. 本篇文章同步微信公众号 欢迎大家关注我的微信公众号:「醉翁猫咪」 学习Android要掌握Android程序结构,和通信技术,和如 ...

  10. Intellij IDEA 自动清除无效 import 和 清除无效 import 的快捷键

    可以settings-general-auto import-java项,勾选optimize imports on the fly,在当前项目下会自动清除无效的import,而且这个是随时自动清除的 ...