简体   繁体   中英

Retrieve ASP.NET Eval on ImageButton Click

I have the following repeater, with some div containing information and an ImageButton . On click of the ImageButton I would like to retrieve the DataBinder.Eval which contains the "ID"

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

On ImageButton click I would like the retrieve the id of the item on which the image button was clicked and send it to another method, as below.

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

However since it is an image button RepeaterItemEventArgs can only be ImageClickEventArgs since its an image button and thus I don't know how I can retrieve the id exactly.

Add hidden field with id to Repeater:

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

Then in event handler:

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

You can also add id in CommandArgument as

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

And in click event handler

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM