Monday, March 14, 2011

Hyperlinked image in Gridview

Method 1:
Works, however, it is difficult to format the dimensions of the image.

Source:
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" 
runat="server" 
ImageUrl='<%# Eval("ProductID", "ContentImages/{0}.jpg") %>' 
NavigateUrl='<%# String.Format("~/productdetails.aspx?ProductID={0}", Eval("ProductID")) %>'>
</asp:HyperLink>
</ItemTemplate>

Method 2:

Use seperate Hyperlink control & Image controls - wrap the hyperlink tag around the image control.

<ItemTemplate>

<asp:HyperLink ID="hlkImageLink
runat="server" 
NavigateUrl='<%# String.Format("~/PT_productdetails.aspx?ProductID={0}", Eval("ProductID")) %>'>

<asp:Image ID="imgProductImage
runat="server" 
ImageUrl='<%# Eval("ProductID", "ContentImages/{0}.jpg") %>'
Width="50" Height="50" />
                    
</asp:HyperLink>
                
</ItemTemplate>

No comments:

Post a Comment