From DataList to details page
-
09-12-2019 - |
Question
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?
Solution
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();
//..
}
OTHER TIPS
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.