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:

>

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(); } }

comments powered by Disqus