TreeView存储过程动态绑定树控件

这是从同事那里得来的代码,当然他也是从网上看来的。呵呵!整理了一下,这个方法绑定速度远远高于上一种(递归绑定)绑定树控件的方法。

下面是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;

public   partial   class  GetDataProcedureCreateTree : System.Web.UI.Page
{
    DbHelperSQL Obj 
= new DbHelperSQL();
    
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
if (!IsPostBack)
        
{
            DataTable dt 
= new DataTable();
            dt 
= getChindNode("0");      //得到所有父节点,放到DATATABLE中,这里默认根节点的父节点为 0
            BindNode(dt, TreeView1.Nodes);         //绑定所有的父节点

        }

    }


    
/// <summary>
    
/// 调用存储过程,得到父节点的子节点,放到DataTable中
    
/// </summary>
    
/// <param name="ParentID"></param>
    
/// <returns></returns>

    private DataTable getChindNode(string ParentID)
    
{
        DataTable dt 
= new DataTable();
        dt 
= Obj.ExecuteSql1("TreeViewGetData",ParentID);
        
return dt; 
    }


    
/// <summary>
    
/// 填充节点事件
    
/// </summary>
    
/// <param name="sender"></param>
    
/// <param name="e"></param>

    protected void TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
    
{
        getDataNode(e.Node.Value, e.Node);     
//点加号展开时调用.得到数据并绑定.传入点击结点ID,和点击节点对像.
    }



    
private void getDataNode(string ParentID,TreeNode Node)
    
{
        BindNode(getChindNode(ParentID), Node.ChildNodes);       
//向结点填充数据.
    }


    
/// <summary>
    
/// 填充节点
    
/// </summary>
    
/// <param name="dt"></param>
    
/// <param name="Node"></param>

    private void BindNode(DataTable dt ,TreeNodeCollection Node)
    
{
        DataView dv 
= new DataView(dt);
        TreeNode NewNode;
        
foreach (DataRowView dr in dv)
        
{
            NewNode 
= new TreeNode();
            NewNode.Text 
= dr["NodeName"].ToString();
            NewNode.Value 
= dr["ID"].ToString();
            Node.Add(NewNode);
            
//是否动态添加结点。
            NewNode.PopulateOnDemand = Convert.ToInt32(dr["ChildNodeCount"].ToString()) > 0;
        }

    }



}

 下面是aspx界面中的源:

<% @ Page Language = " C# "  AutoEventWireup = " true "  CodeFile = " GetDataProcedureCreateTree.aspx.cs "  Inherits = " GetDataProcedureCreateTree "   %>

<! DOCTYPE html PUBLIC  " -//W3C//DTD XHTML 1.0 Transitional//EN "   " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >
< html xmlns = " http://www.w3.org/1999/xhtml "   >
< head runat = " server " >
    
< title > 无标题页 </ title >
</ head >
< body >
    
< form id = " form1 "  runat = " server " >
    
< div >
      
< asp:TreeView ID = " TreeView1 "  runat = " server "  ExpandDepth = " 0 "
            OnTreeNodePopulate
= " TreeView1_TreeNodePopulate "  Style = " position: relative "  ImageSet = " Simple "  CollapseImageUrl = " ~/image/decrease.bmp "  ExpandImageUrl = " ~/image/add.bmp " >
          
< ParentNodeStyle Font - Bold = " False "   />
          
< HoverNodeStyle Font - Underline = " True "  ForeColor = " #5555DD "   />
          
< SelectedNodeStyle Font - Underline = " True "  ForeColor = " #5555DD "  HorizontalPadding = " 0px "
              VerticalPadding
= " 0px "   />
          
< NodeStyle Font - Names = " Tahoma "  Font - Size = " 10pt "  ForeColor = " Black "  HorizontalPadding = " 5px "
              NodeSpacing
= " 0px "  VerticalPadding = " 2px "   />
        
</ asp:TreeView >
    
</ div >
    
</ form >
</ body >
</ html >

下面是DbHelperSQL.cs中的代码:

using  System;
using  System.Data;
using  System.Configuration;
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;


/// <summary>
/// DbHelperSQL 对数据库的各种操作。
/// </summary>

public   class  DbHelperSQL              // 连接SQLSERVER数据库
{
    SqlConnection Conn;                    
//定义连接
    public DbHelperSQL()
    
{
    }

    
/// <summary>
    
/// 连字数据库
    
/// </summary>

    public void ConnectDataBase()
    
{
        
string Connectionstring = ConfigurationSettings.AppSettings["ConnectionString"];
        Conn 
= new SqlConnection(Connectionstring);
        Conn.Open();
    }


    
/// <summary>
    
/// 执行存储过程返回DataTable
    
/// </summary>
    
/// <param name="sql"></param>
    
/// <param name="ParentID"></param>
    
/// <returns></returns>

    public DataTable ExecuteSql1(string sql, string ParentID)
    
{
        DataTable dt;
        
try
        
{
            ConnectDataBase();
            dt 
= new DataTable();
            SqlDataAdapter da 
= new SqlDataAdapter(sql, Conn);

            SqlParameter parm 
= new SqlParameter("@ParentID", ParentID);
            da.SelectCommand.Parameters.Add(parm);
            da.SelectCommand.CommandType 
= CommandType.StoredProcedure;            
            da.Fill(dt);
            Conn.Close();
            
return dt;
        }

        
catch
        
{
            Conn.Close();
            
return dt = null;
        }

    }

}

 

存储过程:

CREATE PROCEDURE TreeViewGetData(
@ParentID nvarchar(
40 ))                     -- 参数,父节点ID
AS
   IF @ParentID IS NULl               
-- 如果,父节点ID为空
           
   SELECT [ID],[NodeName] ,(
      SELECT COUNT(
* ) FROM RA_SubjectStore WHERE ParentID = Org.[ID]) AS ChildNodeCount
    FROM RA_SubjectStore AS Org WHERE @ParentID  IS NULL
   ELSE
    
-- 查询出该父节点下第一级子节点
    SELECT [ID],[NodeName] ,(
      SELECT COUNT(
* ) FROM RA_SubjectStore WHERE ParentID = Org.[ID]) AS ChildNodeCount
    FROM RA_SubjectStore AS Org WHERE ParentID 
= @ParentID

GO

代码已经没有漏掉的了。希望能给大家一点帮助!

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值