使用递归的方式将数据绑定到TreeView上(我使用的这个方法效率上很慢,暂时没找到简便的方法,先将就一下)
protected void AddDataToTree(TreeViewNode pareentNode)
{
DataTable catalogGroup=Base_CategoryProvider .Instance().GetDataTable(
DBQuery.SelectFields(Base_Category.TAG_CATEGORY_NAME,Base_Category.TAG_CATEGORY_CODE)
.From(Base_Category.Base_TableName)
.Where(Base_Category.PARENT_TAG_CAT_CODE,Compare.Equals,DBConst.String(pareentNode.Name)));
if (catalogGroup.Rows.Count >= 1)
{
TreeViewNode node;
foreach (DataRow group in catalogGroup.Rows)
{
node = new TreeViewNode();
node.Name = group[Base_Category.TAG_CATEGORY_CODE].ToString();
node.Text = group[Base_Category.TAG_CATEGORY_NAME].ToString();
pareentNode.Nodes.Add(node);
AddDataToTree(node);
}
}
else
return;
}
protected void Page_Load(object sender, EventArgs e)
{
TreeViewNode mainnode = new TreeViewNode();
mainnode.Text = "所有词条";
mainnode.Name = "All";
#region 无父类的节点
DataTable catalogGroup=Base_CategoryProvider .Instance().GetDataTable(
DBQuery.SelectFields(Base_Category.TAG_CATEGORY_NAME,Base_Category.TAG_CATEGORY_CODE)
.From(Base_Category.Base_TableName)
.Where(Base_Category.PARENT_TAG_CAT_CODE,Compare.Is,DBConst.Null()));
TreeViewNode node;
foreach(DataRow group in catalogGroup.Rows)
{
node = new TreeViewNode();
node.Name=group[Base_Category.TAG_CATEGORY_CODE].ToString();
node.Text=group[Base_Category.TAG_CATEGORY_NAME].ToString();
mainnode.Nodes.Add(node);
AddDataToTree(node);
}
this.keyWordTree.Nodes.Add(mainnode);
#endregion
}
TreeView的客户端事件:
<dx:ASPxTreeView ID="keyWordTree" runat="server" AllowSelectNode="true"> <ClientSideEvents Init="function(s, e) { }" NodeClick="function(s, e) { getKeyWord(s, e); }" /> </dx:ASPxTreeView>
e代表节点.通过e.node.name和e.node.GetText()访问节点数据。