ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Expand Collapse ASP.Net TreeView node on Button Click using jQuery<p>Hi <a class="username" rel="suhaas121"> suhaas121</a>,</p> <p>Please refer below sample.</p> <p><span style="text-decoration: underline;"><strong>HTML</strong></span></p> <pre class="brush: html">&lt;div&gt; &lt;button id=&#34;expand&#34; type=&#34;button&#34;&gt; Expand All&lt;/button&gt; &lt;asp:TreeView ID=&#34;treeview&#34; runat=&#34;server&#34; ImageSet=&#34;WindowsHelp&#34; ExpandDepth=&#34;0&#34; ShowLines=&#34;True&#34; Width=&#34;950px&#34;&gt; &lt;LeafNodeStyle CssClass=&#34;leafNode&#34; /&gt; &lt;NodeStyle CssClass=&#34;treeNode&#34; /&gt; &lt;RootNodeStyle CssClass=&#34;rootNode&#34; /&gt; &lt;SelectedNodeStyle CssClass=&#34;selectNode&#34; /&gt; &lt;/asp:TreeView&gt; &lt;/div&gt; &lt;script type=&#34;text/javascript&#34; src=&#34;http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js&#34;&gt;&lt;/script&gt; &lt;script type=&#34;text/javascript&#34;&gt; $(function () { $(&#34;#expand&#34;).on(&#34;click&#34;, function () { var treeview = $(&#34;#treeview&#34;); $(treeview).find(&#39;img&#39;).trigger(&#39;click&#39;); if ($(this).html() == &#39;Expand All&#39;) { $(this).html(&#34;Collapse All&#34;); } else { $(this).html(&#34;Expand All&#34;); } }); }); &lt;/script&gt;</pre> <p><span style="text-decoration: underline;"><strong>Namespaces</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.Data.SqlClient; using System.Data; using System.Configuration;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Data Imports System.Data.SqlClient</pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { DataTable dt = this.GetData(&#34;SELECT Id, Name FROM VehicleTypes&#34;); this.PopulateTreeView(dt, 0, null); } } private void PopulateTreeView(DataTable dtParent, int parentId, TreeNode treeNode) { foreach (DataRow row in dtParent.Rows) { TreeNode child = new TreeNode { Text = row[&#34;Name&#34;].ToString(), Value = row[&#34;Id&#34;].ToString() }; if (parentId == 0) { treeview.Nodes.Add(child); DataTable dtChild = this.GetData(&#34;SELECT Id, Name FROM VehicleSubTypes WHERE VehicleTypeId = &#34; + child.Value); PopulateTreeView(dtChild, int.Parse(child.Value), child); } else { treeNode.ChildNodes.Add(child); } } } private DataTable GetData(string query) { DataTable dt = new DataTable(); string constr = ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; using (SqlConnection con = new SqlConnection(constr)) { using (SqlCommand cmd = new SqlCommand(query)) { using (SqlDataAdapter sda = new SqlDataAdapter()) { cmd.CommandType = CommandType.Text; cmd.Connection = con; sda.SelectCommand = cmd; sda.Fill(dt); } } return dt; } }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load If Not Me.IsPostBack Then Dim dt As DataTable = Me.GetData(&#34;SELECT Id, Name FROM VehicleTypes&#34;) Me.PopulateTreeView(dt, 0, Nothing) End If End Sub Private Sub PopulateTreeView(ByVal dtParent As DataTable, ByVal parentId As Integer, ByVal treeNode As TreeNode) For Each row As DataRow In dtParent.Rows Dim child As TreeNode = New TreeNode With { .Text = row(&#34;Name&#34;).ToString(), .Value = row(&#34;Id&#34;).ToString() } If parentId = 0 Then treeview.Nodes.Add(child) Dim dtChild As DataTable = Me.GetData(&#34;SELECT Id, Name FROM VehicleSubTypes WHERE VehicleTypeId = &#34; &amp; child.Value) PopulateTreeView(dtChild, Integer.Parse(child.Value), child) Else treeNode.ChildNodes.Add(child) End If Next End Sub Private Function GetData(ByVal query As String) As DataTable Dim dt As DataTable = New DataTable() Dim constr As String = ConfigurationManager.ConnectionStrings(&#34;constr&#34;).ConnectionString Using con As SqlConnection = New SqlConnection(constr) Using cmd As SqlCommand = New SqlCommand(query) Using sda As SqlDataAdapter = New SqlDataAdapter() cmd.CommandType = CommandType.Text cmd.Connection = con sda.SelectCommand = cmd sda.Fill(dt) End Using End Using Return dt End Using End Function</pre> <p><span style="text-decoration: underline;"><strong>&nbsp;Screenshot</strong></span></p> <p><span style="text-decoration: underline;"><strong><img src="https://imgur.com/2S0SCtD.gif" alt="" width="241" height="338" /></strong></span></p>https://www.aspforums.net:443/Threads/712176/Expand-Collapse-ASPNet-TreeView-node-on-Button-Click-using-jQuery/https://www.aspforums.net:443/Threads/712176/Expand-Collapse-ASPNet-TreeView-node-on-Button-Click-using-jQuery/Tue, 13 Nov 2018 06:40:29 GMT