TreeView高效绑定数据

前俩个项目都是做政府的门户网,他们的栏目、部门都要以树形展示,一开始数据比较少,直接递归绑定到TreeView上,后来数据越来越多,树越来越慢,到后面打开一次导入数据至少要等待2分钟。客户不满意了,只能对树形绑定数据进行修改,去网上查了下,发现下面的方法速度比较快
第一次只导入所有根栏目,就是数据库ParentId=0的数据。点击父栏目,如果有子栏目,再从数据库查询其子栏目、绑定。
代码如下:


树形控件
<table width="97%" border="0" cellpadding="3" cellspacing="1" bgcolor="a2c4de">
                            <tr>
                                <td height="30" colspan="4" align="center" background="images/bg7.jpg" bgcolor="#FFFFFF"
                                    class="font12B">
                                    栏目管理</td>
                            </tr>
                           
                            <tr>
                                <td valign="top" colspan="4" align="left" bgcolor="#FFFFFF" >
                                    <asp:TreeView ID="treeCatalog" runat="server" Width="100%" Height="100%"
                                      OnTreeNodePopulate="TreeCatalog_TreeNodePopulate" ExpandDepth="1" ImageSet="Arrows" CollapseImageToolTip="折叠 " ExpandImageToolTip="展开 ">
                                    </asp:TreeView>
                                </td>
                            </tr>
                        </table>
cs代码
绑定数据
private void LoadData()
    {
        try
        {
            DataTable table = new DataTable();
            table = getChildNode(0);            //得到所有所有父节点,放到DataTable中,这里默认根节点的父节为0
            BindNode(table, treeCatalog.Nodes);   //绑定所有的父节点
        }
        catch (Exception ex)
        {
            Response.Write("<script>window.top.navigate('Login.aspx');</script>");
        }
    }

    得到父节点的字节点,放到DataTable中#region 得到父节点的字节点,放到DataTable中
    private DataTable getChildNode(int ParentId)
    {
        DataTable table = new DataTable();
        DataService.Service service = new Service();
        DataSet ds = service.CatalogGetByParentId(ParentId).SearchResultDataSet;
        if (ds!=null&&ds.Tables[0].Rows.Count>0)
        {
            table = ds.Tables[0];
        }
        else
        {
            table = null;
        }
        return table;
    }
    #endregion

    填充节点#region 填充节点
    private void BindNode(DataTable table,TreeNodeCollection node)
    {
        DataView dv = new DataView(table);
        TreeNode NewNode;
        foreach(DataRowView dr in dv)
        {
            NewNode = new TreeNode();
            string text = "<a href='CatalogEdit.aspx?Id=" + dr["Id"].ToString() + "'>" + dr["Name"].ToString();
            text +="</a>&nbsp;&nbsp;&nbsp;<a href='CatalogAdd.aspx?ParentId=" + dr["Id"].ToString() + "'>";
            text+="<img alt='增加子栏目' src='images/jia.jpg' border='0'></a>";
            NewNode.Text = text;
            NewNode.Value = dr["Id"].ToString();
            node.Add(NewNode);
            NewNode.PopulateOnDemand = Convert.ToInt32(dr["ChildNodeCount"].ToString())>0;
        }
    }
    #endregion

    填充节点事件#region 填充节点事件
    protected void TreeCatalog_TreeNodePopulate(object sender,TreeNodeEventArgs e)
    {
        getDataNode(Convert.ToInt32(e.Node.Value), e.Node);     //点加号展开时调用.得到数据并绑定.传入点击结点 ID,和点击节点对像.
    }
    #endregion

    private void getDataNode(int ParentId,TreeNode Node)
    {
        BindNode(getChildNode(ParentId), Node.ChildNodes);      //向结点填充数据.
    }

存储过程
存储过程
CREATE PROCEDURE usp_CatalogGetByParentId
(
@ParentId        int
)
AS
BEGIN
    if @ParentId is null
    SELECT Id,Name,ParentId,CategoryId,IsParent,(select count(*) from Catalog where ParentId=C.Id and Status<>99 ) as ChildNodeCount
    FROM Catalog C where status <>99 and ParentId is null order by [order]
    else
    SELECT Id,Name,ParentId,CategoryId,IsParent,(select count(*) from Catalog where ParentId=C.Id and Status<>99 ) as ChildNodeCount
    FROM Catalog C where status <>99 and ParentId=@parentId order by [order]
END

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值