Hi itsme,
Your Button is inside the child DataList. So you have to add OnItemCommand in child DataList.
Check this example. Now please take its reference and correct your code.
HTML
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<div class="container" style="margin-left: 30%; width: 2000px; height: 1000px">
<div class="row">
<div class="col-sm-4">
<div class="shadow-lg p-3 mb-5 bg-white rounded panel panel-blue">
<div class="panel-heading">
<center>
<b>Search By Category</b></center>
</div>
<div class="panel-body">
<asp:DataList ID="DataList1" runat="server" RepeatColumns="2" RepeatDirection="Horizontal"
OnItemDataBound="DataList1_ItemDataBound">
<ItemTemplate>
<table class="auto-style1" border="1" style="margin-bottom: 40px">
<tr>
<tr>
<td class="auto-style2">
<asp:Label ID="category" runat="server" Font-Bold="True" Text='<%# Eval("Category") %>'
CssClass="btn-lg"></asp:Label><br />
<br />
</td>
</tr>
<tr>
<td class="auto-style2">
<asp:DataList ID="DataList2" runat="server" OnItemCommand="DataList2_ItemCommand">
<ItemTemplate>
<table class="auto-style1" border="1">
<asp:Panel runat="server" ID="pnlid" Visible="false">
<tr>
<td class="auto-style2">
<asp:Label ID="id" runat="server" Font-Bold="True" Text='<%# Eval("Id") %>' Visible="false"></asp:Label>
</td>
</tr>
</asp:Panel>
<tr>
<td class="auto-style2">
<asp:Button ID="Bname" runat="server" Text='<%# Eval("Sub_category") %>' CommandArgument='<%# Eval("Sub_category") %>'
CommandName="viewdetails" CssClass="btn auto-style2" BackColor="#0099cc" Font-Bold="true"
Font-Size="Large" ForeColor="White" />
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
</td>
</tr>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
</div>
</div>
</div>
</div>
</div>
</asp:Content>
Namespaces
C#
using System.Data;
VB.Net
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = Category_tbl().DefaultView.ToTable(true, "Category");
DataList1.DataSource = dt;
DataList1.DataBind();
}
}
private DataTable Category_tbl()
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),
new DataColumn("Category", typeof(string)),
new DataColumn("Sub_category",typeof(string)) });
dt.Rows.Add(1, "Doctors", "Dentists");
dt.Rows.Add(2, "Doctors", "Cardiologists");
dt.Rows.Add(3, "Daily Needs", "Grocery");
dt.Rows.Add(4, "Daily Needs", "Chemists");
dt.Rows.Add(5, "Repairs", "AC");
dt.Rows.Add(6, "Repairs", "Laptop");
dt.Rows.Add(7, "On Demand Service", "Carpenters");
dt.Rows.Add(8, "On Demand Service", "Plumber");
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["Category"].ToString();
DataList subDataList = e.Item.FindControl("DataList2") as DataList;
DataTable dt = Category_tbl().Select("Category='" + category + "'").CopyToDataTable();
subDataList.DataSource = dt;
subDataList.DataBind();
}
}
protected void DataList2_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "viewdetails")
{
Response.Redirect("demo_datalist.aspx?Id=" + e.CommandArgument.ToString());
}
}
VB.Net
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, "Category")
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("Id", GetType(Integer)),
New DataColumn("Category", GetType(String)),
New DataColumn("Sub_category", GetType(String))})
dt.Rows.Add(1, "Doctors", "Dentists")
dt.Rows.Add(2, "Doctors", "Cardiologists")
dt.Rows.Add(3, "Daily Needs", "Grocery")
dt.Rows.Add(4, "Daily Needs", "Chemists")
dt.Rows.Add(5, "Repairs", "AC")
dt.Rows.Add(6, "Repairs", "Laptop")
dt.Rows.Add(7, "On Demand Service", "Carpenters")
dt.Rows.Add(8, "On Demand Service", "Plumber")
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("Category").ToString()
Dim subDataList As DataList = TryCast(e.Item.FindControl("DataList2"), DataList)
Dim dt As DataTable = Category_tbl().Select("Category='" & category & "'").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 = "viewdetails" Then
Response.Redirect("demo_datalist.aspx?Id=" & e.CommandArgument.ToString())
End If
End Sub
Note: I have used DataTable to populate and filter the record. You need to replace with ADO.Net code.