Tuesday, August 27, 2013

Asp.net Treeview from CodeBehind

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TreeView1.aspx.cs" Inherits="DUMMY_TreeView1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:TreeView ID="TreeView1" runat="server" >
    
    </asp:TreeView>
    </div>
    </form>
</body>
</html>


 



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class DUMMY_TreeView1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindTreeView();
        }
        
    }
    private void BindTreeView()
    {
        List<string> list1 = new List<string>();
        list1.Add("Business & Office");
        list1.Add("Database");
        list1.Add("Networking");
        list1.Add("Presentation");
        list1.Add("Project Management");
        list1.Add("Reports");
        list1.Add("Spreadsheet");
        list1.Add("Word Processing");
        List<string> list2 = new List<string>();
        list2.Add("Arts");
        list2.Add("Biographies");
        list2.Add("Children's Books");
        list2.Add("Computers & Internet");
        list2.Add("Cooking");
        list2.Add("History");
        list2.Add("Fiction");
        list2.Add("Mystery");
        list2.Add("Nonfiction");
        list2.Add("Romance");
        list2.Add("Science Fiction");
        list2.Add("Travel");
       
        //Node1 - Software
        TreeNode objNode1 = new TreeNode();
        objNode1.Text = "Software";
        TreeView1.Nodes.Add(objNode1);
        foreach (string objlist1 in list1)
        {
            TreeNode objchildNode1 = new TreeNode();
            objchildNode1.Text = objlist1;
            objNode1.ChildNodes.Add(objchildNode1);
        }
        //Node2 - Books
        //Node1 - Software
        TreeNode objNode2 = new TreeNode();
        objNode2.Text = "Books";
        TreeView1.Nodes.Add(objNode2);
        foreach (string objlist1 in list2)
        {
            TreeNode objchildNode1 = new TreeNode();
            objchildNode1.Text = objlist1;
            objNode2.ChildNodes.Add(objchildNode1);
        }
        //TreeView1.Nodes.Add(objchildNode1);
    }
   
}

Treeview

0 comments:

Post a Comment