Tuesday, December 12, 2006

Using Repeater ItemCommand

This is very similar to Using DataGrid 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();
    }
}

2 comments:

Evan said...

i hope this means you will be adding comments to your other blog ;)

nrps said...

Thanks Mate !!!
Keep up the good work !!!