我尝试使用下面的
创建一个asp,net treeview结构 <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TreeView
ID="TreeView1"
runat="server" />
</div>
</form>
在Code背后
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
PopulateTreeView();
}
private void PopulateTreeView()
{
DataTable treeViewData = GetTreeViewData();
AddTopTreeViewNodes(treeViewData);
}
private DataTable GetTreeViewData()
{
string selectCommand = "SELECT top 10 idx_client,idx_branch,client_name FROM tbl_client";
string conString =System.Configuration.ConfigurationManager.AppSettings["ConnectionString"].ToString();
SqlDataAdapter dad = new SqlDataAdapter(selectCommand, conString);
DataTable dtblDiscuss = new DataTable();
dad.Fill(dtblDiscuss);
return dtblDiscuss;
}
private void AddTopTreeViewNodes(DataTable treeViewData)
{
DataView view = new DataView(treeViewData);
foreach (DataRowView row in view)
{
TreeNode newNode = new TreeNode(row["client_name"].ToString(), row["idx_client"].ToString());
TreeView1.Nodes.Add(newNode);
newNode.PopulateOnDemand = true;
AddChildTreeViewNodes(treeViewData, newNode);
}
}
private void AddChildTreeViewNodes(DataTable treeViewData, TreeNode parentTreeViewNode)
{
DataView view = new DataView(treeViewData);
foreach (DataRowView row in view)
{
TreeNode newNode = new TreeNode(row["client_name"].ToString(), row["idx_client"].ToString());
parentTreeViewNode.ChildNodes.Add(newNode);
newNode.PopulateOnDemand = true;
AddChildTreeViewNodes(treeViewData, newNode);
}
}
但它给出了例外
无法计算表达式,因为当前线程处于堆栈溢出状态
请指出所需的更改,因为我是第一次使用此树视图 我将如何解决这个例外。任何人都可以帮助我。
答案 0 :(得分:0)
问题在于AddChildTreeViewNodes
中的递归调用:
private void AddChildTreeViewNodes(DataTable treeViewData, TreeNode parentTreeViewNode)
{
DataView view = new DataView(treeViewData);
foreach (DataRowView row in view)
{
TreeNode newNode = new TreeNode(row["client_name"].ToString(), row["idx_client"].ToString());
parentTreeViewNode.ChildNodes.Add(newNode);
newNode.PopulateOnDemand = true;
// error caused by recursion below
AddChildTreeViewNodes(treeViewData, newNode); // calls itself again REMOVE THIS
}
}
这里发生的是您正在插入视图的子节点,然后尝试在其中插入另一个子节点(这反过来将执行相同的操作)。这将继续发生,直到堆栈爆炸。
我怀疑你真的不需要我标记为REMOVE THIS
的行