将属性和属性添加到TreeView

时间:2019-01-24 21:45:56

标签: c# asp.net

我有一个ASP.net Treeview控件,需要在其中添加新的属性属性

1)前端树视图

<asp:TreeView ID="Treeview1" runat="server">
</asp:TreeView>

2)后端逻辑

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        GetTreeViewItems();
    }
}

private void GetTreeViewItems()
{
    string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
    SqlConnection con = new SqlConnection(cs);
    SqlDataAdapter da = new SqlDataAdapter("spGetTreeViewItems", con);
    DataSet ds = new DataSet();
    da.Fill(ds);

    ds.Relations.Add("ChildRows", ds.Tables[0].Columns["ID"], 
        ds.Tables[0].Columns["ParentId"]);

    foreach (DataRow level1DataRow in ds.Tables[0].Rows)
    {
        if (string.IsNullOrEmpty(level1DataRow["ParentId"].ToString()))
        {
            TreeNode treeNode = new TreeNode();
            treeNode.Text = level1DataRow["TreeViewText"].ToString();
            treeNode.NavigateUrl = level1DataRow["NavigateURL"].ToString();
 //This is what i want to add which does not exist
            treeNode.Category = "TopNode"; 


            DataRow[] level2DataRows = level1DataRow.GetChildRows("ChildRows");
            foreach (DataRow level2DataRow in level2DataRows)
            {
                TreeNode childTreeNode = new TreeNode();
                childTreeNode.Text = level2DataRow["TreeViewText"].ToString();
                childTreeNode.NavigateUrl = level2DataRow["NavigateURL"].ToString();

           **//This is the property i want to add Category which does not exist**
                childTreeNode.Category = "ChildNode";

//This is the attribute i want to add 
childTreeNode.Attributes.Add("Mode", level2DataRow["Mode"].ToString()); 

                treeNode.ChildNodes.Add(childTreeNode);
            }
            Treeview1.Nodes.Add(treeNode);
        }
    }
}

上面要在TreeView中添加的我要添加的东西是

treeNode.Category = "TopNode"; 

childTreeNode.Category = "ChildNode";        

childTreeNode.Attributes.Add("Mode", level2DataRow["Mode"].ToString())

如果您有任何线索,请告诉我。谢谢。如果您需要更多信息,请告诉我。

0 个答案:

没有答案