Tuesday, 5 March 2013

How to use Treeview control in Asp.net applications

How to use Treeview control in Asp.net applications

Read more: http://www.developerscode.com/2012/08/how-to-use-treeview-control-in-aspnet.html#ixzz2Meq1l641



Hi friends, in this article I would like to explain how to bind data to Tree View control in asp.net using c#. From last few weeks we are getting so many requests about tree view control example in asp.net applications. This time we are going to explain a basic tutorial about treeview control for beginner’s.

Please follow the steps

Step :1
 In this article I am considered States and its districts as nodes and its child nodes. Let us consider a sql table as below:
  1.    
  2. SET ANSI_NULLS ON  
  3. GO  
  4.   
  5. SET QUOTED_IDENTIFIER ON  
  6. GO  
  7.   
  8. SET ANSI_PADDING ON  
  9. GO  
  10.   
  11. CREATE TABLE [dbo].[ExampleOnTreeView](  
  12.  [Id] [int] NULL,  
  13.  [StateId] [int] NULL,  
  14.  [StateName] [varchar](50) NULL  
  15. ) ON [PRIMARY]  
  16.   
  17. GO  
  18.   
  19. SET ANSI_PADDING OFF  
  20. GO  

 Please find the below screen shot(ExampleTreeViewTable.jpg) for sample data that I shown in tree view output.

 

Step 2:
Then,coming to code for TreeView control in asp.net: In .aspx page:

  1. <%@ Page Title="" Language="C#" AutoEventWireup="true" CodeFile="TreeViewExample.aspx.cs" Inherits="Year" %><title></title><form id="form1" runat="server">  
  2. <span style="font-size: small;">Tree View Example : <asp:treeview id="TreeViewStatesAndDistricts" imageset="Msdn" runat="server">  
  3.     </asp:treeview>  
  4.     </span></form>  
  5. <span style="font-size: small;">  
  6. </span>  
Step 3:

In .aspx.cs page:
  1. <span style="font-size: small;">  
  2. using System;  
  3. using System.Web;  
  4. using System.Web.UI;  
  5. using System.Web.UI.WebControls;  
  6. using System.Data.SqlClient;  
  7. using System.Data;  
  8. using System.Configuration;  
  9.   
  10. public partial class TreeViewExample : System.Web.UI.Page  
  11. {  
  12.     SqlConnection con;  
  13.     protected void Page_Load(object sender, EventArgs e)  
  14.     {  
  15.         try  
  16.         {  
  17.             con = new SqlConnection(ConfigurationManager.ConnectionStrings["TestConnection"].ConnectionString);  
  18.             if (!Page.IsPostBack)  
  19.             {  
  20.                 //Getting State Names from the database table(ExampleOnTreeView).  
  21.                 SqlCommand cmdStates = new SqlCommand("Select Id,StateName from ExampleOnTreeView where StateId is null", con);   
  22.                 DataSet dsStates = new DataSet();  
  23.                 SqlDataAdapter daStates = new SqlDataAdapter(cmdStates);  
  24.                 daStates.Fill(dsStates);  
  25.   
  26.                 int StatesCount = dsStates.Tables[0].Rows.Count;  
  27.                 string[] Names = new string[StatesCount];  
  28.                 int i = 0;  
  29.   
  30.                 foreach (DataRow drStates in dsStates.Tables[0].Rows)  
  31.                 {  
  32.                     TreeViewStatesAndDistricts.Nodes.Add(new TreeNode(drStates["StateName"].ToString(), drStates["Id"].ToString()));  
  33.                     Names[i] = drStates["id"].ToString();  
  34.                     i++;  
  35.                 }  
  36.   
  37.   
  38.                 if (i > 0)  
  39.                 {  
  40.                     // Getting District Names from the existing state.  
  41.                     string DistrictsQuery = "";  
  42.                     i = 0;  
  43.                     for (int j = 0; j < StatesCount; j++)  
  44.                     {  
  45.                         if (j == 0)  
  46.                             DistrictsQuery = "StateId = " + Names[j];  
  47.                         else  
  48.                             DistrictsQuery += " or StateId = " + Names[j];  
  49.                     }  
  50.   
  51.                     SqlCommand cmdDistricts = new SqlCommand("Select Id,StateId,StateName from ExampleOnTreeView where " + DistrictsQuery, con);  
  52.                     DataSet dsDistricts = new DataSet();  
  53.                     SqlDataAdapter daDistricts = new SqlDataAdapter(cmdDistricts);  
  54.                     daDistricts.Fill(dsDistricts);  
  55.   
  56.                     StatesCount = dsDistricts.Tables[0].Rows.Count;  
  57.                     Names = new string[StatesCount];  
  58.   
  59.                     foreach (DataRow drDistricts in dsDistricts.Tables[0].Rows)  
  60.                     {  
  61.                         TreeViewStatesAndDistricts.FindNode(drDistricts["StateId"].ToString()).ChildNodes.Add(new TreeNode(drDistricts["StateName"].ToString(), drDistricts["Id"].ToString()));  
  62.                     }  
  63.   
  64.                 }  
  65.             }  
  66.         }  
  67.         catch  
  68.         {   
  69.           
  70.         }  
  71.     }     
  72. }  
  73.   
  74. </span>  
After executing the above code by pressing (F5) it will generate the States & districts in Tree View format as below (ExampleOnTreeView.jpg): 



Read more: http://www.developerscode.com/2012/08/how-to-use-treeview-control-in-aspnet.html#ixzz2Meq6asUF

No comments:

Post a Comment