0

我有一个树视图:

 <asp:TreeView ID="testtree1" runat="server" OnSelectedNodeChanged="testtree1_SelectedNodeChanged">
     <Nodes>
        <asp:TreeNode Expanded="False" Text="Tests" Value="Tests"></asp:TreeNode>
     </Nodes>
 </asp:TreeView>

我在我的 .cs 文件中有以下代码:

protected void testtree1_SelectedNodeChanged(object sender, EventArgs e)
    {
        if (testtree1.SelectedNode.Text == "Tests")
        {
            con.Open();
            SqlCommand cmdd = new SqlCommand("select count(Name) from TypeOfWork");
            cmdd.Connection = con;
            int idcount = Convert.ToInt32(cmdd.ExecuteScalar());
            SqlCommand cmd = new SqlCommand("select Name from TypeOfWork");
            cmd.Connection = con;
            string[] nid = new string[idcount];
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);

            if (ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < idcount; i++)
                {
                    nid[i] = ds.Tables[0].Rows[i]["Name"].ToString().Trim();
                    testtree1.Nodes.Add(new TreeNode(nid[i]));
                }
            }
        }
        else
        {
            string tstr = testtree1.SelectedNode.Text;
            con.Open();
            SqlCommand cmd = new SqlCommand("select TypeOfWorkID from TypeOfWork where Name='" + tstr + "'");
            cmd.Connection = con;
            int tid = Convert.ToInt32(cmd.ExecuteScalar());

            SqlCommand cmdd = new SqlCommand("select count(Name) from CategoryLevel1 where TypeOfWorkID='" + tid + "'");
            cmdd.Connection = con;
            int idcount = Convert.ToInt32(cmdd.ExecuteScalar());
            SqlCommand cmd1 = new SqlCommand("select Name from CategoryLevel1 where TypeOfWorkID='" + tid + "'");
            cmd1.Connection = con;
            string[] nid = new string[idcount];
            SqlDataAdapter da = new SqlDataAdapter(cmd1);
            DataSet ds = new DataSet();
            da.Fill(ds);
            if (ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < idcount; i++)
                {

                    nid[i] = ds.Tables[0].Rows[i]["Name"].ToString().Trim();
                    TreeNode child1 = new TreeNode();
                    child1.Text = nid[i];
                    testtree1.Nodes.Add(child1);
                }
            }
        }



    }

这里的问题是我得到一棵树来获取正确的数据,但没有得到应有的树视图。

根是Tests当我点击它时我得到的CivilRVI但它们不是树视图,它们在tests

当我单击CivilRVI它的子节点正在整个事物下而不是普通树时......该怎么办?

任何帮助

4

1 回答 1

1

您需要添加到当前选定节点的 childNodes 集合,而不是添加到根节点:

protected void testtree1_SelectedNodeChanged(object sender, EventArgs e)
{
    var selectedNode = (sender as System.Web.UI.WebControls.TreeView).SelectedNode;
    selectedNode.ChildNodes.Add(new TreeNode('Your TreeNode here'));
}

在您的代码中,而不是:

testtree1.Nodes.Add(new TreeNode(nid[i]));

采用:

(sender as System.Web.UI.WebControls.TreeView).SelectedNode.ChildNodes.Add(new TreeNode(nid[i]));
于 2012-11-20T11:56:01.670 回答