Click here to Skip to main content
Click here to Skip to main content

Hyperlinked Images in ASP.NET GridView

By , 7 May 2013
 

Problem to solve

Common development task is to place inside GridView the image corresponding to some "Image_URL" typically 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, even 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:
<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

<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)

About the Author

DrABELL
Chief Technology Officer Infosoft Int'l
United States United States
Member
Dr. A. Bell has 20+ years of SW/EE experience, published 200+ tech articles and authored 37 inventions; Win/Web veteran, currently focused on: HTML5, CSS3, Javascript, jQuery, SQL, Windows 8, .NET, C#, WPF, Ultrabooks, Mobile. Developed popular Silverlight Media Player, 3 Fractions Calculator and best YouTube API for ASP.NET (#1 Goog). Sample pubs/projects:
  1. HTML5 Best Practices: Table formatting via CSS3
  2. Personal computer 2012
  3. New iPad: notes from NY Apple store
  4. YouTube and Facebook popularity metrics
  5. Edumatter M12: School Math Calculators and Equation Solvers
  6. How to select web browser and check its capabilities
  7. SQL generates large data sequence
  8. Aggregate Product function extends SQL
  9. Top-50 Digital Cameras
  10. Evolution of digital cameras
  11. WebTV Project: Embedded YouTube Player

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralReason for my vote of 3 sdfghjklmemberJailendra Singh27 Nov '11 - 19:07 
GeneralRe: Sorry, but I did not understand your message.memberDrABELL14 Dec '11 - 1:43 
GeneralReason for my vote of 5 great!memberMarcio_Coelho27 Sep '11 - 10:57 
GeneralRe: Thanks!memberDrABELL27 Sep '11 - 11:40 
GeneralReason for my vote of 4 Good Articlemembermandar13054 Aug '11 - 22:45 
GeneralReason for my vote of 5 nicememberMonjurul Habib27 Jul '11 - 22:17 
GeneralRe: Thanks!memberDrABELL28 Jul '11 - 3:07 
GeneralReason for my vote of 5 Good one Alex.mvpthatraja14 Jul '11 - 7:20 
GeneralRe: Many thanks!memberDrABELL14 Jul '11 - 8:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130513.1 | Last Updated 8 May 2013
Article Copyright 2011 by DrABELL
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid