Treeview Control in ASP.NET

Below code use to Create a treeview control in asp.net

Aspx Page

<form id="form1" runat="server">

 <div>           

    <asp:TreeView ID="TreeView1" runat="server">            

    </asp:TreeView> 

 </div> 

</form>

Aspx.cs
 

public partial class TreeView : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

        // Create the DataTable and columns

        DataTable ItemTable = new DataTable("MyTable");

        ItemTable.Columns.Add("ID", typeof(int));

        ItemTable.Columns.Add("ParentID", typeof(int));

        ItemTable.Columns.Add("Name", typeof(String));

        // add some test data

        ItemTable.Rows.Add(new object[] { 0, -1, "Bill Gates" });

        ItemTable.Rows.Add(new object[] { 1, 0, "Steve Ballmer" });

        ItemTable.Rows.Add(new object[] { 3, 1, "Mary Smith" });

        ItemTable.Rows.Add(new object[] { 2, 0, "Paul Allen" });

        ItemTable.Rows.Add(new object[] { 4, 2, "Ahmed Jones" });

        ItemTable.Rows.Add(new object[] { 5, 2, "Wing Lee" });

        DataRow[] SortedRows;

        SortedRows = ItemTable.Select("", "ParentID");

        XmlDocument XDoc = new XmlDocument();

        XmlDeclaration XDec = XDoc.CreateXmlDeclaration("1.0", null, null);

        XDoc.AppendChild(XDec);

        foreach (DataRow Row in SortedRows)

        {

            // create an element node to insert

            // note: Element names may not have spaces so use ID

            // note: Element names may not start with a digit so add underscore

            XmlElement NewNode = XDoc.CreateElement("_" + Row["ID"].ToString());

            NewNode.SetAttribute("ID", Row["ID"].ToString());

            NewNode.SetAttribute("ParentID", Row["ParentID"].ToString());

            NewNode.SetAttribute("FullName", Row["Name"].ToString());

            // special case for top level node

            if ((int)Row["ParentID"] == -1)

                XDoc.AppendChild(NewNode);  // root node

            else

            {

                // use XPath to find the parent node in the tree

                String SearchString;

                SearchString = String.Format("//*[@ID=\"{0}\"] ", Row["ParentID"].ToString());

                XmlNode Parent = XDoc.SelectSingleNode(SearchString);

                if (Parent != null)

                    Parent.AppendChild(NewNode);

                else

                    ; // Handle Error: Employee with no boss

            }

        }

        XmlDataSource XDdataSource = new XmlDataSource();

        XDdataSource.ID = DateTime.Now.Ticks.ToString();  // unique ID is required

        XDdataSource.Data = XDoc.OuterXml;

        TreeNodeBinding Binding = new TreeNodeBinding();

        Binding.TextField = "FullName";

        Binding.ValueField = "ID";

        TreeView1.DataBindings.Add(Binding);

        // Finally! Hook that bad boy up!      

        TreeView1.DataSource = XDdataSource;

        TreeView1.DataBind();

    }

}

Ebook Download
View all
Learn
View all