Click here to Skip to main content
15,886,258 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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..


C#
 <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)
    {
        // Code for populating Sub Categories GOES HERE...!
    }



----------------------------------------------- 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="&lt;%# Eval("CatID") %&gt;"><![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.
C#
DataList childDataList = (DataList)DataList1.FindControl("DataList2");


On some another site I found below code, but My code not able to recognize item element.

C#
DataList dlSubChild = (DataList)e.Item.FindControl("dlSubChild")
Posted
Updated 14-Aug-13 23:33pm
v5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900