1

My scenario is: I have DataList with some products. My SqlDataSource takes all values but I display only picture, name and price. ProductID label visible in item template is set to false.

I have added an image button to each product and I would like to click this button and go to more specific details page of each product. My solution is to pass ProductID to details page. But, I am missing something and can't figure out how to do that.

My button:

<asp:ImageButton ID="ImageButton1" runat="server" CommandArgument='<%# Eval("productID")%>'
                                ImageUrl="~/Graphics/profile_button.png" onclick="ImageButton1_Click" />

Then onclick:

protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {

            string productID = ???????
            string url;

            url = "~/ProductDetails.aspx?productid=" + ProductID;
            Response.Redirect(url);
        }

As you can see I dont know how to get CommandArgument. How to do that?

dargod
  • 334
  • 1
  • 15

2 Answers2

1

I think I got it. Use

OnCommand="ImageButton1_Click"

(instead of onclick) in the markup of the image button and

protected void ImageButton1_Click(object sender, CommandEventArgs e)
{
     string productID = e.CommandArgument.ToString(); 
     //..
}
AGuyCalledGerald
  • 7,882
  • 17
  • 73
  • 120
  • Tried, just after posting but: Error 1 'System.Web.UI.ImageClickEventArgs' does not contain a definition for 'CommandArgument' and no extension method 'CommandArgument' accepting a first argument of type 'System.Web.UI.ImageClickEventArgs' could be found (are you missing a using directive or an assembly reference?) – dargod Mar 07 '12 at 13:42
  • Aaa yes :) Working well, thanks! I was searching also and found some topics with problem between OnClick and OnCommand in ImageButton. Thanks again. – dargod Mar 07 '12 at 13:55
1

Just use the following.

Don't take image button just take image control and put between anchor tag.

<a href="~/ProductDetails.aspx?productid=<%#Eval("ProductID")"><asp:Image ID="img" runat="server" ImageUrl="="~/Graphics/profile_button.png"> </a>    

Put this code in datalist. It's lighter code.

Mohan
  • 907
  • 5
  • 22
  • 45