Implementing a functional link in a repeater

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

Thanks!

+3
source share
3 answers

I guess 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

protected void lbtn_Command(object sender, CommandEventArgs e)
{
    int id = Convert.ToInt32(e.CommandArgument);
}
+8
source

LinkButtons. , OnClick .

0

First you set the onclick link buttons in the markup. Then you 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 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"]);
    }
}
-1
source

Source: https://habr.com/ru/post/1706736/


All Articles