Click here to Skip to main content
13,556,507 members
Click here to Skip to main content
Add your own
alternative version


24 bookmarked
Posted 13 Jul 2011
Licenced CPOL

Hyperlinked Images in ASP.NET GridView

, 11 Feb 2015
Rate this:
Please Sign up or sign in to vote.
3 different techniques of adding hyperlinked image to ASP.NET GridView in data-centric RIA

Problem to Solve

A 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:HyperLink runat="server" NavigateUrl='<%# Eval("Wiki_URL") %>'

            <img src='<%# Eval("Image_URL") %>'  alt="Read online" />

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:ImageButton runat="server" ImageUrl='<%# Eval("Image_URL") %>'

            PostBackUrl='<%# Eval("Target_URL") %>' AlternateText="Read online" />

3. TemplateField with ImageButton and Javascript 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

 <asp:ImageButton ID="ImageButton1" runat="server" 

 ImageUrl='<%# Eval("Image_URL") %>' 

                        OnClientClick='<%# String.Format
            ("javascript:return openTargetURL(\"{0}\")", 
            Eval("Target_URL")) %>'  AlternateText="Read online" />
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) 
    {, "newWindow", "resizable=1,menubar=0,toolbar=0,scrollbars=1"); }

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; }

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].


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


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


About the Author

Founder Infosoft International Inc
United States United States
Dr. Alexander Bell is a seasoned full-stack Software Engineer (Win/Web/Mobile). He holds PhD in EE/IT, authored 37 inventions and published 300+ technical articles. Currently focused on Java/Javascript, .NET/C#, Android/Mobile, SQL, 'Big Data', Machine Learning, AI, IoT. Alex participated in App Innovation Contests (AIC 2102/2013) with multiple winning submissions. Portfolio samples:

  1. Real-time NY Bus Tracking Web App (IoT)
  2. Android Fraction Calculator "Fractal MK-12"
  3. Android Prime Factorization Calculator 18-digit)
  4. Semaphon™ semantic phone num-to-text converter
  5. Educational Web Portal
  6. Free Online NY Payroll Tax Calculator
  7. WebTV powered by YouTube Player powered by .NET API (#1 on Google)
  8. Engineering Calculator VOLTA
  9. Big Data: Facebook and YouTube
  10. NY City Trivia Quiz
  11. Publications by A. Bell

You may also be interested in...

Comments and Discussions

GeneralReason for my vote of 5 nice Pin
Monjurul Habib27-Jul-11 22:17
memberMonjurul Habib27-Jul-11 22:17 
GeneralRe: Thanks! Pin
DrABELL28-Jul-11 3:07
memberDrABELL28-Jul-11 3:07 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180515.1 | Last Updated 11 Feb 2015
Article Copyright 2011 by DrABELL
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid