从数据库中的TextBox在TreeView中添加新子项?

时间:2012-09-26 09:46:04

标签: c# asp.net sql visual-studio-2010 treeview

我使用下面的代码。这个表在我的数据库中。我有文本框和按钮,我希望点击按钮可以在Proba1,Proba2上添加新的孩子...我需要做什么改变并写入我的代码:

enter image description here enter image description here

输出显示:

enter image description here

protected void Page_Load(object sender, EventArgs e)
{

    fill_Tree2();

}


void fill_Tree2()
{



    DataSet PrSet = PDataset("Select * from ParentTable");

    TreeView1.Nodes.Clear();

    foreach (DataRow dr in PrSet.Tables[0].Rows)
    {

        TreeNode tnParent = new TreeNode();

        tnParent.Text = dr["ParentName"].ToString();

        tnParent.Value = dr["ParentID"].ToString();

        tnParent.PopulateOnDemand = true;

        tnParent.ToolTip = "Click to get Child";

        tnParent.SelectAction = TreeNodeSelectAction.SelectExpand;

        tnParent.Expand();

        tnParent.Selected = true;

        TreeView1.Nodes.Add(tnParent);

        FillChild(tnParent, tnParent.Value);

    }



}

public void FillChild(TreeNode parent, string ParentId)
{

    DataSet ds = PDataset("Select * from ChildTable where ParentId =" + ParentId);

    parent.ChildNodes.Clear();

    foreach (DataRow dr in ds.Tables[0].Rows)
    {

        TreeNode child = new TreeNode();

        child.Text = dr["ChildName"].ToString().Trim();

        child.Value = dr["ChildId"].ToString().Trim();

        if (child.ChildNodes.Count == 0)
        {

            child.PopulateOnDemand = true;

        }

        child.ToolTip = "Click to get Child";

        child.SelectAction = TreeNodeSelectAction.SelectExpand;

        child.CollapseAll();

        parent.ChildNodes.Add(child);

    }

}



protected DataSet PDataset(string Select_Statement)
{

    SqlConnection SqlCon = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=cms;Integrated Security=True");

    SqlDataAdapter ad = new SqlDataAdapter(Select_Statement, SqlCon);

    DataSet ds = new DataSet();

    ad.Fill(ds);

    return ds;



}

0 个答案:

没有答案