views:

650

answers:

3

I am implementing a Repeater in my web application to display data. I want to add functional action links in a column similar to the built-in functionality in a GridView. Can anybody give me the steps required? I assume I will add a LinkButton control to each row, somehow set the OnClick event handler to point to the same method, and somehow pass in the unique identifier on the row as a parameter.

Thanks!

A: 

Use LinkButtons. That way, you can handle the OnClick event in the code behind.

theG
Yes, that's what I meant. Thanks for pointing that out. How would I go about setting up the event handler from the repeater and passing in the unique ID has an argument?
Mike C.
A: 

First you would set the onclick of the linkbutton in markup. You'll then want to implement the ItemDataBound event for the repeater.

  if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
         SomeObject obj = e.Item.DataItem as SomeObject; // w/e type of item you are bound to
         var linkButton = e.Item.FindControl("linkButtonId") as LinkButton;
         if(linkButton != null)
         {
              //either set a custom attribute or maybe append it on to the linkButton's ID
              linkButton.Attributes["someUniqueId"] = obj.SomeID;
         }
    }

Then in the click event

void lb_Click(object sender, EventArgs e)
{
    LinkButton lb = sender as LinkButton;
    if (lb != null)
    {
        // obviously do some checking to ensure the attribute isn't null
        // and make it the correct datatype.
        DoSomething(lb.Attributes["someUniqueId"]);
    }
}
Jab
+3  A: 

I'm guessing this is what you want.

    <asp:Repeater ID="rpt" runat="server">
        <ItemTemplate>
            <asp:LinkButton ID="lbtn" runat="server" OnCommand="lbtn_Command" 
            CommandArgument='<%# DataBinder.Eval(Container.DataItem, "KeyIDColumn") %>' ></asp:LinkButton>
        </ItemTemplate>
    </asp:Repeater>

Then in your code behind

protected void lbtn_Command(object sender, CommandEventArgs e)
{
    int id = Convert.ToInt32(e.CommandArgument);
}
Eoin Campbell
*GREAT* answer. Exactly what I wanted. Thank you! I've never noticed the OnCommand and CommandArgument property on the LinkButton control.
Mike C.
Yeah it's very handy especially with DataBound Content...Equally if you needed mutliple linkButtons for different commands (e.g. VoteUp / VoteDown / MarkAsSpam) then you can specify different CommandNames for each control and switch/case on the CommandName in your code behind
Eoin Campbell
Awesome. Is there a way to handle multiple values in the CommandArgument property other than using a delimited string?
Mike C.
Ah, much cleaner than using the databound method I posted. I had totally forgot about the command arguments. +1
Jab
@Mike C. Well you can store inject two seperate values. One in CommandArgument, one in CommandName, so if its going to be the exact same command each time, you could use the CommandName as a second parameter, but outside of that, you'd need to use a Delimited String for storing values
Eoin Campbell