In this article, we will explore how to build Carousel control. To start with, first we will build
Static
HTML carousel control & then we will integrate it with our .NET Repeater control to show data & work similar to
Static
HTML content.
It is expected you have basic knowledge on what is JQuery.
Below are the steps to be followed to build Carousel control:
- Open new web site & start scratching default.aspx as mentioned below
- Download below JQuery JavaScript files from http://jquery.com site
- jquery-latest.pack.js
- jcarousellite_1.0.1.js
- Add above JS script reference in default.aspx under
head
tags as shown below:
<script type="text/javascript" src=" jquery-latest.pack.js"></script>
<script type="text/javascript" src=" jcarousellite_1.0.1.js"></script>
- Now we will add a
div
, which will contain Unordered list tag & image tags. This div
should be assigned with some class name. In our instance, we are using class name as “anyClass
” & image should be assigned with proper image source path:
<div class="anyClass">
<ul>
<li><img src=" sample-logo.png" alt="" width="100" height="100"></img></li>
<li><img src=" sample-logo.png" alt="" width="100" height="100"></img></li>
<li><img src=" sample-logo.png" alt="" width="100" height="100"></img></li>
<li><img src=" sample-logo.png" alt="" width="100" height="100"></img></li>
</ul>
</div>
- Now for navigation of images, we need 2 buttons for forward & backward navigation.
<button class="prev"><<</button>
<button class="next">>></button>
- In the
head
section, add JS script tag & add the following code.
<script type="text/javascript">
$(function() {
$(".anyClass").jCarouselLite({
btnNext: ".next",
btnPrev: ".prev"
});
});
</script>
/* Comment : div class name & following and name in this line of code should match $(".anyClass").jCarouselLite( */
- Now run the page & see the output.
- So far so good, till now we have seen static HTML works fine. Let’s say we have image list which comes from DB source or any other source & you want to bind it to gridview or any list control. But when you use these controls, you would not be able to add UnOrdered list
ul
and the HTML as same as above static. If you have similar HTML, JQuery carousel will not work properly. I mean you need to have div
, followed by ol
then followed by li
& finally img
tag.
- So to get out of this & generate similar stuff, we need
Repeater
control instead of GridView
, DataGrid
and DataList
.
- First let's add
Repeater
control to webpage, inside our DIV & css class should be “anyClass
”:
<div class="anyClass">
<ul>
<asp:Repeater ID="myRepeater" runat="server">
<itemtemplate>
<li>
<table>
<tr>
<td><img src="<%# DataBinder.Eval(Container.DataItem, " imageurl=")%>" alt="" width="100" height="100" />
</td>
</tr>
</table>
</li>
</itemtemplate>
</ul>
</div>
- To bind this repeater with source, we will generate static list as follows:
var list = (new[] { new { ImageURL = "images/sample-logo.png", Text = "The Sun" } }).ToList();
list.Add(new { ImageURL = "images/sample-logo.png", Text = "New York" });
list.Add(new { ImageURL = "images/sample-logo.png", Text = "Office Meeting" });
list.Add(new { ImageURL = "images/sample-logo.png", Text = "A Beach" });
list.Add(new { ImageURL = "images/sample-logo.png", Text = "Global Technology" });
list.Add(new { ImageURL = "images/sample-logo.png", Text = "My Global Technology" });
list.Add(new { ImageURL = "images/sample-logo.png", Text = "Our Global Technology" });
Hope the above code is simple enough to understand, if yes, then let’s move on.
- Finally, add this list to our repeater control:
myRepeater.DataSource = list;
myRepeater.DataBind();
- Now run the application and have a look at the output.
Happy coding… hope this helps!