Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

asp.net

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

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

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

How can I achieve this ?

Actually when I click the image i want to go to another page and want to show detailed information of my data item, in repeater i m just showing short information.

My repeater looks like this:

   <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 PhotoID in the event when i click the image.

My photo class looks like this:

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

I have been doing winform programming just started web, maybe it is easy but i m struggling to find a solution.

I somehow managed to show hand cursor when i hover the image though.

like image 790
Embedd_0913 Avatar asked Aug 02 '11 19:08

Embedd_0913


People also ask

How to Find control in repeater on Button click in asp net c#?

When the Button is clicked, first the Repeater Item is referenced using the NamingContainer property. Then the Label and the TextBox controls are referenced from the Repeater Item using the FindControl method. Finally the values of Label and TextBox controls are displayed using JavaScript Alert Message Box.

What is ItemDataBound in repeater?

ItemDataBound: Execute just before it is rendered on the page. ItemCommand: Execute when button kind control of repeater clicked.

What is repeater in ASP NET?

The Repeater control is used to display a repeated list of items that are bound to the control. The Repeater control may be bound to a database table, an XML file, or another list of items. Repeater is a Data Bind Control. Data Bind Controls are container controls.


2 Answers

Try this:

<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
    }
}
like image 174
Mrchief Avatar answered Oct 08 '22 22:10

Mrchief


You can use ItemCommand of repeater control 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 

           }
       }
 }
like image 38
Amit Kumawat Avatar answered Oct 08 '22 23:10

Amit Kumawat