如何在asp.net转发器控件中获取项目点击事件?

时间:2022-08-29 03:35:51

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.

我希望当我点击图像时,我得到一个包含我的数据项的id字段的事件。

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.

我想在单击图像时获取PhotoID。

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.

我一直在做刚刚启动web的winform编程,也许这很容易,但我很难找到解决方案。

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

我以某种方式设法在我悬停图像时显示手形光标。

3 个解决方案

#1


13  

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

#2


4  

You can use ItemCommand of repeater control like this -

你可以像这样使用转发器控制的ItemCommand -

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


0  

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:

就个人而言,我认为处理这类事情的最简单方法是简单地使用ItemTemplate来生成常规的html链接,而不是在代码隐藏中做任何事情。像这样的东西:

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

我知道这不是你提出的问题,但恕我直言,这是完成任务的最佳方法。

#1


13  

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

#2


4  

You can use ItemCommand of repeater control like this -

你可以像这样使用转发器控制的ItemCommand -

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


0  

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:

就个人而言,我认为处理这类事情的最简单方法是简单地使用ItemTemplate来生成常规的html链接,而不是在代码隐藏中做任何事情。像这样的东西:

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

我知道这不是你提出的问题,但恕我直言,这是完成任务的最佳方法。