0

When I click on one of the imagebuttons in the datalist the onclick will fire, but how do I know which button has been clicked?

<asp:DataList ID="dtlProducten" runat="server" DataSourceID="dtsProducten" RepeatColumns="3" Width="274px">
    <ItemTemplate>
        <asp:Label ID="NaamLabel" runat="server" Text='<%# Eval("Naam") %>' />
        : €<asp:Label ID="PrijsLabel" runat="server" Text='<%# Eval("Prijs", "{0:N}") %>' />
        <br />
        <asp:ImageButton ID="ibProduct" runat="server" BorderColor="#B5CC20" BorderStyle="Solid" Height="100px" ImageUrl='<%# Eval("Foto", "~/Pictures/{0}") %>' Width="100px" Onclick="ImageButtonProduct_Click"/>


Ted
  • 2,525
  • 2
  • 37
  • 54
  • What are the properties of the objects supplied in the dtsProducten datasource. If one of the properties has an unique value like an id for that item, you could use it as a reference to that object. – M.B. Mar 15 '19 at 12:37
  • Yes, all the objects in dtsProducten have a unique ProductID – Pieter Pauwels Mar 15 '19 at 12:38
  • But how do I get it out is the question – Pieter Pauwels Mar 15 '19 at 12:39
  • You could look into the following link [What is the use of “object sender” and “EventArgs e” parameters?](https://stackoverflow.com/questions/14479143/what-is-the-use-of-object-sender-and-eventargs-e-parameters). This should clarify what is needed to get a reference to the clicked image. – M.B. Mar 15 '19 at 12:47

1 Answers1

1

I think this answer could be the basis of an answer.

I've started adapting it to something that will hopefully lead you to a working solution.

protected void ImageButtonProduct_Click(object sender, EventArgs e)
{
    ImageButton btn = (ImageButton) sender;
    DataListItem item = (DataListItem) btn.NamingContainer;
    Label lbl = (Label) item.FindControl("PrijsLabel");
    //... do other stuff here
}
Ted
  • 2,525
  • 2
  • 37
  • 54