检索ASP.. NET Eval on ImageButton单击

本文关键字:ImageButton 单击 on Eval ASP NET 检索 | 更新日期: 2023-09-27 17:54:15

我有以下中继器,其中一些div包含信息和ImageButton。点击ImageButton,我想检索包含"ID"DataBinder.Eval

 <asp:Repeater ID="RptPendingRequests" runat="server" onitemcommand ="RptPendingRequests_ItemCommand">
    //other divs here
    <div class="LabelCustomShort" style="font-size:10px"><%#DataBinder.Eval(Container.DataItem, "ID")%></div>
    <asp:ImageButton runat="server" ID="btnPrint" ImageUrl="~/Images/print.gif" onclick="btnPrint_Click" /></div>
    //other divs here
    <asp:Button runat="server" ID="btnSubmit"  Text="Submit" style="float:right;" onclick="btnSubmit_Click" />
    </asp:Repeater>

在ImageButton上单击,我想检索图像按钮被单击的项目的id,并将其发送给另一个方法,如下所示。

  protected void btnPrint_Click(object sender, RepeaterItemEventArgs e)
    {
        var id = e.Item.FindControl("Id") as Label;
        DefaultClass.createPDF(Convert.ToInt32(id));
    }

然而,由于它是一个图像按钮RepeaterItemEventArgs只能是ImageClickEventArgs,因为它是一个图像按钮,因此我不知道如何准确地检索id

检索ASP.. NET Eval on ImageButton单击

添加id为Repeater的隐藏字段:

<asp:HiddenField ID="hfId" runat="server" Value='<%# Eval("ID") %>' />

然后在事件处理程序中:

var id = ((Control)sender).Parent.FindControl("hfId") as HiddenField;
DefaultClass.createPDF(Convert.ToInt32(id.Value));

您也可以在CommandArgument中添加id

<asp:ImageButton runat="server" ID="btnPrint" ImageUrl="~/Images/print.gif" OnClick="btnPrint_Click" CommandArgument = '<%# Eval("ID") %>'/>

点击事件处理程序

protected void btnPrint_Click(object sender, EventArgs e)
{
   string id = (sender as ImageButton).CommandArgument;
}