How to get Full row from repeater control in code behind?

13,061

Here is the code for the repeater

<asp:Repeater ID="Repeater1" runat="server" 
        onitemcommand="Repeater1_ItemCommand">
        <HeaderTemplate>
            <ul>
        </HeaderTemplate>
        <ItemTemplate>
        <li>
            <asp:LinkButton ID="btnDeleteComment" runat="server" Text="Delete" CommandName="DeleteComment" CommandArgument=<%#Eval("myId") %>></asp:LinkButton>
            <asp:Label ID="Label1" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "FileName")%>'></asp:Label>
            </li>
        </ItemTemplate>
        <FooterTemplate>
        </ul>
        </FooterTemplate>
    </asp:Repeater>

Here is the code for the code behind

public partial class _Default : System.Web.UI.Page
    {
        public class myObject
        {
            public string FileName { get; set; }
            public int myId { get; set; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            List<myObject> myList = new List<myObject>();
            myList.Add(new myObject {myId = 1, FileName = "one" });
            myList.Add(new myObject { myId = 2, FileName = "two" });
            myList.Add(new myObject { myId = 3, FileName = "three" });

            Repeater1.DataSource = myList;
            Repeater1.DataBind();
        }

        protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            Label item = (Label)e.Item.FindControl("label1");

        }
    }
Share:
13,061
Zeus
Author by

Zeus

Updated on June 08, 2022

Comments

  • Zeus
    Zeus almost 2 years

    I am using repeater control to create a table with few rows and columns. On Repeater_ItemCommand I want to select clicked row in the code behind and store it in the session. How do I do this ?

    When I click the row, my e.Item.DataItem is coming NULL. I am using <%# DataBinder.Eval(Container.DataItem, "FILE_NAME")%> to bind my values in asp.net

    I cant use LINQ.

    Thanks Ved

  • Zeus
    Zeus over 11 years
    I would love to but its out of my hand..I have to use repeater
  • Zeus
    Zeus over 11 years
    I want to select the entire row, I am not using server controls. Displaying text inside TD.
  • Mateusz Antkiewicz
    Mateusz Antkiewicz over 11 years
    as far as i know Repeater.ItemCommand event Occurs when a button is clicked in the Repeater control. You can have a LinkButton with CSS style display:block inside each <td> with command argument and Text property.
  • Shai Cohen
    Shai Cohen over 11 years
    @Ivan: Why do you say repeaters "not supposed to be used that way"?
  • Ivan Koshelev
    Ivan Koshelev over 11 years
    @Shai Cohen: From what i've read, its used for data rendering but not data manipulation. MSDN seems to agree : "The Repeater control has no built-in selection capabilities or editing support".(msdn.microsoft.com/en-us/library/…)
  • Shai Cohen
    Shai Cohen over 11 years
    @IvanKoshelew: From my experience, all the other "rich" controls just add extended functionality to the basic repeater (ie: paging, edit rows, sorting, etc.). BUT, you still need to implement these manually. Usually, any control that "does it all for you" also does too much for you, costing you overhead for features you are not necessarily using. I am not saying these controls are not useful, but their usefulness comes at a price. If you are doing RAD, I can see why you want to roll it out as fast as possible, but if you are looking for long-term efficient, use the repeater. IMHO.
  • Shai Cohen
    Shai Cohen over 11 years
    @IvanKoshelew: This has become a discussion, so if you would like to discuss some more, let's join a chat.
  • SarangK
    SarangK about 7 years
    Label item = (Label)e.Item.FindControl("label1"); worked for me. It saved my lot of time. @atbebtg Thanks for answer. :)