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.Get Selected (Checked) Bootstrap TreeView Nodes Text on Button Click using jQuery in ASP.Net<p>Hi <a class="username" rel="rani"> rani</a>,</p> <p>Check this example. Now please take its reference and correct your code.</p> <p><strong><span style="text-decoration: underline;">HTML</span></strong></p> <pre class="brush: html">&lt;link rel=&#34;stylesheet&#34; href=&#34;https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css&#34; /&gt; &lt;script type=&#34;text/javascript&#34; src=&#34;https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js&#34;&gt;&lt;/script&gt; &lt;script type=&#34;text/javascript&#34; src=&#34;https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js&#34;&gt;&lt;/script&gt; &lt;script type=&#34;text/javascript&#34; src=&#34;https://cdnjs.cloudflare.com/ajax/libs/bootstrap-treeview/1.2.0/bootstrap-treeview.min.js&#34;&gt;&lt;/script&gt; &lt;script type=&#34;text/javascript&#34;&gt; $(function () { BindTreeView(); $(&#39;#btnGet&#39;).on(&#39;click&#39;, function () { var checked = $(&#39;#dvTree&#39;).treeview(&#39;getChecked&#39;); var message = &#34;Checked nodes are :\n&#34;; for (var i = 0; i &lt; checked.length; i++) { message += checked[i].text + &#34;\n&#34;; } alert(message); }); }); function BindTreeView() { $.ajax({ type: &#34;POST&#34;, url: &#34;Default.aspx/GetTreeData&#34;, data: {}, contentType: &#34;application/json; charset=utf-8&#34;, dataType: &#34;json&#34;, success: function (response) { var nodes = JSON.parse(response.d).nodes; $(&#39;#dvTree&#39;).treeview({ data: JSON.stringify(nodes), multiSelect: false, showCheckbox: true, onNodeChecked: function (event, data) { }, onNodeSelected: function (event, data) { } }); }, failure: function (response) { alert(response.responseText); }, error: function (response) { alert(response.responseText); } }); } &lt;/script&gt; &lt;div id=&#34;dvTree&#34;&gt; &lt;/div&gt; &lt;input type=&#34;button&#34; value=&#34;Get Checked&#34; id=&#34;btnGet&#34; /&gt;</pre> <p><strong><span style="text-decoration: underline;">Namespaces</span></strong></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.Data; using System.Web.Script.Serialization; using System.Web.Services;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Data Imports System.Web.Script.Serialization Imports System.Web.Services</pre> <p><strong><span style="text-decoration: underline;">Code</span></strong></p> <p><strong>C#</strong></p> <pre class="brush: csharp">[WebMethod] public static string GetTreeData() { &nbsp;&nbsp;&nbsp; DataTable dt = new DataTable(); &nbsp;&nbsp;&nbsp; dt.Columns.AddRange(new DataColumn[] { new DataColumn("Id"), new DataColumn("Text"), new DataColumn("ParentId") }); &nbsp;&nbsp;&nbsp; dt.Rows.Add(1, "IT", 0); &nbsp;&nbsp;&nbsp; dt.Rows.Add(2, "HR", 0); &nbsp;&nbsp;&nbsp; dt.Rows.Add(3, "Marketing", 0); &nbsp;&nbsp;&nbsp; dt.Rows.Add(4, "David", 1); &nbsp;&nbsp;&nbsp; dt.Rows.Add(5, "Jhon", 1); &nbsp;&nbsp;&nbsp; dt.Rows.Add(6, "Kevin", 2); &nbsp;&nbsp;&nbsp; dt.Rows.Add(7, "Marry", 2); &nbsp;&nbsp;&nbsp; dt.Rows.Add(8, "Robert", 3); &nbsp;&nbsp;&nbsp; dt.Rows.Add(9, "Andrea", 6); &nbsp;&nbsp;&nbsp; dt.Rows.Add(10, "Adam", 8); &nbsp;&nbsp;&nbsp; dt.Rows.Add(11, "Dharmendra", 4); &nbsp;&nbsp;&nbsp; dt.Rows.Add(12, "Ajeet", 5); &nbsp;&nbsp;&nbsp; Node nodes = new Node { text = "Department" }; &nbsp;&nbsp;&nbsp; DataView view = new DataView(dt); &nbsp;&nbsp;&nbsp; view.RowFilter = "ParentId=0"; &nbsp;&nbsp;&nbsp; foreach (DataRowView kvp in view) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; State state = new State(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; state.checkedd = false; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; state.disabled = false; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; state.expanded = true; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; state.selected = false; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; string parentId = kvp["Id"].ToString(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Node node = new Node { text = kvp["text"].ToString(), selectable = true, state = state }; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; nodes.nodes.Add(node); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AddChildItems(dt, node, parentId); &nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; return (new JavaScriptSerializer()).Serialize(nodes).Replace(",\"nodes\":[]", "").Replace("checkedd", "checked"); } private static void AddChildItems(DataTable dt, Node parentNode, string ParentId) { &nbsp;&nbsp;&nbsp; State state = new State(); &nbsp;&nbsp;&nbsp; state.checkedd = false; &nbsp;&nbsp;&nbsp; state.disabled = false; &nbsp;&nbsp;&nbsp; state.expanded = false; &nbsp;&nbsp;&nbsp; state.selected = false; &nbsp;&nbsp;&nbsp; DataView viewItem = new DataView(dt); &nbsp;&nbsp;&nbsp; viewItem.RowFilter = "ParentId=" + ParentId; &nbsp;&nbsp;&nbsp; foreach (DataRowView childView in viewItem) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Node node = new Node { text = childView["text"].ToString(), selectable = true, state = state }; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; parentNode.nodes.Add(node); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; string pId = childView["Id"].ToString(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AddChildItems(dt, node, pId); &nbsp;&nbsp;&nbsp; } } public class Node { &nbsp;&nbsp;&nbsp; public Node() &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; nodes = new List&lt;Node&gt;(); &nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; public string id { get; set; } &nbsp;&nbsp;&nbsp; public string text { get; set; } &nbsp;&nbsp;&nbsp; public string icon { get; set; } &nbsp;&nbsp;&nbsp; public bool selectable { get; set; } &nbsp;&nbsp;&nbsp; public State state { get; set; } &nbsp;&nbsp;&nbsp; public List&lt;Node&gt; nodes { get; set; } } public class State { &nbsp;&nbsp;&nbsp; public bool checkedd { get; set; } &nbsp;&nbsp;&nbsp; public bool disabled { get; set; } &nbsp;&nbsp;&nbsp; public bool expanded { get; set; } &nbsp;&nbsp;&nbsp; public bool selected { get; set; } }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">&lt;WebMethod()&gt; Public Shared Function GetTreeData() As String &nbsp;&nbsp;&nbsp; Dim dt As DataTable = New DataTable() &nbsp;&nbsp;&nbsp; dt.Columns.AddRange(New DataColumn() {New DataColumn("Id"), New DataColumn("Text"), New DataColumn("ParentId")}) &nbsp;&nbsp;&nbsp; dt.Rows.Add(1, "IT", 0) &nbsp;&nbsp;&nbsp; dt.Rows.Add(2, "HR", 0) &nbsp;&nbsp;&nbsp; dt.Rows.Add(3, "Marketing", 0) &nbsp;&nbsp;&nbsp; dt.Rows.Add(4, "David", 1) &nbsp;&nbsp;&nbsp; dt.Rows.Add(5, "Jhon", 1) &nbsp;&nbsp;&nbsp; dt.Rows.Add(6, "Kevin", 2) &nbsp;&nbsp;&nbsp; dt.Rows.Add(7, "Marry", 2) &nbsp;&nbsp;&nbsp; dt.Rows.Add(8, "Robert", 3) &nbsp;&nbsp;&nbsp; dt.Rows.Add(9, "Andrea", 6) &nbsp;&nbsp;&nbsp; dt.Rows.Add(10, "Adam", 8) &nbsp;&nbsp;&nbsp; dt.Rows.Add(11, "Dharmendra", 4) &nbsp;&nbsp;&nbsp; dt.Rows.Add(12, "Ajeet", 5) &nbsp;&nbsp;&nbsp; Dim nodes As Node = New Node With { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .text = "Department" &nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; Dim view As DataView = New DataView(dt) &nbsp;&nbsp;&nbsp; view.RowFilter = "ParentId=0" &nbsp;&nbsp;&nbsp; For Each kvp As DataRowView In view &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim state As State = New State() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; state.checkedd = False &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; state.disabled = False &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; state.expanded = True &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; state.selected = False &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim parentId As String = kvp("Id").ToString() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim node As Node = New Node With { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .text = kvp("text").ToString(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .selectable = True, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .state = state &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; nodes.nodes.Add(node) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AddChildItems(dt, node, parentId) &nbsp;&nbsp;&nbsp; Next &nbsp;&nbsp;&nbsp; Return (New JavaScriptSerializer()).Serialize(nodes).Replace(",""nodes"":[]", "").Replace("checkedd", "checked") End Function Private Shared Sub AddChildItems(ByVal dt As DataTable, ByVal parentNode As Node, ByVal ParentId As String) &nbsp;&nbsp;&nbsp; Dim state As State = New State() &nbsp;&nbsp;&nbsp; state.checkedd = False &nbsp;&nbsp;&nbsp; state.disabled = False &nbsp;&nbsp;&nbsp; state.expanded = False &nbsp;&nbsp;&nbsp; state.selected = False &nbsp;&nbsp;&nbsp; Dim viewItem As DataView = New DataView(dt) &nbsp;&nbsp;&nbsp; viewItem.RowFilter = "ParentId=" &amp; ParentId &nbsp;&nbsp;&nbsp; For Each childView As DataRowView In viewItem &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim node As Node = New Node With { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .text = childView("text").ToString(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .selectable = True, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .state = state &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; parentNode.nodes.Add(node) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim pId As String = childView("Id").ToString() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AddChildItems(dt, node, pId) &nbsp;&nbsp;&nbsp; Next End Sub Public Class Node &nbsp;&nbsp;&nbsp; Public Sub New() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; nodes = New List(Of Node)() &nbsp;&nbsp;&nbsp; End Sub &nbsp;&nbsp;&nbsp; Public Property id As String &nbsp;&nbsp;&nbsp; Public Property text As String &nbsp;&nbsp;&nbsp; Public Property icon As String &nbsp;&nbsp;&nbsp; Public Property selectable As Boolean &nbsp;&nbsp;&nbsp; Public Property state As State &nbsp;&nbsp;&nbsp; Public Property nodes As List(Of Node) End Class Public Class State &nbsp;&nbsp;&nbsp; Public Property checkedd As Boolean &nbsp;&nbsp;&nbsp; Public Property disabled As Boolean &nbsp;&nbsp;&nbsp; Public Property expanded As Boolean &nbsp;&nbsp;&nbsp; Public Property selected As Boolean End Class</pre> <p><strong><span style="text-decoration: underline;">Screenshot</span></strong></p> <p><img src="https://i.imgur.com/KyMBmZN.jpg" alt="" width="398" height="422" /></p> <p>For more function on Bootstrap TreeView refer below link.</p> <p><a href="https://github.com/jonmiles/bootstrap-treeview">https://github.com/jonmiles/bootstrap-treeview</a> </p>https://www.aspforums.net:443/Threads/121531/Get-Selected-Checked-Bootstrap-TreeView-Nodes-Text-on-Button-Click-using-jQuery-in-ASPNet/https://www.aspforums.net:443/Threads/121531/Get-Selected-Checked-Bootstrap-TreeView-Nodes-Text-on-Button-Click-using-jQuery-in-ASPNet/Wed, 28 Aug 2019 05:56:13 GMT