Click here to Skip to main content
15,034,598 members
Articles / Web Development / HTML5
Tip/Trick
Posted 13 Jul 2011

Stats

103.8K views
25 bookmarked

Hyperlinked Images in ASP.NET GridView

Rate me:
Please Sign up or sign in to vote.
4.75/5 (17 votes)
11 Feb 2015CPOL1 min read
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.NET
<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.NET
<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

ASP.NET
<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>

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)

Share

About the Author

DrABELL
Software Developer (Senior)
United States United States
Dr. Alexander Bell is a seasoned full-stack Software Engineer (Win/Web/Mobile). He holds PhD in Electrical and Computer Engineering, authored 37 inventions and published 300+ technical articles. Currently focused on multiple Android/Mobile development projects and Big Data' Machine Learning, AI, IoT. Alex participated in App Innovation Contests (AIC 2102/2013) with multiple winning submissions. Sample portfolio apps and publications:

  1. Publications by A. Bell
  2. Educational Web Portal
  3. Engineering Calculator VOLTA
  4. Aggregate Product function extends SQL
  5. Use SQL to generate large data sequence
  6. HTML5/CSS3 graphic enhancement: buttons, inputs
  7. Advanced CSS3 Styling of HTML5 SELECT Element
  8. enRoute: Real-time NY City Bus Tracking Web App
  9. YouTube™ API for ASP.NET
  10. HTML5 Tables Formatting: Alternate Rows, Color Gradients, Shadows

Comments and Discussions

 
GeneralMy vote of 3 Pin
ChenShaoHsi12-Feb-15 2:22
MemberChenShaoHsi12-Feb-15 2:22 
QuestionNice Article Pin
khaliqhamid20-May-14 0:56
Memberkhaliqhamid20-May-14 0:56 
AnswerRe: Nice Article Pin
DrABELL21-May-14 3:09
professionalDrABELL21-May-14 3:09 
GeneralReason for my vote of 3 sdfghjkl Pin
Jailendra Singh27-Nov-11 19:07
MemberJailendra Singh27-Nov-11 19:07 
GeneralRe: Sorry, but I did not understand your message. Pin
DrABELL14-Dec-11 1:43
professionalDrABELL14-Dec-11 1:43 
GeneralReason for my vote of 5 great! Pin
Marcio_Coelho27-Sep-11 10:57
MemberMarcio_Coelho27-Sep-11 10:57 
GeneralRe: Thanks! Pin
DrABELL27-Sep-11 11:40
professionalDrABELL27-Sep-11 11:40 
GeneralReason for my vote of 4 Good Article Pin
mandar13054-Aug-11 22:45
Membermandar13054-Aug-11 22:45 
GeneralReason for my vote of 5 nice Pin
Monjurul Habib27-Jul-11 22:17
professionalMonjurul Habib27-Jul-11 22:17 
GeneralRe: Thanks! Pin
DrABELL28-Jul-11 3:07
professionalDrABELL28-Jul-11 3:07 
GeneralReason for my vote of 5 Good one Alex. Pin
thatraja14-Jul-11 7:20
professionalthatraja14-Jul-11 7:20 
GeneralRe: Many thanks! Pin
DrABELL14-Jul-11 8:58
professionalDrABELL14-Jul-11 8: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.