ASP .NET Image Slider with fancy thumbnail hover effect
Introduction
Recently I was checking out some nice jQuery image sliders and come across a fancy thumbnail hover effect. I decided to use this effect and set up an ASP .NET image slider with jQuery and CSS.
Background
In order to focus on the fancy thumbnail hover effect, I decided to make the application as simple as possible. I have used an XML document that contains the images paths and the Repeater control with paging.
Models
Using the code
The XML structure is defined as bellow.
<images>
<image imageUri="/Images/DSC_0349_k.JPG" thumbnailUri="/Images/DSC_0349_k_thumb.JPG" />
<image imageUri="/Images/DSC_0359_nb.JPG" thumbnailUri="/Images/DSC_0359_nb_thumb.JPG" />
</images>
Every image has a preview file whose location is set in the imageUri attribute and a thumbnail file whose location is set in the thumbnailUri attribute. You will notice that all the images are stored in the folder Images in the root of the Web site.
I decided to use the Repeater ASP .NET control because it responds to my need. This control is a data-bound list that allows custom layout by repeating a specified template for each item displayed in the list.
<asp:Repeater ID="RepeaterImages" runat="server" EnableViewState="false">
<HeaderTemplate>
<ul class="thumb">
</HeaderTemplate>
<ItemTemplate>
<li><a href='<%#DataBinder.Eval(Container.DataItem, "imageUri")%>'>
<img src='<%#DataBinder.Eval(Container.DataItem, "thumbnailUri")%>' alt="" />
</a></li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>
In the layout, I used an unordered list ul. Each item of the list contains the thumbnail and a link to its preview.
The binding is done by the method LoadImages.
private void LoadImages()
{
// Populate the repeater control with the images DataSet
// Indicate that the data should be paged
// Set the number of images you wish to display per page
// Set the PagedDataSource's current page
var pds = new PagedDataSource
{
DataSource = ImagesDataView,
AllowPaging = true,
PageSize = 9,
CurrentPageIndex = CurrentPage - 1
};
LabelCurrentPage.Text = "Page " + CurrentPage + " of " + pds.PageCount;
// Disable Previous or Next buttons if necessary
ImageButtonPrevious.Visible = !pds.IsFirstPage;
ImageButtonNext.Visible = !pds.IsLastPage;
// DataBind
RepeaterImages.DataSource = pds;
RepeaterImages.DataBind();
}
The current page number is persisted in the viewstate.
public int CurrentPage
{
get
{
// Look for current page in ViewState
object o = ViewState["CurrentPage"];
if (o == null) return 1; // default page index of 1
return (int) o;
}
set { ViewState["CurrentPage"] = value; }
}
The DataView is persisted in the cache to boost paging performance.
public DataView ImagesDataView
{
get
{
if (Cache["ImagesDataView"] == null)
{
// Read images info from XML document into a DataSet
var d = new DataSet();
d.ReadXml(MapPath("/App_Data/Images.xml"));
Cache["ImagesDataView"] = d.Tables[0].DefaultView;
}
return (DataView)Cache["ImagesDataView"];
}
}
The paging is done through two buttons and a label that contains the current page number.
<div id="pager">
<asp:Label ID="LabelCurrentPage" runat="server" EnableViewState="false"></asp:Label>
<asp:ImageButton ID="ImageButtonPrevious" runat="server" ImageUrl="Styles/Images/Arrow-Left-icon.png"
OnClick="ImageButtonPrevious_Click" CssClass="previous" EnableViewState="false" />
<asp:ImageButton ID="ImageButtonNext" runat="server" ImageUrl="Styles/Images/Arrow-right-icon.png"
OnClick="ImageButtonNext_Click" CssClass="next" EnableViewState="false" />
</div>
If the page is being rendered for the first time or isn't being loaded in response to a postback, we load the images.
protected void Page_Load(object sender, EventArgs e)
{
// Reload control if the page is being rendered for the first time
// or isn't being loaded in response to a postback
if (!IsPostBack)
{
LoadImages();
}
}
When the user clicks on the next button, the current page number is updated and the images are loaded.
protected void ImageButtonNext_Click(object sender, EventArgs e)
{
// Set viewstate variable to the next page
CurrentPage++;
// Reload control
LoadImages();
}
When the user clicks on the previous button, the current page number is updated and the images are loaded.
protected void ImageButtonPrevious_Click(object sender, EventArgs e)
{
// Set viewstate variable to the previous page
CurrentPage--;
// Reload control
LoadImages();
}
The icon buttons that I used are free and from Icon Archive.
The images that I used are free and from morgueFile.
You will notice that I disabled the session state because I don't need it and for performance reasons If you don't need it is recommended to disable it.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="jQuery.ImageSlider.Default" EnableSessionState="false" %>
You will also notice that I disabled the view state for all the controls on the page because I don't need it. I only need the viewstate for persisting the current page number.
For the explanation of the fancy hover effect, I invite you to read Soh Tanaka's blog.
You can also store the images in a database and retreive them through a businnes layer where you can perform image processing. Then you can use a PagedDataSource for paging, a listView that contains the images and an HttpHandler to display the images in the web page. I invite you to take a look at this article If you want to use a database.
You can also find this article on my blog.
Post Comment
Fslb88 I truly appreciate this article post.Much thanks again. Great.
HoXuND please go to the web sites we follow, like this one particular, as it represents our picks through the web
7cV6AH I will immediately grab your rss feed as I can not find your e-mail subscription link or e-newsletter service. Do you have any? Please let me know in order that I could subscribe. Thanks.
zQ9GCV Im thankful for the article.Much thanks again. Awesome.
Really enjoyed this article.Much thanks again. Really Cool.
8YdpH8 This particular blog is obviously awesome and factual. I have picked up a lot of useful advices out of this source. I ad love to visit it over and over again. Thanks a lot!
Nx3xyt I really liked your blog post.Really looking forward to read more. Much obliged.
i4QrUE Thanks for some other wonderful article. The place else may anyone get that kind of info in such an ideal approach of writing? I ave a presentation next week, and I am at the look for such info.
2xj2FC Wow that was odd. I just wrote an really long comment but after I clicked submit my comment didn at show up. Grrrr well I am not writing all that over again. Anyhow, just wanted to say superb blog!
tVhmqo I think you have observed some very interesting points, regards for the post.
qQM0Df Major thankies for the blog post. Want more.
NKHbBs Looking forward to reading more. Great article post.Thanks Again. Great.
Purchase nz, best price for in uk and getting prescription australia, can you buy over the counter in mexico except how much does pills cost. To buy australia also where can i buy over the counter uk, buying online safely and buying despite withdrawal uk.
hz0E6E This awesome blog is obviously interesting additionally informative. I have discovered helluva interesting tips out of this blog. I ad love to go back every once in a while. Cheers!
7ClV6t I'd should test with you here. Which isn't something I often do! I enjoy studying a publish that can make folks think. Additionally, thanks for allowing me to remark!
R3DJw1 Well I truly enjoyed reading it. This subject offered by you is very practical for proper planning.
wn2YnD Enjoyed every bit of your article post. Want more.
zfogyk You could definitely see your expertise within the paintings you write. The world hopes for more passionate writers such as you who aren't afraid to say how they believe. Always follow your heart.
Really informative blog.Really thank you!
Muchos Gracias for your blog post.Much thanks again. Will read on...
Appreciate you sharing, great article post.Really looking forward to read more.
Very good post. Great.
Muchos Gracias for your blog.Really looking forward to read more. Awesome.
Awesome blog article. Keep writing.
A round of applause for your article.Really looking forward to read more.
Major thankies for the blog post.Really thank you! Cool.
I really enjoy the article.Really thank you! Cool.
I value the article post.Thanks Again. Want more.
Thanks a lot for the article.Much thanks again. Really Great.
I cannot thank you enough for the article.Thanks Again. Will read on...
Thank you ever so for you article post. Want more.
I am so grateful for your post.Thanks Again. Great.
I truly appreciate this blog post.Much thanks again. Much obliged.
Hey, thanks for the article.Really looking forward to read more. Keep writing.
Thanks a lot for the article post. Awesome.
Great, thanks for sharing this blog.Really looking forward to read more. Want more.
Thanks for sharing, this is a fantastic blog post.Really looking forward to read more. Will read on...
Very neat article.Really looking forward to read more. Really Cool.
A big thank you for your article post.Really thank you! Fantastic.
I appreciate you sharing this blog.Really looking forward to read more. Much obliged.
Fantastic blog post.Much thanks again.
Really enjoyed this blog.Thanks Again. Keep writing.
Fantastic post.Thanks Again. Will read on...
Really informative blog.Really thank you! Really Great.
Great, thanks for sharing this post.Really thank you! Really Great.
I think this is a real great post.Really looking forward to read more. Cool.
Looking forward to reading more. Great article.Much thanks again. Really Cool.
Fantastic post. Will read on...
Say, you got a nice blog article. Really Great.
I believe everybody went like Ones New website, reason being things like this site without doubt has a article on quality. I loved read A New content. go on To remain a useful article, I will avail Once more by One additional time. Bless you.