Tree View Menu Control

Description:
Here I am discussing about Tree View control.

 TreeView.aspx:

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeFile="TreeView.aspx.cs" Inherits="TreeView" %>

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="Server">
<asp:TreeView ID="tvCategory" runat="server"  OnTreeNodePopulate="Node_Populate">
<%--ExpandImageUrl="~/Images/closed.gif"  CollapseImageUrl="~/Images/open.gif"--%>
<Nodes>
<asp:TreeNode Text="Categories" Value="0" PopulateOnDemand="True"></asp:TreeNode>
</Nodes>
</asp:TreeView>
</asp:Content>



TreeView.aspx.cs:
Write the below code in the code behind 
Here I am using 3 tables:
1)Category
2)SubCategory
3)MinCategory
























public partial class TreeView : System.Web.UI.Page
{
    SqlConnection con;
    SqlCommand cmd;
    SqlDataAdapter adapter;
    private DataTable dtSource = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Node_Populate(object sender, TreeNodeEventArgs e)
    {
        if (e.Node.ChildNodes.Count == 0)
        {
            switch (e.Node.Depth)
            {
                case 0:
                    FillCategories(e.Node);
                    break;
                case 1:
                    FillSubCategoriesForCategories(e.Node);
                    break;
                case 2:
                    FillMinCategoriesForSubCategories(e.Node);
                    break;
            }

        }
    }
    //Category 
    private void FillCategories(TreeNode node)
    {
        con = new SqlConnection("Data Source=TAPA-PC;Initial Catalog=Customer;User ID=sa;Password=123");      
        adapter = new SqlDataAdapter("select * from Category", con);
        DataSet Categories = new DataSet();  // Categories is dataset name
        adapter.Fill(Categories);
        if (Categories.Tables.Count > 0)
        {
            foreach (DataRow row in Categories.Tables[0].Rows)
            {
                TreeNode newNode = new TreeNode(row["Category_Name"].ToString(), row["Category_Id"].ToString());
                newNode.PopulateOnDemand = true ;
                newNode.SelectAction = TreeNodeSelectAction.Expand;
                node.ChildNodes.Add(newNode);
            }
        }
    }

    //SubCategory
    private void FillSubCategoriesForCategories(TreeNode node)
    {
        string authorID = node.Value;

        con = new SqlConnection("Data Source=TAPA-PC;Initial Catalog=Customer;User ID=sa;Password=123");
        cmd = new SqlCommand("select * from SubCategory where CategoryID=" + authorID, con); // stored proc      
        adapter = new SqlDataAdapter(cmd);

        DataSet SubCategoriesForCategories = new DataSet();
        adapter.Fill(SubCategoriesForCategories);
        if (SubCategoriesForCategories.Tables.Count > 0)
        {
            foreach (DataRow row in SubCategoriesForCategories.Tables[0].Rows)
            {
                TreeNode newNode = new TreeNode(row["SubCategory"].ToString(), row["SubCategoryID"].ToString());               
                newNode.SelectAction = TreeNodeSelectAction.Expand;
                node.ChildNodes.Add(newNode);
                FillMinCategoriesForSubCategories(newNode);
            }
        }
    }

    //SubMinCategory
    private void FillMinCategoriesForSubCategories(TreeNode node)
    {
        string authorID = node.Value;

        con = new SqlConnection("Data Source=TAPA-PC;Initial Catalog=Customer;User ID=sa;Password=123");
        cmd = new SqlCommand("select * from MinCategory where SubCategoryID=" + authorID, con); // stored proc      
        adapter = new SqlDataAdapter(cmd);

        DataSet SubCategoriesForCategories = new DataSet();
        adapter.Fill(SubCategoriesForCategories);
        if (SubCategoriesForCategories.Tables.Count > 0)
        {
            foreach (DataRow row in SubCategoriesForCategories.Tables[0].Rows)
            {
                TreeNode newNode = new TreeNode(row["MinCategory"].ToString(), row["MinCategoryID"].ToString());
                newNode.SelectAction = TreeNodeSelectAction.Expand;
                node.ChildNodes.Add(newNode);              
            }
        }
    }
 }
Screen Shots:






0 Responses to “Tree View Menu Control”

Post a Comment

Labels

Topics