views:

244

answers:

3

I have a repeater that lists Users.

Each row has the userId, username, etc, and a button in the last column.

When the button is clicked, how can I get the current row's user ID?

+2  A: 

You could add an OnItemCommand event, and bind the UserId to the CommandArgument of a button.

<asp:Repeater ID="repeater1" runat="server" OnItemCommand="repeater1_ItemCommand">
    <ItemTemplate>
        <asp:Button ID="button1" runat="server" CommandArgument='<%#Eval("UserId")%>' />
    </ItemTemplate>
</asp:Repeater>

Code Behind:

protected void repeater1_ItemCommand(object sender, RepeaterCommandEventArgs e)
{
    Guid userId = new Guid(e.CommandArgument.ToString());
}
sshow
But I need to get the UserId during the click event?
mrblah
I'm sorry, I don't understand?
sshow
what you are looking for is the best solution, when you click the button, it will fire repeater itemcommand event, there you can find the userID. Simply what you have written your in the click event, put it in repeate_ItemCommand Event.
Muhammad Akhtar
+1  A: 

I have also found that using a hidden field on the row is helpful as well.

Anthony Shaw
A: 

Both earlier answers are good.

Use Anthony's method (a hidden field) if you're interested in doing "plain old" HTML postback (instead of going through ASP.NET's postback mechanisms).

Otherwise, use sshow's. It's a little more "idiomatic" to the .NET framework (at least through version 2.0).

harpo