I have a Datalist control which populates a list of categories as a linkbuttons. Clicking specific category I would like to populate associated subcategories.
But, not sure how to fire click event of linkbutton inside datalist control.
I tried to use CommandArgument and CommandName properties, but event is still not firing.
Below is the sample code..
<asp:DataList ID="DataList1" runat="server" DataKeyField="CatID">
<ItemTemplate>
<li>
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="ShowSubCats" CommandArgument='<%# Eval("CatID") %>' OnClick="populateSubCats"><%# Eval("Category") %> </asp:LinkButton>
</li>
</ItemTemplate>
</asp:DataList>
//CODE BEHIND CS FILE CODE
protected void populateSubCats(object sender, EventArgs e)
{
}
----------------------------------------------- UPDATE -----------------------------------------
------------------------------------------------------------------------------------------------
Hi I resolved problem myself, but having another problem, code behind not able find CHILD Datalist control(for subcategories) contained into PARENT datalist control
I'am getting ERROR: <b>The name 'DataList2' does not exist in the current context</b>
Below is the SAMPLE CODE
// ----------------- subMenus.aspx ----------------------
<ul>
<asp:datalist id="DataList1" runat="server" datakeyfield="CatID" xmlns:asp="#unknown">
<itemtemplate>
<li>
<asp:linkbutton id="LinkButton1" runat="server" commandname="Display" oncommand="ImageButton_Command" commandargument="<%# Eval("CatID") %>"><![CDATA[<%# Eval("Category") %>]]></asp:linkbutton>
</li>
<ul>
<asp:datalist id="DataList2" runat="server">
<itemtemplate>
<li>
<asp:linkbutton id="LinkButton2" runat="server" commandname="Display"><![CDATA[<%# Eval("SubCategory") %>]]></asp:linkbutton>
</li>
</itemtemplate>
</asp:datalist>
</ul>
</itemtemplate>
</asp:datalist>
</ul>
//-------------- subMenus.aspx.cs ------------------------
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
populateCatefories();
}
}
protected void populateCatefories()
{
SqlConnection conn = new SqlConnection(clsConnection.connectionString());
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "GetAllCategories";
cmd.Connection = conn;
try
{
conn.Open();
DataList1.DataSource = cmd.ExecuteReader();
DataList1.DataKeyField = "CatID";
DataList1.DataBind();
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
finally
{
conn.Close();
cmd.Dispose();
}
}
protected void ImageButton_Command(object sender, CommandEventArgs e)
{
if (e.CommandName == "Display")
{
getSubCategories(Convert.ToInt32(e.CommandArgument));
}
}
protected void getSubCategories(int subCatID)
{
SqlConnection conn = new SqlConnection(clsConnection.connectionString());
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "getsubCategories";
cmd.Parameters.AddWithValue("@CatID", subCatID);
cmd.Connection = conn;
try
{
conn.Open();
DataList2.DataSource = cmd.ExecuteReader();
DataList2.DataKeyField = "SubCatID";
DataList2.DataBind();
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
finally
{
conn.Close();
cmd.Dispose();
}
}
----------------------------------------- FURTHER UPDATE ---------------------------------------
------------------------------------------------------------------------------------------------
I have added below code to get ID of child datalist control, but it is getting errror: Object reference not set to an instance of an object.
DataList childDataList = (DataList)DataList1.FindControl("DataList2");
On some another site I found below code, but My code not able to recognize item element.
DataList dlSubChild = (DataList)e.Item.FindControl("dlSubChild")