Click here to Skip to main content
13,409,230 members (41,952 online)
Click here to Skip to main content
Add your own
alternative version


24 bookmarked
Posted 13 Jul 2011

Hyperlinked Image in ASP.NET GridView

, 13 Jul 2011
Rate this:
Please Sign up or sign in to vote.
3 different techniques of adding hyperlinked image to GridView control in data-centric RIA
This is an old version of the currently published tip/trick.
Typical scenario in data-centric RIA development is to insert into GridView control a web 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. In most general case the image could be missing, thus the alternative text should serve as a hyperlinked text. Three different solutions pertinent to ASP.NET GridView control are described below.
<h3>1. TemplateField with pair of <code>Hyperlink</code> field and <code>img</code> element</h3>
<h4>Listing 1</h4>
<pre lang="aspnet">
<asp:HyperLink runat="server" NavigateUrl='<%# Eval("Wiki_URL") %>' Target="_blank">
<img src='<%# Eval("Image_URL") %>' alt="Read online" />
<h3>2. TemplateField with <code>ImageButton</code></h3>
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):
<h4>Listing 2</h4>
<pre lang="html"><itemtemplate>
&lt;asp:ImageButton runat="server" ImageUrl='&lt;%# Eval("Image_URL") %&gt;' PostBackUrl='&lt;%# Eval("Target_URL") %&gt;' AlternateText="Read online" /&gt;
<h3>3. TemplateField with <code>ImageButton</code> and javascript onClick</h3>
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:
<h4>Listing 3</h4>
<pre lang="html"><itemtemplate>
&lt;asp:ImageButton ID="ImageButton1" runat="server" ImageUrl='&lt;%# Eval("Image_URL") %&gt;'
OnClientClick='&lt;%# String.Format("javascript:return openTargetURL(\"{0}\")", Eval("Target_URL")) %&gt;' AlternateText="Read online" /&gt;
<h5>Javascript snippet to be added to <code>head</code> section</h5>
This sample Javascript code snippet allows opening target web page in new browser window with customized appearance set programmatically:
<pre lang="html">&lt;script type="text/javascript"&gt;
function openTargetURL(url)
{, "newWindow", "resizable=1,menubar=0,toolbar=0,scrollbars=1"); }
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:

<a href="" target="_blank"><img src="" alt="Music Library implemented as nested GridView controls" /></a>
<h3>Design Notes: Server side Image rendering </h3>
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:
<h4>Listing 4</h4>
<pre lang="html">&lt;style type="text/css"&gt;
/* image rendered as img element within table */
img { width:200px; }
/* image rendered as &lt;input&gt; element within table */
input[type="image"] {width:200px; }
<h3>Image decoration via HTML5/CSS3</h3>
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. <a href="">Add Image Borders using HTML5/CSS3</a>[<a href="" target="_blank" title="New Window">^</a>]

<i>Note to editors/readers: as FYI, the system incorrectly renders the html tags pertinent to ASP.NET section; it seems to cut off the closing tags, thus the resulting HTML code snippet looks kinda weird</i>


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

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.
GeneralMy vote of 3 Pin
ChenShaoHsi12-Feb-15 3:22
memberChenShaoHsi12-Feb-15 3:22 
QuestionNice Article Pin
khaliqhamid20-May-14 1:56
memberkhaliqhamid20-May-14 1:56 
AnswerRe: Nice Article Pin
DrABELL21-May-14 4:09
professionalDrABELL21-May-14 4:09 
GeneralReason for my vote of 3 sdfghjkl Pin
Jailendra Singh27-Nov-11 20:07
memberJailendra Singh27-Nov-11 20:07 
GeneralRe: Sorry, but I did not understand your message. Pin
DrABELL14-Dec-11 2:43
memberDrABELL14-Dec-11 2:43 
GeneralReason for my vote of 5 great! Pin
Marcio_Coelho27-Sep-11 11:57
memberMarcio_Coelho27-Sep-11 11:57 
GeneralRe: Thanks! Pin
DrABELL27-Sep-11 12:40
memberDrABELL27-Sep-11 12:40 
GeneralReason for my vote of 4 Good Article Pin
mandar13054-Aug-11 23:45
membermandar13054-Aug-11 23:45 
GeneralReason for my vote of 5 nice Pin
Monjurul Habib27-Jul-11 23:17
memberMonjurul Habib27-Jul-11 23:17 
GeneralRe: Thanks! Pin
DrABELL28-Jul-11 4:07
memberDrABELL28-Jul-11 4:07 
GeneralReason for my vote of 5 Good one Alex. Pin
thatraja14-Jul-11 8:20
mvpthatraja14-Jul-11 8:20 
GeneralRe: Many thanks! Pin
DrABELL14-Jul-11 9:58
memberDrABELL14-Jul-11 9:58 

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
Web04 | 2.8.180221.1 | Last Updated 13 Jul 2011
Article Copyright 2011 by DrABELL
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid