SIGN UP MEMBER LOGIN:    
ARTICLE

JQuery Carousel Control in ASP.NET

Posted by Surya Prakash Articles | ASP.NET MVC with C# November 01, 2011
In this article we will explore how to build a Carousel control.
Reader Level:


In this article we will explore how to build a Carousel control. To start with first we will build a 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 JQuery is.

 

Below are the steps to be followed to build a Carousel control

1.       Open a new web site & start with default.aspx as mentioned below

2.       Download the below JQuery java script files from http://jquery.com site

a.       jquery-latest.pack.js

b.      jcarousellite_1.0.1.js

3.       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>

 

4.       Now we will add a div, which will contain an 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" ></li>

        <li><img src=" sample-logo.png" alt="" width="100" height="100" ></li>

        <li><img src=" sample-logo.png" alt="" width="100" height="100" ></li>

        <li><img src=" sample-logo.png" alt="" width="100" height="100" ></li>

    </ul>

</div>

 

5.       Now for navigation of images, we need 2 buttons for forward & backward navigation.

<button class="prev"><<</button>

<button class="next">>></button>

 

6.       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( */

 

7.       Now run the page & see the output.

8.       So far so good, till now we have seen static HTML work fine.  Let's say we have image list which comes from DB source or any other source & you want to bind it to grid view or any list control. But when you use these control 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.

9.       So to get out of this & generate similar stuff, we need to Repeater control instead of GridView, DataGrid and DataList

10.   First lets add a 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>

                </asp:Repeater>

            </ul>

        </div>

  

11.   To bind this repeater with a source, we will generate a static list as following

 

          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

 

12.   Finally add this list to our repeater control

 

myRepeater.DataSource = list;

myRepeater.DataBind();

  

13.   Now run the application & have a look at the output.

 

Hope this helps!

Login to add your contents and source code to this article
share this article :
post comment
 

can u send me the demo code example .from ur example it is not working properly...

Posted by angshuman dash May 12, 2012

A great thing would be if you could include a picture of the finished result.

Posted by miram miram Nov 18, 2011

Hi, I want to use same steps for repeater control. how can i use it? plz reply.

Posted by Sunny Jagtap Nov 17, 2011
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor