How to get click element event in asp.net repeater control?

I am using a repeater control to display some data on my page.

The repeater element template has an image and a label field.

I want that when I click on the image, I get an event containing the id field of my data element.

How can i achieve this?

In fact, when I click on the image, I want to go to another page and want to show detailed information about my data element, in the repeater I will just show short information.

My repeater is as follows:

<asp:Repeater ID="itemRepeater" runat="server" OnItemCreated="itemRepeater_ItemCreated" > <ItemTemplate> <tr> <td colspan="2"> <asp:Image ID="phImage" runat="server" ImageUrl='<%#"~/ImageHandler.ashx?id=" + DataBinder.Eval(Container.DataItem, "PhotoID")%>' /> </td> <td> <asp:Label ID="lblImageName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Name") %>' /> </td> </tr> </ItemTemplate> </asp:Repeater> 

I want to get a PhotoID when I click an image.

My photography class is as follows:

  public class PhotoDC { public byte[] ImagebyteArray { get; set; } public string Name { get; set; } public int PhotoID { get; set; } } 

I am involved in winform programming, just running the web, it may be easy, but I'm trying to find a solution.

I somehow managed to show the cursor when I find the image.

+6
source share
3 answers

Try the following:

 <asp:Repeater ID="itemRepeater" runat="server" OnItemCreated="itemRepeater_ItemCreated" > <ItemTemplate> <tr> <td colspan="2"> <asp:ImageButton ID="phImage" runat="server" ImageUrl='<%#"~/ImageHandler.ashx?id=" + DataBinder.Eval(Container.DataItem, "PhotoID")%>' OnCommand="Image_Click" CommandName="ImageClick" CommandArgument='<%# Eval("PhotoID") %>' /> </td> <td> <asp:Label ID="lblImageName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Name") %>' /> </td> </tr> </ItemTemplate> </asp:Repeater> protected void Image_Click(object sender, CommandEventArgs e) { if (e.CommandName == "ImageClick"){ //e.CommandArgument --> photoid value //Do something } } 
+11
source

You can use ItemCommand to control the repeater like this -

 protected void itemRepeater_ItemCommand(object source, RepeaterCommandEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { if (e.CommandName == "img_Click") // check command is cmd_delete { // get you required value int CustomerID = Convert.ToInt32(e.CommandArgument); //Write some code for what you need } } } 
+3
source

Personally, I believe that the easiest way to deal with something like this is to simply use the ItemTemplate to create a regular html link, rather than doing something in the code. Something like that:

  <asp:Repeater ID="itemRepeater" runat="server" OnItemCreated="itemRepeater_ItemCreated" > <ItemTemplate> <tr> <td colspan="2"> <a href="/Details.aspx?id=<%=DataBinder.Eval(Container.DataItem, "PhotoID")%>"> <asp:Image ID="phImage" runat="server" ImageUrl='<%#"~/ImageHandler.ashx?id=" + DataBinder.Eval(Container.DataItem, "PhotoID")%>' /> </a> </td> <td> <asp:Label ID="lblImageName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Name") %>' /> </td> </tr> </ItemTemplate> 

I know that this is not strictly the question that you asked, but IMHO this is the best approach to the task.

0
source

Source: https://habr.com/ru/post/894136/


All Articles