AlexG AlexG - 3 months ago 25
C# Question

How to get Repeater Item data object on ItemCommand event

I have a repeater of LinkButtons and on the ItemCommand event. I need to get the Data Object that created the link button. My DataSource is List so on ItemCommand i need MyObject object = ???


Are you looking for this? This will send the ID to the code behind using CommandArgument so it can be processed.

<asp:LinkButton ID="LinkButton1" CommandArgument='<%# Eval("ID") %>' runat="server" CommandName="myCommand">LinkButton</asp:LinkButton>

And in code behind:

protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
    if (e.CommandName == "myCommand")
        string myID = e.CommandArgument.ToString();

Or you can use CommandArgument='<%# Container.ItemIndex %>'. Then you know the row number and can access the corresponding index in your source.