TreeView高效绑定数据
加快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>
<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> <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); //向结点填充数据.
}
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> <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
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