aspdotnet-suresh offers articles and tutorials,csharp dot net, articles and tutorials,VB.NET Articles,Gridview articles,code examples of 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Display Gridview images with lightbox effect whenever user clicks on images in gridview using

Mar 5, 2011

In this article I will explain how to show gridview images with lightbox effect using

In my previous article I explained clearly how to insert images into our images folder and images path in database and display images in gridview from folder based on imagepaths exists in database for that check this link
how to insert images into folder and imagespath into database and display images I gridview .After inserting images in folder and displayed in gridview I extended functionality to display gridview images using lightbox effect if user click on any image in gridview at that time we need to show that images with lightbox effect. To display images in gridview with lightbox effect we need to design our aspx page like this

<html xmlns="">
<head id="Head1" runat="server">
<title>Show Gridview Images with Lightbox effect</title>
<style type="text/css">
position: absolute;
top: 0%;
left: 0%;
width: 100%;
height: 100%;
-moz-opacity: 0.8;
filter: alpha(opacity=80);
.white_content {
position: absolute;
top: 25%;
left: 35%;
width: 25%;
padding: 0px;
border: 0px solid #a6c25c;
background-color: white;
overflow: auto;
font-family:Arial, Helvetica, sans-serif;
<script type="text/javascript">
function ShowImages() {
document.getElementById('light').style.display = 'block';
document.getElementById('fade').style.display = 'block'
return false;
<form id="form1" runat="server">
<div align="center">
<asp:GridView runat="server" ID="gvImages" AutoGenerateColumns="false"
DataSourceID="sqldataImages" CssClass="Gridview"
<asp:BoundField DataField="ID" HeaderText="ID" />
<asp:BoundField DataField="ImageName" HeaderText="Image Name" />
<asp:TemplateField HeaderText="Image">
<asp:ImageButton ID="imgbtn" runat="server" ImageUrl='<%#Eval("ImagePath") %>' OnClick="imgbtn_Click" />
<asp:SqlDataSource ID="sqldataImages" runat="server"  ConnectionString="<%$ConnectionStrings:dbconnection %>"
SelectCommand="select * from ImagesPath" >
<div id="light" class="white_content">
<table cellpadding=0 cellspacing=0 border=0 style="background-color:#a6c25c;" width="100%"><tr><td height="16px"><a href = "javascript:void(0)" onclick = "document.getElementById('light').style.display='none';document.getElementById('fade').style.display='none'"><img src="close.gif" style="border :0px"  width="13px" align="right" height="13px"/></a></td></tr>
<tr><td style="padding-left:16px;padding-right:16px;padding-bottom:16px">
<table align="center"  border="0" cellpadding="0" cellspacing="0" style="background-color:#fff" width="100%">
<td align="center" colspan="2" class="headertext" >Gridview Image </td>
<tr><td align="center">
<asp:Image ID="imglightbox" runat="server"/>
<tr><td height="10px"></td></tr>
<div align="center" class=" headertext">
<asp:Label ID="txtlbl" runat="server"  ></asp:Label></div>
<div id="fade" class="black_overlay"></div>
After completion of aspx page design write the following code in code behind

protected void imgbtn_Click(object sender, EventArgs e)
ImageButton imgbtn = sender as ImageButton;
GridViewRow gvrow = imgbtn.NamingContainer as GridViewRow;
//Find Image button in gridview
ImageButton imgbtntxt =(ImageButton)gvrow.FindControl("imgbtn");
//Assign imagebutton url to image field in lightbox
imglightbox.ImageUrl = imgbtn.ImageUrl;
ScriptManager.RegisterStartupScript(Page, typeof(Page) , "ShowValidation", "javascript:ShowImages();", true);
After that set your database connection in web.config like this because we are using this connection in our sqldatasource to get the data from database

<add name="dbconnection" connectionString="Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB"/>
</connectionStrings >


Download sample code attached


If you enjoyed this post, please support the blog below. It's FREE!

Get the latest,, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email


Anonymous said...

Thanks....and nice article,very helpful..!!

nikoo said...

Thanks for sharing this. I have a gridview but instead of show only image in LightBox I would like to show aspx page that is containing Product Image along with Product description. Is there any way to have lightbox show aspx page?

Anonymous said...

Thanks for this article and I have small problem when we upload and click the submit button it is redirecting to the
But there is no such page in your project.Can you rectify this for me.

Suresh Dasari said...

hi nikoo here in my post i designed table to display the image with lightbox effect instead of image displaying you should use iframe in that iframe call ur productpage then at that u have a chance to show the product description page instead of image with lightbox effect

Suresh Dasari said...

hi can you please check the code in this insert images in folder and image path in database post after insertion of image i am redirecting to home page may because of that is the page is redirecting u can check it once

Kiran,Software Engineer said...

Thanks suresh for you reply and I got rectified with the previous problem and I would like you to ask that can you include me the code for uploading multiple images in this example.

Anonymous said...

Thank you Suresh. Simple, elegant, helpful!

Neha said...

Hello Suresh,i am getting httphandler error,please can you sort out my dis problem.thank you,Neha

NATURE said...

hi i want coding grid view add column checkbox
i am click checkbox values store paid in db


Anonymous said...

this Task Solve A problem For My Thanks

jayashri said...

your all post is so helpful for us and your way of presentation is also nice

krupalini said...

thanks yar.......... ur posts are really good

Anonymous said...

can u update mouseover on image will zoom.that is featured image zoom in jquery..if u have update the articles...


Suresh Dasari said...

check this post

Yogesh Chavan said...

Hello Sir . how to manage image size of lightbox effect

Anonymous said...

Very Helpful

JayPrakash kumar said...

hello sir how to edit update delete image in Gridview giv me fast answer

Zwelithini Dladlama said...

Eh thank you Suresh, very clear and straight to the point. Can you do that in

syed irfan said...

Its not Working

syed irfan said...

Ya its working Dude. Thnx...

Anonymous said...

Thanks a ton! :)

Saranya Raman said...

hi can u please tell me how to set image as caption in gridview?

Anonymous said...

Hello, I'm a newer in ASP.NET, and very intested this codes, but I can't download the source codes. Why?

Muhasin n.k said...

hai suresh

thanks for the great work,
one thing please check once most od the demo page image is not working properly
if you can do it ,its a big help for us.
thank u
muhasin NK

Give your Valuable Comments

Other Related Posts

© 2015 All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.