Build Image Slider With ASP.NET

Introduction

The Microsoft ASP. NET technologies helps to build websites, and the web sites may be contain image sliders. When you want to put an image control in your website and attach many images with this control, and then if you want this image control to automatically rotate those images then I think the first thing you must do it with Ajax, for that you create a XML file as described here.

XmlFile.XML

Note:
add a XML file along with your website in the Solution Explorer, then right-click on the XML page thenselect "Properties" -> "Schemas" , then add the Schema name "adrotator.xsd" then write the following code:

<?xml version="1.0" encoding="utf-8" ?>

<Advertisements>

  <Ad>

    <ImageUrl>http://localhost:64916/images/img2.jpg</ImageUrl>

  </Ad>

  <Ad>

    <ImageUrl>http://localhost:64916/images/img3.jpg</ImageUrl>

  </Ad>

  <Ad>

    <ImageUrl>http://localhost:64916/images/img4.jpg</ImageUrl>

  </Ad>

</Advertisements>

After creating this file, drag and drop the ScriptManager control on the top of your "aspx" page in the design site then drag and drop the UpdatePanel and Timer control from the AJAX Extensions then drag and drop an AdRotator control and put it into the UpdatePanel. You must also place the timer control inside the UpdatePanel. Simply set the timer control's "interval" property and add the "advertisement file", in other words the previously created "XML file" into the AdRotator "advertisement file" properties.

Simply run your website, the image rotator will work properly.

Source Code

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default"  Debug="true"%>

 

<!DOCTYPE html>

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        <asp:ScriptManager ID="ScriptManager1" runat="server">

        </asp:ScriptManager>

        <asp:UpdatePanel ID="UpdatePanel1" runat="server">

            <ContentTemplate>

                <asp:AdRotator ID="AdRotator1" runat="server" AdvertisementFile="~/XMLFile.xml" />

                <asp:Timer ID="Timer1" runat="server" Interval="2000">

                </asp:Timer>

            </ContentTemplate>

        </asp:UpdatePanel>

        </div>

    </form>

</body>

</html>

 

Output

adrotator-ajax.gif

Image slider with JavaScript in ASP.NET

If you not want to use all these things to simply use an image control and manage that control by JavaScript, then for the image rotation to occur automatically in ASP.NET websites, let's see how to do that.

I wrote some simple code for doing all the preceding AJAX functionality with some simple JavaScript code.

The following script code does all those things efficiently and easily.


<
script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js"></script>

        <script type="text/javascript">

            var NoofImage = ['http://localhost:64916/images/img1.png', 'http://localhost:64916/images/img2.jpg', 'http://localhost:64916/images/img3.jpg', 'http://localhost:64916/images/img4.jpg'];

            var count = NoofImage.length;

            $(function () { setInterval(Slider, 2000) });

            function Slider()

            {

                $('#imageSlide').fadeIn("slow",function(){$(this).attr('src', NoofImage[(NoofImage.length++)%count]).fadeIn("slow");

                });

            }

        </script>

Now let's see the full source code

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default"  Debug="true"%>

 

<!DOCTYPE html>

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

   <script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js"></script>

        <script type="text/javascript">

            var NoofImage = ['http://localhost:64916/images/img1.png', 'http://localhost:64916/images/img2.jpg', 'http://localhost:64916/images/img3.jpg', 'http://localhost:64916/images/img4.jpg'];

            var count = NoofImage.length;

            $(function () { setInterval(Slider, 2000) });

            function Slider()

            {

                $('#imageSlide').fadeIn("slow",function(){$(this).attr('src', NoofImage[(NoofImage.length++)%count]).fadeIn("slow");

                });

            }

        </script>

        <img id="imageSlide" src="www.c-sharpcorner.com" style="height: 250px; width: 250px" />

        </div>

    </form>

</body>

</html>


Output

image-slider-javascript.gif