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.Populate (Bind) Nested DataList using C# and VB.Net in ASP.Net<p>Hi <a class="username" rel="itsme"> itsme</a>,</p> <p>Refer below sample.</p> <p><span style="text-decoration: underline;"><strong>HTML</strong></span></p> <pre class="brush: html">&lt;asp:DataList ID=&#34;DataList1&#34; runat=&#34;server&#34; RepeatColumns=&#34;3&#34; RepeatDirection=&#34;Horizontal&#34; OnItemCommand=&#34;DataList1_ItemCommand&#34; Width=&#34;600px&#34; OnItemDataBound=&#34;DataList1_ItemDataBound&#34;&gt; &lt;ItemTemplate&gt; &lt;table class=&#34;auto-style1&#34; border=&#34;1&#34;&gt; &lt;tr&gt; &lt;tr&gt; &lt;td class=&#34;auto-style2&#34;&gt; &lt;asp:Label ID=&#34;lblid&#34; runat=&#34;server&#34; Font-Bold=&#34;True&#34; Text=&#39;&lt;%# Eval(&#34;Id&#34;) %&gt;&#39;&gt;&lt;/asp:Label&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td class=&#34;auto-style2&#34;&gt; &lt;asp:Label ID=&#34;category&#34; runat=&#34;server&#34; Font-Bold=&#34;True&#34; Text=&#39;&lt;%# Eval(&#34;Category&#34;) %&gt;&#39; CssClass=&#34;btn-lg&#34;&gt;&lt;/asp:Label&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td class=&#34;auto-style2&#34;&gt; &lt;asp:DataList ID=&#34;DataList2&#34; runat=&#34;server&#34;&gt; &lt;ItemTemplate&gt; &lt;table class=&#34;auto-style1&#34; border=&#34;1&#34;&gt; &lt;tr&gt; &lt;td class=&#34;auto-style2&#34;&gt; &lt;asp:Label ID=&#34;id&#34; runat=&#34;server&#34; Font-Bold=&#34;True&#34; Text=&#39;&lt;%# Eval(&#34;Id&#34;) %&gt;&#39;&gt;&lt;/asp:Label&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td class=&#34;auto-style2&#34;&gt; &lt;asp:Label ID=&#34;Bname&#34; runat=&#34;server&#34; Font-Bold=&#34;True&#34; Text=&#39;&lt;%# Eval(&#34;Sub_category&#34;) %&gt;&#39; CssClass=&#34;btn-lg&#34;&gt;&lt;/asp:Label&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/ItemTemplate&gt; &lt;/asp:DataList&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td class=&#34;auto-style2&#34;&gt; &lt;asp:Button ID=&#34;Button1&#34; runat=&#34;server&#34; CommandArgument=&#39;&lt;%# Eval(&#34;Id&#34;) %&gt;&#39; CommandName=&#34;viewdetails&#34; CssClass=&#34;btn btn-warning&#34; Font-Bold=&#34;true&#34; Text=&#34;View Details&#34; /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/ItemTemplate&gt; &lt;/asp:DataList&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; </pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Data</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 = new DataTable(); dt.Columns.AddRange(new DataColumn[] { new DataColumn(&#34;Id&#34;, typeof(int)), new DataColumn(&#34;Category&#34;, typeof(string)) }); dt.Rows.Add(1, &#34;A&#34;); dt.Rows.Add(2, &#34;B&#34;); dt.Rows.Add(3, &#34;C&#34;); DataList1.DataSource = dt; DataList1.DataBind(); } } protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e) { if (e.CommandName == &#34;viewdetails&#34;) { ClientScript.RegisterClientScriptBlock(this.GetType(), &#34;&#34;, &#34;alert(&#39;&#34; + &#34;Id is : &#34; + e.CommandArgument.ToString() + &#34;&#39;)&#34;, true); } } public DataTable SubCategoryData() { DataTable dt = new DataTable(); dt.Columns.AddRange(new DataColumn[] { new DataColumn(&#34;Id&#34;, typeof(int)), new DataColumn(&#34;Sub_category&#34;, typeof(string)) }); dt.Rows.Add(1, &#34;a1&#34;); dt.Rows.Add(1, &#34;a11&#34;); dt.Rows.Add(2, &#34;b1&#34;); dt.Rows.Add(2, &#34;b11&#34;); dt.Rows.Add(3, &#34;c1&#34;); dt.Rows.Add(3, &#34;c11&#34;); return dt; } protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { DataRowView dataRowView = e.Item.DataItem as DataRowView; string id = dataRowView[&#34;Id&#34;].ToString(); DataList subDataList = e.Item.FindControl(&#34;DataList2&#34;) as DataList; DataTable dt = SubCategoryData(); DataRow[] dr = dt.Select(&#34;Id = &#34; + id); subDataList.DataSource = dr.CopyToDataTable(); subDataList.DataBind(); } }</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 = New DataTable() dt.Columns.AddRange(New DataColumn() {New DataColumn(&#34;Id&#34;, GetType(Integer)), New DataColumn(&#34;Category&#34;, GetType(String))}) dt.Rows.Add(1, &#34;A&#34;) dt.Rows.Add(2, &#34;B&#34;) dt.Rows.Add(3, &#34;C&#34;) DataList1.DataSource = dt DataList1.DataBind() End If End Sub Protected Sub DataList1_ItemCommand(ByVal source As Object, ByVal e As DataListCommandEventArgs) If e.CommandName = &#34;viewdetails&#34; Then ClientScript.RegisterClientScriptBlock(Me.GetType(), &#34;&#34;, &#34;alert(&#39;&#34; &amp; &#34;Id is : &#34; &amp; e.CommandArgument.ToString() &amp; &#34;&#39;)&#34;, True) End If End Sub Public Function SubCategoryData() As DataTable Dim dt As DataTable = New DataTable() dt.Columns.AddRange(New DataColumn() {New DataColumn(&#34;Id&#34;, GetType(Integer)), New DataColumn(&#34;Sub_category&#34;, GetType(String))}) dt.Rows.Add(1, &#34;a1&#34;) dt.Rows.Add(1, &#34;a11&#34;) dt.Rows.Add(2, &#34;b1&#34;) dt.Rows.Add(2, &#34;b11&#34;) dt.Rows.Add(3, &#34;c1&#34;) dt.Rows.Add(3, &#34;c11&#34;) Return dt End Function Protected Sub DataList1_ItemDataBound(ByVal sender As Object, ByVal e As DataListItemEventArgs) If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then Dim dataRowView As DataRowView = TryCast(e.Item.DataItem, DataRowView) Dim id As String = dataRowView(&#34;Id&#34;).ToString() Dim subDataList As DataList = TryCast(e.Item.FindControl(&#34;DataList2&#34;), DataList) Dim dt As DataTable = SubCategoryData() Dim dr As DataRow() = dt.[Select](&#34;Id = &#34; &amp; id) subDataList.DataSource = dr.CopyToDataTable() subDataList.DataBind() End If End Sub</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><img src="https://i.imgur.com/iyEyCEj.gif" alt="" width="425" height="280" /></p>https://www.aspforums.net:443/Threads/760327/Populate-Bind-Nested-DataList-using-C-and-VBNet-in-ASPNet/https://www.aspforums.net:443/Threads/760327/Populate-Bind-Nested-DataList-using-C-and-VBNet-in-ASPNet/Wed, 04 Sep 2019 04:54:06 GMT