<?xml version="1.0" encoding="UTF-8" ?>
<properties>
<general>
<title type="textbox">title1</title>
<subtitle type="textbox">subtitle1</subtitle>
<radius type="textbox">20</radius>
</general>
<behavior>
<sorting>
<enable type="checkbox">True</enable>
<by type="dropdown">Data</by>
<order type="dropdown">Descending</order>
</sorting>
</behavior>
<appearance>
<series>
<innerseries type="colorpicker">#996666</innerseries>
<innerseries type="colorpicker"></innerseries>
<innerseries type="colorpicker"></innerseries>
<transparency type="slider">19</transparency>
</series>
</appearance>
</properties>
以上是我的 XML。
在我的 .ASPX 页面中:
<asp:TreeView ID="TreeView1" runat="server"
OnSelectedNodeChanged="TreeView1_SelectedNodeChanged1" ImageSet="Arrows"
EnableTheming="true">
</asp:TreeView>
<asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" />
在我的代码隐藏中,我动态加载一个 xml 并构建一个树视图
protected void Button1_Click(object sender, EventArgs e)
{
doc.LoadXml("XmlFile");
XmlNode node = doc.DocumentElement;
TreeView1.Nodes.Clear();
TreeView1.Nodes.Add(new TreeNode(doc.DocumentElement.Name));
TreeNode tNode = new TreeNode();
tNode = TreeView1.Nodes[0];
Property obj = new Property();
obj.AddChildNode(node, tNode);
}
In my Property.cs (Class File)
public void AddChildNode(XmlNode inXmlNode, TreeNode inTreeNode)
{
XmlNode xNode;
TreeNode tNode;
XmlNodeList nodeList;
int i;
// Loop through the XML nodes until the leaf is reached.
// Add the nodes to the TreeView during the looping process.
//if (inXmlNode.HasChildNodes)
if (inXmlNode.ChildNodes.Count >= 1 && inXmlNode.ChildNodes[0].HasChildNodes)
{
nodeList = inXmlNode.ChildNodes;
for (i = 0; i <= nodeList.Count - 1; i++)
{
xNode = inXmlNode.ChildNodes[i];
inTreeNode.ChildNodes.Add(new TreeNode(xNode.Name));
tNode = inTreeNode.ChildNodes[i];
AddChildNode(xNode, tNode);
}
}
else
{
if (inXmlNode.ChildNodes.Count > 1)
{
nodeList = inXmlNode.ChildNodes;
for (i = 0; i <= nodeList.Count - 1; i++)
{
xNode = inXmlNode.ChildNodes[i];
inTreeNode.ChildNodes.Add(new TreeNode(xNode.Name));
tNode = inTreeNode.ChildNodes[i];
AddChildNode(xNode, tNode);
}
}
// Here you need to pull the data from the XmlNode based on the
// type of node, whether attribute values are required, and so forth.
else
{
if (inXmlNode.ChildNodes.Count == 0)
{
inTreeNode.Text = (inXmlNode.Name).Trim();
inTreeNode.Target = inXmlNode.Attributes[0].Value;
}
else
{
inTreeNode.Text = (inXmlNode.Name).Trim();
inTreeNode.Target = inXmlNode.Attributes[0].Value;
inTreeNode.Value = inXmlNode.ChildNodes[0].Value;
}
}
//inTreeNode.Value = (inXmlNode.OuterXml).Trim();
}
}
这里的问题是,当我运行我的代码并选择 Title 或 Subtile 节点时,焦点设置到该节点并触发 SelectIndexChanged 事件。但是,当我选择第一个孩子(Innerseries)时,它会完美运行。当第二个 Innerseries 孩子被选中,选择集中在那个节点上。但是,当我选择第三个孩子(Innereseries)时,焦点在第二个孩子上。所以,看起来当节点名称和值相同时,它选择了第一个节点.