AlexG
AlexG

Reputation: 53

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 = ???

Upvotes: 3

Views: 3392

Answers (2)

FuDm
FuDm

Reputation: 49

Object DataItem = (Object) e.Item.DataItem;

And then use the DataItem.

Upvotes: 0

VDWWD
VDWWD

Reputation: 35514

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.

Upvotes: 2

Related Questions