Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Source Code for JQuery ASP.NET Controls

, 10 Jun 2009
Get a start to building your own JQuery Controls
Mullivan.Web.zip
TestApp
App_Data
bin
Meps.Web.dll
Meps.Web.pdb
Mullivan.Web.dll
Mullivan.Web.pdb
TestApp.dll
TestApp.pdb
CSS
images
ui-bg_flat_30_cccccc_40x100.png
ui-bg_flat_50_5c5c5c_40x100.png
ui-bg_glass_20_555555_1x400.png
ui-bg_glass_40_0078a3_1x400.png
ui-bg_glass_40_ffc73d_1x400.png
ui-bg_gloss-wave_25_333333_500x100.png
ui-bg_highlight-soft_80_eeeeee_1x100.png
ui-bg_inset-soft_25_000000_1x100.png
ui-bg_inset-soft_30_f58400_1x100.png
ui-icons_222222_256x240.png
ui-icons_4b8e0b_256x240.png
ui-icons_a83300_256x240.png
ui-icons_cccccc_256x240.png
ui-icons_ffffff_256x240.png
Images
bass.jpg
drums.jpg
guitar.jpg
mic.jpg
piano.jpg
LocalTestRun.testrunconfig
Properties
TestApp.csproj.user
TestResults
Sully_SULLYLAPTOP 2009-05-18 11_46_18.trx
Sully_SULLYLAPTOP 2009-05-18 11_46_18
In
SULLYLAPTOP
Out
testproject.dll
TestProject.pdb
Sully_SULLYLAPTOP 2009-05-18 11_47_15.trx
Sully_SULLYLAPTOP 2009-05-18 11_47_15
In
SULLYLAPTOP
Out
testproject.dll
TestProject.pdb
Sully_SULLYLAPTOP 2009-05-18 11_48_19.trx
Sully_SULLYLAPTOP 2009-05-18 11_48_19
Out
testproject.dll
TestProject.pdb
Sully_SULLYLAPTOP 2009-05-18 11_50_12.trx
Sully_SULLYLAPTOP 2009-05-18 11_50_12
In
SULLYLAPTOP
Out
testproject.dll
TestProject.pdb
Sully_SULLYLAPTOP 2009-05-18 11_51_17.trx
Sully_SULLYLAPTOP 2009-05-18 11_51_17
Out
testproject.dll
TestProject.pdb
Sully_SULLYLAPTOP 2009-05-18 11_52_03.trx
Sully_SULLYLAPTOP 2009-05-18 11_52_03
In
SULLYLAPTOP
Out
testproject.dll
TestProject.pdb
Sully_SULLYLAPTOP 2009-05-18 11_52_28.trx
Sully_SULLYLAPTOP 2009-05-18 11_52_28
In
SULLYLAPTOP
Out
testproject.dll
TestProject.pdb
Sully_SULLYLAPTOP 2009-05-19 11_04_00.trx
Sully_SULLYLAPTOP 2009-05-19 11_04_00
Out
testproject.dll
TestProject.pdb
Sully_SULLYLAPTOP 2009-05-19 11_25_01.trx
Sully_SULLYLAPTOP 2009-05-19 11_25_01
Out
testproject.dll
TestProject.pdb
Sully_SULLYLAPTOP 2009-05-19 11_25_08.trx
Sully_SULLYLAPTOP 2009-05-19 11_25_08
Out
testproject.dll
TestProject.pdb
Sully_SULLYLAPTOP 2009-05-19 11_25_29.trx
Sully_SULLYLAPTOP 2009-05-19 11_25_29
Out
testproject.dll
TestProject.pdb
Sully_SULLYLAPTOP 2009-05-19 11_26_11.trx
Sully_SULLYLAPTOP 2009-05-19 11_26_11
In
SULLYLAPTOP
Out
testproject.dll
TestProject.pdb
Sully_SULLYLAPTOP 2009-05-20 13_45_21.trx
Sully_SULLYLAPTOP 2009-05-20 13_45_21
In
SULLYLAPTOP
Out
testproject.dll
TestProject.pdb
TestSecurity.suo
TestSecurity.vsmdi
Mullivan.Web
bin
Debug
Mullivan.Web.dll
Images
calendar.gif
hSliderEnd.gif
hSliderMid.gif
hSliderTab.gif
vSliderEnd.gif
vSliderMid.gif
vSliderTab.gif
JS
Mullivan.snk
Properties
UI
Design
WebControls
WebControls
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Web.UI;
using Mullivan.Web.UI.Design.WebControls;
using System.Diagnostics;
using System.Web.UI.Design;

namespace Mullivan.Web.UI.WebControls
{
    [ToolboxData("<{0}:JQuerySlider  runat=\"server\"></{0}:JQuerySlider>"),
    Designer(typeof(JQuerySliderDesigner))]
    public class JQuerySlider : WebControl
    {
        private string _values = "25,40,50,75";
        private bool _initialized = false;

        public JQuerySlider() : base(HtmlTextWriterTag.Div) { }

