Search code examples
c#asp.netrepeaterfindcontrol

How to get items row id of repeater when user clicks rows linkbutton control


I have a repeater, nested with Html table rows:

<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
               <HeaderTemplate>
                   <table id="grid">
                       <thead>
                           <tr>
                               <th>SID</th>
                               <th>Start Date</th>
                               <th>End Date</th>..
                               <th>PDF Text</th>
                           </tr>
                       </thead>
                       <tbody>
          </HeaderTemplate>
          <ItemTemplate>
              <tr>
                  <td><%#DataBinder.Eval(Container.DataItem,"ID") %></td>
                  <td><%#DataBinder.Eval(Container.DataItem,"startDate") %></td>
                  <td><%#DataBinder.Eval(Container.DataItem,"endDate") %></td
                  <td>
                      <asp:LinkButton runat="server" ID="lbtnPDF" Text="PDF Full Text" OnClick="lbtnPDF_Click" />
                  </td>
              </tr>
          </ItemTemplate>
               <FooterTemplate>
                   </tbody>
                   </table>
               </FooterTemplate>
           </asp:Repeater>

When the user clicks on linkbutton, I want to get the ID of that row of repeater item and will display the PDF file which is related to that ID, in another aspx page.

I wrote on itemdatabound event :

protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            LinkButton lnkBtn = (LinkButton)e.Item.FindControl("lbtnPDF");

        }

How can I get column Id of repeater nested with an html table?


Solution

  • I don't think you are following the right approach to do it. I would choose the benefits of Repeater's ItemCommand Event to do it.

    The following code might be helpful to you:

    HTML/ASP.net

       <asp:Repeater ID="Repeater1" DataSourceID="SqlDataSource1" runat="server" OnItemCommand="Repeater1_ItemCommand">
        <ItemTemplate>
            <asp:Button ID="Button1" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"Username") %>' CommandName="ViewPDF" CommandArgument='<%#DataBinder.Eval(Container.DataItem,"ID") %>' />
        </ItemTemplate>
    </asp:Repeater>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString='<%$ ConnectionStrings:myConnString %>' SelectCommand="SELECT * FROM [tblUserAccounts]"></asp:SqlDataSource>
    

    As you have noticed, I have used CommandName and CommandArgument attributes in the label. I have used CommandName attribute to identify which action the user is expecting. Also the CommandArgument to pass the required data object to server.

    Go to Repeater's OnItemCommand instead of ItemDataBound event. And write the code as below;

     protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "ViewPDF")
            {
                Response.Redirect("~/MyPDFFiles/" + (string)e.CommandArgument);
            }
        }
    

    Now run and test your Application!

    Hope this helps!