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?
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?
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());
}
I have also found that using a hidden field on the row is helpful as well.
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).