        public enum JQuerySliderOrientation
        {
            //Auto = 0,
            Horizontal = 1,
            Vertical = 2
        }

        public event EventHandler SelectedValuesChanged;

        [Browsable(false)]
        public string ClientJQueryControlId
        {
            get
            {
                this.EnsureID();
                return "jq" + this.ClientID;
            }
        }

        public string ClientValuesControlId
        {
            get
            {
                return this.ClientJQueryControlId + "_Values";
            }
        }

        [Category("Behavior"),
         Browsable(true)]
        public bool Animate
        {
            get
            {
                return (this.ViewState["Animate"] != null ?
                    (bool)this.ViewState["Animate"]
                    : true);
            }
            set
            {
                this.ViewState["Animate"] = value;
            }
        }

        [Category("Behavior"),
         Browsable(true)]
        public int Maximum
        {
            get
            {
                return (this.ViewState["Maximum"] != null ?
                    (int)this.ViewState["Maximum"] 
                    : 100);
            }
            set
            {
                this.ViewState["Maximum"] = value;
            }
        }

        [Category("Behavior"),
         Browsable(true)]
        public int Minimum
        {
            get
            {
                return (this.ViewState["Minimum"] != null ?
                    (int)this.ViewState["Minimum"]
                    : 0);
            }
            set
            {
                this.ViewState["Minimum"] = value;
            }
        }

        [Category("Behavior"),
         Browsable(true)]
        public int Step
        {
            get
            {
                return (this.ViewState["Step"] != null ?
                    (int)this.ViewState["Step"]
                    : 1);
            }
            set
            {
                if(value > 0)
                    this.ViewState["Step"] = value;
            }
        }

        [Category("Behavior"),
         Browsable(true)]
        public int Value
        {
            get
            {
                int[] vals = this.Values;
                if (vals.Length < 1)
                {
                    return this.Minimum;
                }
                return vals[0];
            }
            set
            {
                this.Values = new int[1] { value };
            }
        }

        [Category("Behavior"),
         Browsable(true),
        TypeConverter(typeof(JQuerySliderValuesStringConverter)) ]
        public int[] Values
        {
            get
            {
                return ConvertValuesToArray(_values);
            }
            set
            {
                if (!_initialized)
                {
                    this._values = ConvertValueArrayToString(value);
                }
                else
                {
                    this._values = ConvertValueArrayToString(value);
                    OnSelectedValuesChanged(new EventArgs());
                }
            }
        }

        [Category("Appearance"),
         Browsable(true)]
        public override string CssClass
        {
            get
            {
                return (this.ViewState["CssClass"] != null ?
                    this.ViewState["CssClass"].ToString()
                    : string.Empty);
            }
            set
            {
                this.ViewState["CssClass"] = value;
            }
        }

        [Browsable(false)]
        public string ValuesAsString
        {
            get
            {
                return ConvertValueArrayToString(this.Values);
            }
        }

        [Category("Behavior"),
         Browsable(true)]
        public JQuerySliderOrientation Orientation
        {
            get
            {
                return (this.ViewState["Orientation"] != null ?
                    (JQuerySliderOrientation)this.ViewState["Orientation"]
                    : JQuerySliderOrientation.Horizontal);
            }
            set
            {
                this.ViewState["Orientation"] = value;
            }
        }

        [Category("Behavior"),
        Browsable(true)]
        public string OnClientSelectedValuesChanged
        {

            get
            {
                return (this.ViewState["OnClientSelectedValuesChanged"] != null ?
                    this.ViewState["OnClientSelectedValuesChanged"].ToString()
                    : null);
            }
            set
            {
                this.ViewState["OnClientSelectedValuesChanged"] = value;
            }
        }

        private void OnSelectedValuesChanged(EventArgs eventArgs)
        {
            if (SelectedValuesChanged != null)
                SelectedValuesChanged(this, eventArgs);
        }

        internal static string ConvertValueArrayToString(int[] values)
        {
            StringBuilder sb = new StringBuilder();
            foreach (int val in values)
            {
                if (sb.Length != 0)
                    sb.Append(",");
                sb.Append(val);
            }
            return sb.ToString();
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            this.EnsureID();

            this.Page.RegisterRequiresControlState(this);
            ClientScriptProxy p = ClientScriptProxy.Current;


            JQueryUtil.RegisterRequiredResources(p, this);

            #if DEBUG 
            p.RegisterClientScriptInclude(this, WebResources.JQUERY_JAVASCRIPT_UI_SLIDER, false);
            #endif

            if (this.Page.IsPostBack)
            {
                this.Values = ConvertValuesToArray(this.Page.Request.Form[ClientValuesControlId]);
            }

            this._initialized = true;
        }

        internal static int[] ConvertValuesToArray(string values)
        {
            string[] vals = values.Split(new char[1]{','}, StringSplitOptions.RemoveEmptyEntries);
            List<int> intVals = new List<int>();
            foreach (string val in vals)
            {
                int intVal = 0;
                if (int.TryParse(val, out intVal))
                    intVals.Add(intVal);
            }
          
            return intVals.ToArray();
        }

