Tree View Menu Control
Description:
TreeView.aspx:
TreeView.aspx.cs:
} Screen Shots:
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);
}
}
}
Subscribe to:
Post Comments (Atom)
Labels
- Abstraction in Object Oriented Programming (OOPS) Concept (1)
- Access ChildControls in Gridview using Javascript (1)
- Add a WCF Service Reference to the Client (1)
- ASP.Net GridView Highlight Row onmouseover (1)
- ASP.NET View State And ViewStateEncryptionModes Overview (1)
- Calling Javascript From Any Part Of Code Behind Page By Registering The Script (1)
- Check And UnCheck CheckBoxes In Gridview using javascript (1)
- contact your server administrator. (1)
- DataControlField class (1)
- DataKeys ID (Identity Column) in Child Controls events in GridView (1)
- DataList Paging With PagedDataSource (1)
- Declaring Session in Asp.Net (1)
- Difference between Struct and Class (1)
- Displaying Images In GridView From DataBase (1)
- Dynamic Sitemaps in ASP.NET (1)
- Err: You must install Office SharePoint Server 2007 – Please read Microsoft Knowledge Base article: 962935 with the most recent service pack (1)
- GridView Class (1)
- Gridview Inside GridView (1)
- Gridview Paging using C# (1)
- Gridview Sorting Using C# (1)
- GridView.RowDataBound EventEvent (1)
- GridViewRow (1)
- Inserting Images To Database And Display in GridView (1)
- Microsoft Office Sharepoint Server 2007 on Windows Server 2008 – This Program is blocked due to compatibility issues (1)
- ModalPopUp Using CSS and Div To Reduce the Weight On WebPage (1)
- Session State in Asp.Net OverView (1)
- SharePoint 2010 – The form cannot be rendered. This may be due to a misconfiguration of the Microsoft SharePoint Server State Service. For more information (1)
- Tooltip GridView Header (1)
- Tree View Menu Control (1)
- WCF Service (12)
- What is Encapsulation in OOPS? (1)
Topics
- Abstraction in Object Oriented Programming (OOPS) Concept (1)
- Access ChildControls in Gridview using Javascript (1)
- Add a WCF Service Reference to the Client (1)
- ASP.Net GridView Highlight Row onmouseover (1)
- ASP.NET View State And ViewStateEncryptionModes Overview (1)
- Calling Javascript From Any Part Of Code Behind Page By Registering The Script (1)
- Check And UnCheck CheckBoxes In Gridview using javascript (1)
- contact your server administrator. (1)
- DataControlField class (1)
- DataKeys ID (Identity Column) in Child Controls events in GridView (1)
- DataList Paging With PagedDataSource (1)
- Declaring Session in Asp.Net (1)
- Difference between Struct and Class (1)
- Displaying Images In GridView From DataBase (1)
- Dynamic Sitemaps in ASP.NET (1)
- Err: You must install Office SharePoint Server 2007 – Please read Microsoft Knowledge Base article: 962935 with the most recent service pack (1)
- GridView Class (1)
- Gridview Inside GridView (1)
- Gridview Paging using C# (1)
- Gridview Sorting Using C# (1)
- GridView.RowDataBound EventEvent (1)
- GridViewRow (1)
- Inserting Images To Database And Display in GridView (1)
- Microsoft Office Sharepoint Server 2007 on Windows Server 2008 – This Program is blocked due to compatibility issues (1)
- ModalPopUp Using CSS and Div To Reduce the Weight On WebPage (1)
- Session State in Asp.Net OverView (1)
- SharePoint 2010 – The form cannot be rendered. This may be due to a misconfiguration of the Microsoft SharePoint Server State Service. For more information (1)
- Tooltip GridView Header (1)
- Tree View Menu Control (1)
- WCF Service (12)
- What is Encapsulation in OOPS? (1)
0 Responses to “Tree View Menu Control”
Post a Comment