Click here to Skip to main content
15,884,353 members
Articles / Web Development / HTML5
Tip/Trick

Hyperlinked Images in ASP.NET GridView

Rate me:
Please Sign up or sign in to vote.
4.75/5 (17 votes)
5 Aug 2011CPOL1 min read 115.7K   25  
3 different techniques of adding hyperlinked image to ASP.NET GridView control in data-centric RIA
This is an old version of the currently published tip/trick.

Problem to solve


Pretty common development scenario is to place inside GridView control the image corresponding to some "Image_URL" value stored in the data field of underlying database, which is "hyperlinked" to another "Target_URL" also stored in the database. Image OnClick event should open the target web page. If image is missing then the alternative text should serve as a hyperlinked text.

Three possible solutions pertinent to the ASP.NET GridView control are described below:

1. TemplateField with Hyperlink and img element


Listing 1


<asp:TemplateField>
    <ItemTemplate>
        <asp:HyperLink runat="server" NavigateUrl='<%# Eval("Wiki_URL") %>'
Target="_blank">
            <img src='<%# Eval("Image_URL") %>'  alt="Read online" />
        </asp:HyperLink>
    </ItemTemplate>
</asp:TemplateField>

2. TemplateField with ImageButton


This technique though it provides the minimum coding and fairly robust results is not recommended as it uses PostBack and thus requires the server "round-trip", dramatically degrading the responsiveness of RIA and negatively impacting the overall user experience (it's brought here mostly for didactic purpose).

Listing 2


<asp:TemplateField>
    <ItemTemplate>
            <asp:ImageButton runat="server" ImageUrl='<%# Eval("Image_URL") %>'
            PostBackUrl='<%# Eval("Target_URL") %>' AlternateText="Read online" />
    </ItemTemplate>
</asp:TemplateField>

3. TemplateField with ImageButton and Javascript window.open added to onClick


This method requires some additional Javascript coding, but provides maximum flexibility in terms of formatting the target window to be opened on image click event:

Listing 3


<itemtemplate>
 <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl='<%# Eval("Image_URL") %>' 
                        OnClientClick='<%# String.Format("javascript:return openTargetURL(\"{0}\")", Eval("Target_URL")) %>'  AlternateText="Read online" />
</itemtemplate>

Javascript snippet to be added to head section

This sample Javascript code snippet allows opening target web page in new browser window with customized appearance set programmatically:
HTML
<script type="text/javascript">
    function openTargetURL(url) 
    { window.open(url, "newWindow", "resizable=1,menubar=0,toolbar=0,scrollbars=1"); }
</script>

DEMO


ASP.NET GridView Hyperlinked image technique as described above has been used in Online Music Library: click on the sample snapshot shown below to see the working demo:
Music Library implemented as nested GridView controls

Design Notes: Server side Image rendering


Be aware that pertinent to Listing 1, the image is rendered as "img" element, while in two other cases it is rendered as "input type="image" element. Corresponding sample CSS3 code snippet in Listing 4 shows the best practices addressing these differences.

Listing 4


HTML
<style type="text/css">
    /* image rendered as img element within table */
    img { width:200px; }
    /* image rendered as <input> element within table */
    input[type="image"] {width:200px; }
</style>

Image decoration via HTML5/CSS3


Various aesthetic enhancement techniques, namely single and double borders, borders with inset shadows and color gradients could be applied to the image as described in [1].

References


  1. Add Image Borders using HTML5/CSS3[^]
  2. Online Slide Show as pure HTML5 / CSS3 solution: NO Javascript[^]

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Engineer
United States United States
Dr. Alexander Bell (aka DrABell), a seasoned full-stack Software (Win/Web/Mobile) and Data Engineer holds PhD in Electrical and Computer Engineering, authored 37 inventions and published 100+ technical articles and developed multiple award-winning apps (App Innovation Contests AIC 2012/2013 submissions) Alexander is currently focused on Microsoft Azure Cloud and .NET 6/8 development projects.

  1. HTML5/CSS3 graphic enhancement: buttons, inputs
  2. HTML5 Tables Formatting: Alternate Rows, Color Gradients, Shadows
  3. Azure web app: Engineering Calculator VOLTMATTER
  4. Azure: NYC real-time bus tracking app
  5. Quiz Engine powered by Azure cloud
  6. 'enRoute': Real-time NY City Bus Tracking Web App
  7. Advanced CSS3 Styling of HTML5 SELECT Element
  8. Aggregate Product function extends SQL
  9. YouTube™ API for ASP.NET

Comments and Discussions

Discussions on this specific version of this article. Add your comments on how to improve this article here. These comments will not be visible on the final published version of this article.