        protected override void OnPreRender(EventArgs e)
        {
            ClientScriptProxy p = ClientScriptProxy.Current;

            p.RegisterHiddenField(this, ClientValuesControlId, _values);

            string strSlideChanged = ClientJQueryControlId + "_SlideChanged";

            StringBuilder sbStartup = new StringBuilder();
            sbStartup.Append("var ");
            sbStartup.Append(ClientJQueryControlId);
            sbStartup.Append(" = jQuery('#");
            sbStartup.Append(this.ClientID);
            sbStartup.AppendLine("');");

            sbStartup.Append("var ");
            sbStartup.Append(ClientValuesControlId);
            sbStartup.Append(" = document.getElementById('");
            sbStartup.Append(ClientValuesControlId);
            sbStartup.AppendLine("');");

            sbStartup.Append("$(window).bind('load', ");
            sbStartup.Append("Init_");
            sbStartup.Append(ClientJQueryControlId);
            sbStartup.AppendLine(");");

            sbStartup.Append("function Init_");
            sbStartup.Append(ClientJQueryControlId);
            sbStartup.AppendLine("(){");
            sbStartup.Append(ClientJQueryControlId);
            sbStartup.AppendLine(".slider({ ");

            sbStartup.Append("animate:");
            sbStartup.AppendLine(this.Animate.ToString().ToLower());
            
            sbStartup.Append(",max:");
            sbStartup.AppendLine(this.Maximum.ToString().ToLower());
            
            sbStartup.Append(",min:");
            sbStartup.AppendLine(this.Minimum.ToString().ToLower());
            
            sbStartup.Append(",step:");
            sbStartup.AppendLine(this.Step.ToString());
            
            sbStartup.Append(",orientation:'");
            sbStartup.Append(this.Orientation.ToString().ToLower());
            sbStartup.AppendLine("'");

            sbStartup.Append(", values: [");
            sbStartup.Append(_values);
            sbStartup.AppendLine("]");

            sbStartup.AppendLine("});");


            sbStartup.Append(ClientJQueryControlId);
            sbStartup.Append(".bind('slidechange', function (event, ui){");
            sbStartup.Append(strSlideChanged);
            sbStartup.AppendLine("(event, ui);});");
            sbStartup.AppendLine("}");

            sbStartup.Append("function ");
            sbStartup.Append(strSlideChanged);
            sbStartup.AppendLine("(event, ui){");
            
            sbStartup.Append(@"
var strValues = '';
if(ui.values != null){
    for(var i = 0; i < ui.values.length; i++){
        if(i != 0)
            strValues += ',';
    
        strValues += ui.values[i];
    }
}

");
            sbStartup.Append(ClientValuesControlId);
            sbStartup.AppendLine(".value = strValues;");

            if(!string.IsNullOrEmpty(this.OnClientSelectedValuesChanged))
            {
                sbStartup.Append(this.OnClientSelectedValuesChanged);
                sbStartup.AppendLine("(event, ui);");
            }

            sbStartup.AppendLine("}");

            p.RegisterStartupScript(this.Page, typeof(WebResources), "_jsStartup__" + this.ClientID, sbStartup.ToString(), true);

            base.OnPreRender(e);

        }

        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            if (this.DesignMode)
            {
                string cssClass = string.Empty;
                if (!string.IsNullOrEmpty(this.CssClass))
                    cssClass = this.CssClass;

                writer.AddAttribute(HtmlTextWriterAttribute.Class,
                    string.Format(
                    "{0} ui-slider ui-slider-{1} ui-widget ui-widget-content ui-corner-all",
                    cssClass,
                    this.Orientation.ToString().ToLower()));

                writer.AddAttribute(DesignerRegion.DesignerRegionAttributeName, "0");
            }

            base.AddAttributesToRender(writer);
        }

        public class JQuerySliderValuesStringConverter : StringConverter
        {
            public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
            {
                if (value is string)
                {
                    string strValue = (string)value;
                    return JQuerySlider.ConvertValuesToArray(strValue);
                }
                return base.ConvertFrom(context, culture, value);
            }

            public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
            {
                if (value is int[])
                {
                    return JQuerySlider.ConvertValueArrayToString((int[])value);
                }

                return base.ConvertTo(context, culture, value, destinationType);
            }

            public override bool GetPropertiesSupported(ITypeDescriptorContext context)
            {
                return true;
            }
        }
    }
}

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)

About the Author

Sike Mullivan
Software Developer (Senior)
United States United States
I am a Sr SharePoint Developer in Saint Louis, MO. I've been developing professionally for about four years now. For three years I worked for a Document Imaging company that developed applications for Scanning, Indexing, Migrating, and Searching SharePoint (MOSS, WSS). I'm now working on a team for a Cable company that customizes their internal and external SharePoint implementations.

| Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 10 Jun 2009
Article Copyright 2009 by Sike Mullivan
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid