Using Repeater ItemCommand

This is very similar to Using GridView RowCommand but with some slight changes.

First create a repeater control and bind some data to it which ever way you like. Inside your ItemTemplate (or any other template associated with the repeater control) create a link button control (or button control, whatever you want). Set the CommandName to a descriptive name. Set the CommandArgument to your unique identifier. Mine looks like this:

<asp:LinkButton ID="btnDeleteComment" runat="server" Text="Delete" CommandName="DeleteComment" CommandArgument=<%#Eval("CommentID") %>></asp:LinkButton>

I am setting the CommandArgument to the CommentID which is databound to my repeater. Now in design view, double click on your repeater control and Visual Studio will create a method for handling the ItemCommand event. Here we will test if the CommandName is the one we are looking for and then do some action with the CommandArgument. (In my case delete a comment). This is what mine looks like:

protected void rptComments_ItemCommand(object source, RepeaterCommandEventArgs e) { if(e.CommandName.ToLower().Equals("deletecomment")) { clsComment comment = new clsComment("mediadb"); comment.CommentID = int.Parse(((LinkButton)e.CommandSource).CommandArgument); comment.DeleteRecord(); rptComments.DataBind(); } }


Rush Frisby

Rush is a software architect who loves solving problems. You'll usually find him writing about security concepts and sharing solutions to problems he's faced.