开发者

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

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.


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


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 

           }
       }
 }


Personally I think the simplest way to handle something like this is to simply use the ItemTemplate to generate a regular html link, rather than do any thing in the code-behind. Something like this:

   <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 this isn't strictly the question you asked, but IMHO it's the best approach to accomplish the task.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