Saturday, 10 September 2016

Dynamic Image Gallery Using jQuery in Asp.net C#

9/10/2016 - By Pranav Singh 6

This article will show you how you can create a dynamic image gallery in asp.net using jQuery data from SQL server table.


This article I have referred the below gallery.


Now we will create sql table for storing data. Which we will retrieve in code.

Now for this article first we will create a new asp.net application and add the below code into the page.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication3.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Dynamic Image Gallery Using jQuery in Asp.net c#</title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <link rel="stylesheet" type="text/css" href="StyleSheet1.css" />
    <script type="text/javascript">
        jQuery(document).ready(function () {
            jQuery('.photo-thumbnails .thumbnail').click(function () {
                // Setting class "current" to the thumbnail that was clicked.
                jQuery('.photo-thumbnails .thumbnail').removeClass('current');
                jQuery(this).addClass('current');
                // Getting "src" attribute of the image that was clicked.
                var path = jQuery(this).find('img').attr('src');
                // Setting "src" attribute of the big image.
                jQuery('#big-photo img').attr('src', path);
            });

        }); 
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div class="gallery-photos clearfix">
        <div class="photo-thumbnails" runat="server" id="divGallery">
                      
        </div>
        <div id="big-photo" class="big-photo">
            <img src="images/1.jpg" alt="" />
        </div>
    </div>
    </form>
</body>
</html>

In above code I have added simply a jquery reference and make the photo-thumbnails runat server and provided id to it.

Now lets check the code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Text;
namespace WebApplication3
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            BindGallery();
        }
        public void BindGallery()
        {
            SqlConnection objcon = new SqlConnection("Data Source=VINAY-PP;Initial Catalog=DEMO;Integrated Security=True");
            DataTable objdt = new DataTable();
            string query = "select * from ImageGallery;";
            SqlDataAdapter objda = new SqlDataAdapter(query, objcon);
            objcon.Open();
            objda.Fill(objdt);
            objcon.Close();
            StringBuilder objstring = new StringBuilder();
            if (objdt.Rows.Count > 0)
            {
                for (int i = 0; i < objdt.Rows.Count; i++)
                {
                    string cssClass = i == 0 ? "thumbnail current" : "thumbnail";
                    objstring.Append("<div class='" + cssClass + "'>");
                    objstring.Append("<div class=\"thumbnail-inner\">");
                    objstring.Append("<img src='" + objdt.Rows[i]["ImagePath"].ToString() + "' alt=\"\" />");
                    objstring.Append(" </div>");
                    objstring.Append(" </div>");
                }
                divGallery.InnerHtml = objstring.ToString();
            }
        }
    }
}

In above code I have taken data from database and then prepare a string for gallery with the image path. After preparing the string I have bind it to the divgallery.

Now we have done to check the output.




DOWNLOAD

About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

6 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top