动态添加的叶子节点怎么去掉前面的+号?
树刚开始是空的:
//初始化
TreeNode NewNode = new TreeNode("根节点", "00");
NewNode.ImageUrl = "../images/OUTOPEN.BMP";
NewNode.PopulateOnDemand = false;
NewNode.Expanded = true;
NewNode.SelectAction = TreeNodeSelectAction.SelectExpand;
TreeNode NewChildNode1 = new TreeNode("节点1", "file");
NewChildNode1.PopulateOnDemand = true;
NewChildNode1.ImageUrl = "../images/OUTCLOSE.BMP";
NewChildNode1.Expanded = false;
NewChildNode1.SelectAction = TreeNodeSelectAction.SelectExpand;
NewNode.ChildNodes.Add(NewChildNode1);
TreeNode NewChildNode2 = new TreeNode("节点2", "item");
NewChildNode2.PopulateOnDemand = true;
NewChildNode2.ImageUrl = "../images/OUTCLOSE.BMP";
NewChildNode2.Expanded = false;
NewChildNode2.SelectAction = TreeNodeSelectAction.SelectExpand;
NewNode.ChildNodes.Add(NewChildNode2);
this.TreeView1.Nodes.Add(NewNode);
这样树就有了1个根2个子节点,现在我需要在单击某个父节点是才动态添加其子节点,比如在点"节点1"时从数据库中读出几条记录添加为"节点1"的子节点,是这样做的:
//点击某个节点时触发
protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
{
string sql = "XXXXXXXXX"; //这里根据不同情况拼一条SQL语句
AddChildNodes(TreeView1.SelectedNode, strSql); //调用添加子节点函数
}
//添加子节点
void AddChildNodes(TreeNode node, string strSql)
{
//获取记录
if (node.ChildNodes.Count == 0)
{
foreach (DataRow row in dt.Rows)//把获取的记录一条一条添加为子节点
{
TreeNode NewNode = new TreeNode(row["Name"].ToString(), row["Code"].ToString());
if (row["url"].ToString() != "")
{
NewNode.Expanded = true; //查资料说这样就可以了,但是不行,添加的子节点前面还有加号,用NewNode.Expand(); NewNode.ExpandAll();这些都不行
NewNode.ImageUrl = "images/icon1.gif";
NewNode.NavigateUrl = row["url"].ToString();
NewNode.Target = "right";
NewNode.SelectAction = TreeNodeSelectAction.Select;
}
else
{
NewNode.Expanded = false;
NewNode.SelectAction = TreeNodeSelectAction.SelectExpand;
}
NewNode.PopulateOnDemand = true;
node.ChildNodes.Add(NewNode);
}
}
}
添加上去的子节点前面都有加号,我是不想要加号,因为这已经都是叶子节点了,不该有加号的,请帮忙看下该怎么弄?
[解决办法]
NewNode.Expanded = true; //查资料说这样就可以了,但是不行,添加的子节点前面还有加号,用NewNode.Expand(); NewNode.ExpandAll();这些都不行
expanded属性是还可以不可以扩展的意思,应该是false才对吧?
[解决办法]
ShowExpandCollapse 获取或设置一个值,它指示是否显示展开节点指示符。
ShowLines 获取或设置一个值,它指示是否显示连接子节点和父节点的线条。
[解决办法]
if (node.ChildNodes.Count == 0)
{
node.ShowExpandCollapse = false
//其他的代码
}
[解决办法]
默认情况,子节点就是没有
如:
<div>
<asp:TreeView ID="tree" runat="server"></asp:TreeView>
</div>
TreeNode node = new TreeNode();
node.Text = "Node1";
TreeNode node2 = new TreeNode();
node2.Text = "Node2";
node.ChildNodes.Add(node2);
TreeNode node3 = new TreeNode();
node3.Text = "node3";
this.tree.Nodes.Add(node);
this.tree.Nodes.Add(node3);