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 Command Argument value on child DataList Button Click using C# and VB.Net in ASP.Net<p>Hi <a class="username" rel="itsme"> itsme</a>,</p> <p>Your Button is inside the child DataList. So you have to add OnItemCommand in child DataList.</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;asp:Content ID=&#34;Content2&#34; ContentPlaceHolderID=&#34;ContentPlaceHolder1&#34; runat=&#34;Server&#34;&gt; &lt;div class=&#34;container&#34; style=&#34;margin-left: 30%; width: 2000px; height: 1000px&#34;&gt; &lt;div class=&#34;row&#34;&gt; &lt;div class=&#34;col-sm-4&#34;&gt; &lt;div class=&#34;shadow-lg p-3 mb-5 bg-white rounded panel panel-blue&#34;&gt; &lt;div class=&#34;panel-heading&#34;&gt; &lt;center&gt; &lt;b&gt;Search By Category&lt;/b&gt;&lt;/center&gt; &lt;/div&gt; &lt;div class=&#34;panel-body&#34;&gt; &lt;asp:DataList ID=&#34;DataList1&#34; runat=&#34;server&#34; RepeatColumns=&#34;2&#34; RepeatDirection=&#34;Horizontal&#34; OnItemDataBound=&#34;DataList1_ItemDataBound&#34;&gt; &lt;ItemTemplate&gt; &lt;table class=&#34;auto-style1&#34; border=&#34;1&#34; style=&#34;margin-bottom: 40px&#34;&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;br /&gt; &lt;br /&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; OnItemCommand=&#34;DataList2_ItemCommand&#34;&gt; &lt;ItemTemplate&gt; &lt;table class=&#34;auto-style1&#34; border=&#34;1&#34;&gt; &lt;asp:Panel runat=&#34;server&#34; ID=&#34;pnlid&#34; Visible=&#34;false&#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; Visible=&#34;false&#34;&gt;&lt;/asp:Label&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/asp:Panel&gt; &lt;tr&gt; &lt;td class=&#34;auto-style2&#34;&gt; &lt;asp:Button ID=&#34;Bname&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;Sub_category&#34;) %&gt;&#39; CommandArgument=&#39;&lt;%# Eval(&#34;Sub_category&#34;) %&gt;&#39; CommandName=&#34;viewdetails&#34; CssClass=&#34;btn auto-style2&#34; BackColor=&#34;#0099cc&#34; Font-Bold=&#34;true&#34; Font-Size=&#34;Large&#34; ForeColor=&#34;White&#34; /&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;/table&gt; &lt;/ItemTemplate&gt; &lt;/asp:DataList&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/asp:Content&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;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Data</pre> <p><strong><span style="text-decoration: underline;">Code</span></strong></p> <p><strong>C#</strong></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { DataTable dt = Category_tbl().DefaultView.ToTable(true, &#34;Category&#34;); DataList1.DataSource = dt; DataList1.DataBind(); } } private DataTable Category_tbl() { DataTable dt = new DataTable(); dt.Columns.AddRange(new DataColumn[3] { new DataColumn(&#34;Id&#34;, typeof(int)), new DataColumn(&#34;Category&#34;, typeof(string)), new DataColumn(&#34;Sub_category&#34;,typeof(string)) }); dt.Rows.Add(1, &#34;Doctors&#34;, &#34;Dentists&#34;); dt.Rows.Add(2, &#34;Doctors&#34;, &#34;Cardiologists&#34;); dt.Rows.Add(3, &#34;Daily Needs&#34;, &#34;Grocery&#34;); dt.Rows.Add(4, &#34;Daily Needs&#34;, &#34;Chemists&#34;); dt.Rows.Add(5, &#34;Repairs&#34;, &#34;AC&#34;); dt.Rows.Add(6, &#34;Repairs&#34;, &#34;Laptop&#34;); dt.Rows.Add(7, &#34;On Demand Service&#34;, &#34;Carpenters&#34;); dt.Rows.Add(8, &#34;On Demand Service&#34;, &#34;Plumber&#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 category = dataRowView[&#34;Category&#34;].ToString(); DataList subDataList = e.Item.FindControl(&#34;DataList2&#34;) as DataList; DataTable dt = Category_tbl().Select(&#34;Category=&#39;&#34; + category + &#34;&#39;&#34;).CopyToDataTable(); subDataList.DataSource = dt; subDataList.DataBind(); } } protected void DataList2_ItemCommand(object source, DataListCommandEventArgs e) { if (e.CommandName == &#34;viewdetails&#34;) { Response.Redirect(&#34;demo_datalist.aspx?Id=&#34; + e.CommandArgument.ToString()); } }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) If Not Me.IsPostBack Then Dim dt As DataTable = Category_tbl().DefaultView.ToTable(True, &#34;Category&#34;) DataList1.DataSource = dt DataList1.DataBind() End If End Sub Private Function Category_tbl() As DataTable Dim dt As DataTable = New DataTable() dt.Columns.AddRange(New DataColumn(2) { New DataColumn(&#34;Id&#34;, GetType(Integer)), New DataColumn(&#34;Category&#34;, GetType(String)), New DataColumn(&#34;Sub_category&#34;, GetType(String))}) dt.Rows.Add(1, &#34;Doctors&#34;, &#34;Dentists&#34;) dt.Rows.Add(2, &#34;Doctors&#34;, &#34;Cardiologists&#34;) dt.Rows.Add(3, &#34;Daily Needs&#34;, &#34;Grocery&#34;) dt.Rows.Add(4, &#34;Daily Needs&#34;, &#34;Chemists&#34;) dt.Rows.Add(5, &#34;Repairs&#34;, &#34;AC&#34;) dt.Rows.Add(6, &#34;Repairs&#34;, &#34;Laptop&#34;) dt.Rows.Add(7, &#34;On Demand Service&#34;, &#34;Carpenters&#34;) dt.Rows.Add(8, &#34;On Demand Service&#34;, &#34;Plumber&#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 category As String = dataRowView(&#34;Category&#34;).ToString() Dim subDataList As DataList = TryCast(e.Item.FindControl(&#34;DataList2&#34;), DataList) Dim dt As DataTable = Category_tbl().Select(&#34;Category=&#39;&#34; &amp; category &amp; &#34;&#39;&#34;).CopyToDataTable() subDataList.DataSource = dt subDataList.DataBind() End If End Sub Protected Sub DataList2_ItemCommand(ByVal source As Object, ByVal e As DataListCommandEventArgs) If e.CommandName = &#34;viewdetails&#34; Then Response.Redirect(&#34;demo_datalist.aspx?Id=&#34; &amp; e.CommandArgument.ToString()) End If End Sub</pre> <p>Note: I have used DataTable to populate and filter the record. You need to replace with ADO.Net code.</p>https://www.aspforums.net:443/Threads/196759/Get-Command-Argument-value-on-child-DataList-Button-Click-using-C-and-VBNet-in-ASPNet/https://www.aspforums.net:443/Threads/196759/Get-Command-Argument-value-on-child-DataList-Button-Click-using-C-and-VBNet-in-ASPNet/Fri, 06 Sep 2019 03:27:03 GMT