-->

How to Populate Treeview Nodes Dynamically (On Demand) using Ajax in ASP.NET

Introduction

In this post, I explain How to Populate Treeview Nodes Dynamically (On Demand) using Ajax in ASP.NET
My previous releted post was : How to Bind Treeview from database using recursion function in asp.net c#.

Steps :

Step - 1 : Create New Project.

Go to File > New > Project > Select asp.net web forms application > Entry Application Name > Click OK.

Step-2: Add a Database.

Go to Solution Explorer > Right Click on App_Data folder > Add > New item > Select SQL Server Database Under Data > Enter Database name > Add.

Step-3: Create table for fetch data for show in treeview.

Open Database > Right Click on Table > Add New Table > Add Columns > Save > Enter table name > Ok.
In this example, I have used two tables as below




Step-4: Add Entity Data Model.

Go to Solution Explorer > Right Click on Project name form Solution Explorer > Add > New item > Select ADO.net Entity Data Model under data > Enter model name > Add.
A popup window will come (Entity Data Model Wizard) > Select Generate from database > Next >
Chose your data connection > select your database > next > Select tables > enter Model Namespace > Finish.

Step-5: Add a Animated image for show in Loding panel.

Go to Solution Explorer > Right Click on Project name form Solution Explorer > Add > Existing item > Browse & Select File > Add.

Step-6: Add a Webpage and Design for Show Data in Treeview

Go to Solution Explorer > Right Click on Project name form Solution Explorer > Add > New item > Select web form/ web form using master page under Web > Enter page name > Add.

Here I have used a little bit Jquery code for show Loding Panel in proper location.
JS Code
  1. <%-- Here I am writing some Jquery Code for show loading panel at proper place --%>
  2. <script src="Scripts/jquery-1.7.1.js"></script>
  3. <script language="javascript" type="text/javascript">
  4. function placeUP() {
  5. var mouseX;
  6. var mouseY;
  7. // below line for get mouse position
  8. $(document).mousemove(function (e) {
  9. mouseX = e.pageX;
  10. mouseY = e.pageY;
  11.  
  12. });
  13. // below line for show loading panel at proper place
  14. $('#<%= TreeView1.ClientID%> a').click(function () {
  15. $('#UP').css({'top': mouseY, 'left': mouseX});
  16. });
  17. }
  18. </script>
HTML Code
  1. <h3>Populate Treeview Nodes Dynamically (On Demand)</h3>
  2. <%-- Scripr manager is required as we are using AJAX --%>
  3. <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
  4. <%-- UpdateProgress is used for show loading panel while loading child nodes --%>
  5. <asp:UpdateProgress ID="UpdateProgress1" runat="server">
  6. <ProgressTemplate>
  7. <div id="UP" style="position:absolute;background-image:url('ajax-loader.gif'); background-repeat:no-repeat;
  8. width:20px;">&nbsp;</div>
  9. </ProgressTemplate>
  10. </asp:UpdateProgress>
  11. <asp:UpdatePanel ID="UpdatePanel1" runat="server">
  12. <ContentTemplate>
  13.  
  14. <script type="text/javascript">
  15. Sys.Application.add_load(placeUP);
  16. </script>
  17. <%-- Here ExpandDepth="0" for eleminates the expansion of the added treenodes --%>
  18.  
  19. <asp:TreeView ID="TreeView1" runat="server" ExpandDepth="0" PopulateNodesFromClient="false" OnTreeNodePopulate="TreeView1_TreeNodePopulate"
  20. >
  21.  
  22. </asp:TreeView>
  23. </ContentTemplate>
  24. </asp:UpdatePanel>

Step-7: Write following code in Page_Load event for Show data (Main) in Treeview.


  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. if (!IsPostBack)
  4. {
  5. PopulateCountry();
  6. }
  7. }

and here is the function...

  1. private void PopulateCountry()
  2. {
  3. List<CountryMaster> allCountry = new List<CountryMaster>();
  4. using (MyDatabaseEntities dc = new MyDatabaseEntities())
  5. {
  6. allCountry = dc.CountryMasters.ToList();
  7. }
  8. foreach (CountryMaster c in allCountry)
  9. {
  10. TreeNode t = new TreeNode(c.CountryName, c.CountryID.ToString());
  11. t.PopulateOnDemand = true;
  12. TreeView1.Nodes.Add(t);
  13. }
  14. }


Step-8: Write following code in TreeView1_TreeNodePopulate event for Populate Sub Node(s) under Main Node in Treeview



  1. protected void TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
  2. {
  3. // This code is for populate Child nodes
  4. TreeNode main = e.Node;
  5. int countryID = Convert.ToInt32(main.Value);
  6. List<StateMaster> allState = new List<StateMaster>();
  7. using (MyDatabaseEntities dc = new MyDatabaseEntities())
  8. {
  9. allState = dc.StateMasters.Where(a => a.CountryID.Equals(countryID)).OrderBy(a => a.StateName).ToList();
  10. }
  11. foreach (StateMaster s in allState)
  12. {
  13. TreeNode sub = new TreeNode(s.StateName, s.StateID.ToString());
  14. main.ChildNodes.Add(sub);
  15. }
  16. }

Step-9: Run Application.





Hello ! My name is Sourav Mondal. I am a software developer working in Microsoft .NET technologies since 2010.

I like to share my working experience, research and knowledge through my site.

I love developing applications in Microsoft Technologies including Asp.Net webforms, mvc, winforms, c#.net, sql server, entity framework, Ajax, Jquery, web api, web service and more.

Related Posts: