Click here to Skip to main content
15,893,588 members
Articles / Web Development / HTML

ASP.NET MVC3 Slideshow Control using jQuery and XML

Rate me:
Please Sign up or sign in to vote.
5.00/5 (7 votes)
11 Sep 2012CPOL6 min read 62.1K   3.5K   23  
Article on how to create an ASP.NET MVC3 slideshow user control/partial view using jQuery and XML
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;

namespace Mvc2SlideShow.Models
{
    public class YsaSSModel
    {
        /// <summary>
        /// div id
        /// </summary>
        private string _wrapperID = YsaSSHelper.GetNextId();
        public string WrapperID
        {
            get
            {
                return "ysaSlideShowDiv" + _wrapperID;
            }
        }

        /// <summary>
        /// slideshow container
        /// </summary>
        public string SSContainer { get; set; }

        /// <summary>
        /// slideshow script
        /// </summary>
        public string SSScript { get; set; }

        /// <summary>
        /// xml path
        /// </summary>
        private string _xPath = "site";

        public string XPath
        {
            get { return this._xPath; }
            set { this._xPath = value; }
        }

        /// <summary>
        /// xml source
        /// </summary>
        private string _xmlSource = "/xml/sites.xml";
        public string XMLSource
        {
            get { return this._xmlSource; }
            set { this._xmlSource = value; }
        }

        private int _width = 728;
        public int Width
        {
            set { this._width = value; }
            get { return this._width; }
        }

        private int _height = 95;
        public int Height
        {
            set { this._height = value; }
            get { return this._height; }
        }

        /// <summary>
        /// autoplay true|false
        /// </summary>
        /// 
        private bool _autoPlay = true;
        public bool AutoPlay
        {
            get
            {
                return this._autoPlay;
            }
            set
            {
                this._autoPlay = value;
            }
        }

        /// <summary>
        /// ShowNavigation true|false
        /// </summary>
        /// 
        private bool _showNavigation = true;
        public bool ShowNavigation
        {
            get
            {
                return this._showNavigation;
            }
            set
            {
                this._showNavigation = value;
            }
        }

        private int _delay_btw_slide = 10000;
        /// <summary>
        /// delay between slide in miliseconds
        /// </summary>
        /// 
        public int Delay_btw_slide
        {
            set { this._delay_btw_slide = value; }
            get { return this._delay_btw_slide; }
        }

        private int _fadeDuration = 2000;
        /// <summary>
        /// transition duration (milliseconds)
        /// </summary>
        /// 
        public int FadeDuration
        {
            set { this._fadeDuration = value; }
            get { return this._fadeDuration; }
        }

        private int _cycles_before_stopping = 99;
        /// <summary>
        /// cycles befote stopping
        /// </summary>
        public int Cycles_before_stopping
        {
            set { this._cycles_before_stopping = value; }
            get { return this._cycles_before_stopping; }
        }

        private string _btnPrevious = "/Content/ysaSS-images/previous.gif";
        public string BtnPreviousImagePath
        {
            get
            {
                return this._btnPrevious;
            }
            set
            {
                this._btnPrevious = value;
            }
        }

        /// <summary>
        /// Next button
        /// </summary>
        /// 
        private string _btnNext = "/Content/ysaSS-images/next.gif";
        public string BtnNextImagePath
        {
            get
            {
                return this._btnNext;
            }
            set
            {
                this._btnNext = value;
            }
        }

        /// <summary>
        /// Play button
        /// </summary>
        /// 
        private string _btnPlay = "/Content/ysaSS-images/play.gif";
        public string BtnPlayImagePath
        {
            get
            {
                return this._btnPlay;
            }
            set
            {
                this._btnPlay = value;
            }
        }

        /// <summary>
        /// Play button
        /// </summary>
        /// 
        private string _btnPause = "/Content/ysaSS-images/pause.gif";
        public string BtnPauseImagePath
        {
            get
            {
                return this._btnPause;
            }
            set
            {
                this._btnPause = value;
            }
        }


        //constructor
        public YsaSSModel()
        {

        }

        public void CreateScript()
        {
            SSContainer = YsaSSHelper.CreateDiv(WrapperID);
            SSScript = YsaSSHelper.CreateScript(WrapperID, XMLSource, XPath, Width, Height,
                BtnPreviousImagePath,
                BtnPlayImagePath, BtnNextImagePath, BtnPauseImagePath,
                ShowNavigation, AutoPlay,
                Delay_btw_slide, Cycles_before_stopping, FadeDuration);
        }

    }

    //helper class
    public static class YsaSSHelper
    {
        public static int ID = 0;


        //id
        public static string GetNextId()
        {
            ID++;
            return ID.ToString();
        }

        //create html div
        public static string CreateDiv(string wrapperID)
        {
            StringBuilder sbDiv = new StringBuilder(string.Empty);
            sbDiv.Append("<div id='" + wrapperID + "' style='");
            sbDiv.Append(" background: none repeat scroll 0% 0% white;");
            sbDiv.Append(" overflow: hidden;");
            sbDiv.Append(" position: relative;");
            sbDiv.Append(" visibility: visible;");
            sbDiv.Append(" -moz-background-clip: border;");
            sbDiv.Append(" -moz-background-origin: padding;");
            sbDiv.Append(" -moz-background-inline-policy: continuous;");
            sbDiv.Append("'></div>");

            return sbDiv.ToString();
        }

        //create script
        public static string CreateScript(string wrapperID, string xmlSource, string xpath, int width,
            int height, string btnPreviousImagePath, string btnPlayImagePath, string btnNextImagePath,
            string btnPauseImagePath, bool showNavigation, bool autoPlay, int delay_btw_slide,
            int cycles_before_stopping, int fadeDuration)
        {
            StringBuilder ssScript = new StringBuilder(string.Empty);
            string arrName = "ysaMyArray" + wrapperID;

            ssScript.Append("<script type='text/javascript'>");
            ssScript.Append("var " + arrName + "= [];");
            ssScript.Append("$(document).ready(function() {");
            ssScript.Append(" $.ajax({");
            ssScript.Append("type: \"GET\",");
            ssScript.Append("url: '" + xmlSource + "',");
            ssScript.Append("cache: true,");
            ssScript.Append("dataType: \"xml\",");
            ssScript.Append("success: function(xml) {");
            ssScript.Append("var count = 0;");
            ssScript.Append("$(xml).find('" + xpath + "').each(function() {");

            ssScript.Append(" var url = $(this).find('url').text();");
            ssScript.Append("var target = $(this).find('target').text();");
            ssScript.Append("var imageURL = $(this).find('imageURL').text();");
            ssScript.Append("var alt = $(this).find('alt').text();");

            ssScript.Append(arrName + "[parseInt(count)] = new Array(imageURL, url, target, alt); ");
            ssScript.Append("count++;");
            ssScript.Append("});");

            ssScript.Append(" var mygallery" + wrapperID + " = new simpleGallery({");
            ssScript.Append(" wrapperid: '" + wrapperID + "',");
            ssScript.Append("dimensions: [" + width.ToString() + "," + height.ToString() + "],"); //width/height of gallery in pixels. Should reflect dimensions of the images exactly
            ssScript.Append("imagearray: " + arrName + ","); //array of images
            ssScript.Append("navimages: ['" + btnPreviousImagePath + "', '" +
                btnPlayImagePath + "', '" +
                btnNextImagePath + "', '" +
                btnPauseImagePath + "'],");
            ssScript.Append("showpanel: '" + showNavigation.ToString().ToLower() + "',");
            ssScript.Append(" autoplay: [" + autoPlay.ToString().ToLower() + "," + delay_btw_slide.ToString() + ","
                + cycles_before_stopping.ToString() + "],"); //[auto_play_boolean, delay_btw_slide_millisec, cycles_before_stopping_int]
            ssScript.Append(" persist: true,");
            ssScript.Append(" fadeduration:" + fadeDuration.ToString() + ","); //transition duration (milliseconds)
            ssScript.Append(" oninit: function() {"); //event that fires when gallery has initialized/ ready to run
            ssScript.Append("  },");
            ssScript.Append("  onslide: function(curslide, i) {"); //event that fires after each slide is shown
            //curslide: returns DOM reference to current slide's DIV (ie: try alert(curslide.innerHTML)
            //i: integer reflecting current image within collection being shown (0=1st image, 1=2nd etc)
            ssScript.Append("   }");
            ssScript.Append("  })");
            ssScript.Append("  }");
            ssScript.Append("   });");

            ssScript.Append(" });");
            ssScript.Append("</script>");

            return ssScript.ToString();
        }

    }

    public class MenuOptions
    {
        public int Width { get; set; }
        public int Height { get; set; }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
I have over 10 years of experience working with Microsoft technologies. I have earned my Microsoft Certified Technology Specialist (MCTS) certification. I'm a highly motivated self-starter with an aptitude for learning new skills quickly.

Comments and Discussions