c# - 用户单击行链接按钮控件时如何获取中继器的项目行ID

标签 c# asp.net repeater findcontrol

我有一个中继器,嵌套了 Html 表格行:

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

当用户点击 linkbutton 时,我想获取该行转发器项目的 ID,并在另一个 aspx 页面中显示与该 ID 相关的 PDF 文件。

我在 itemdatabound 事件上写道:

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

        }

如何获取嵌套在 html 表格中的转发器的列 ID?

最佳答案

我认为您没有按照正确的方法进行操作。我会选择 Repeater 的 ItemCommand Event 的好处来做这件事。

以下代码可能对您有帮助:

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>

如您所见,我在标签中使用了 CommandName 和 CommandArgument 属性。我使用 CommandName 属性来识别用户期望的操作。还有 CommandArgument 将所需的数据对象传递给服务器。

转到 Repeater 的 OnItemCommand 而不是 ItemDataBound 事件。并编写如下代码;

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

现在运行并测试您的应用程序!

希望这对您有所帮助!

关于c# - 用户单击行链接按钮控件时如何获取中继器的项目行ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25099087/

相关文章:

c# - 检查手机上的互联网连接

c# - 通用类型层次结构的流畅接口(interface)

c# - Blazor 将数据绑定(bind)到集合?

asp.net - 来自经典 ASP 或 ASP.NET 的 Microsoft Search Server 2008 Express Edition

c# - 如何从后面的代码访问父转发器 ID

c# - 捕获 jquery ajax 错误回调中的错误?

javascript - 使用 Javascript 在导航栏中显示当前选项卡

c# - 如何获取中继器内复选框的 ID

c# - 展示装置中继器

c# - 从通用类型继承为接口(interface)