We help IT Professionals succeed at work.

GridView hyperlink image done another way?

mitdaniels
mitdaniels used Ask the Experts™
on
Is there a another way of adding a hyperlinked image to a GridView:

<asp:GridView runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField>
    <ItemTemplate>
        <asp:HyperLink NavigateUrl='<%# Eval("HttpUri")%>' runat="server">
            <asp:image imageurl='<%# Eval("ImageUri")%>' runat="server" />
        </asp:HyperLink>
    </ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Software Engineer
Top Expert 2011
Commented:
The hyperlink control has an imageurl property that can be set to specify the image for the hyperlink

<asp:GridView runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField>
    <ItemTemplate>
        <asp:HyperLink NavigateUrl='<%# Eval("HttpUri")%>' ImageUrl='<%# Eval("ImageUri")%>' runat="server" />
    </ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

Open in new window

Author

Commented:
Thanks, do you know whether these are the only 2 ways of doing this. In other words, do I have to use the TemplateField / ItemTemplate controls to achieve this?
Paul JacksonSoftware Engineer
Top Expert 2011
Commented:
It is the recommended way of doing that is with a template field but can be done without a template field as well.

http://geekswithblogs.net/AzamSharp/archive/2005/10/30/58551.aspx
Top Expert 2010
Commented:

Author

Commented:
I tried without the template field, but cannot get the image to display when it loads, instead it provides a link to the image. How do I get the image to display as soon as the control is loaded?

<asp:HyperLinkField DataNavigateUrlFields="ImageUri" DataTextField="LabName" NavigateUrl="HttpUri" />

Author

Commented:
Thanks