簡體   English   中英

用戶單擊行時如何獲取轉發器的項目行ID

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

我有一個與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>

當用戶單擊鏈接按鈕時,我想獲取該轉發器項目行的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);
        }
    }

現在運行並測試您的應用程序!

希望這可以幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM