I found a really nice jQuery slider control that I incorporated into an ASP.NET Custom Control. My apologies, though, that I can’t find where I got this code. It may be just the jQuery UI slider.
I have added the following properties:
- Initial Value
- Max Value
- Min Value
- Increment
- Selected Value
This allows you to specify a large range of values, like 0 to 100, but then you can set an increment of 5 or 10 so that there are only 10 or 20 values to select from rather than 100. For instance, if you were having the user select a rotation value, you could have 0 to 360 degrees, but 360 values on a slider is a bit too much. So, you set the increment to 15, and they have 24 values for which they can actually see a noticeable difference.
The Initial Value is just that–it lets you set the default. Use the Selected Value to get the value that was selected.

Anyway, here’s the code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
namespace Components
{
public class Slider: System.Web.UI.WebControls.WebControl, INamingContainer
{
TextBox txtValue;
#region Properties...
public int InitialValue
{
get
{
object o = ViewState["InitialValue"];
if (o == null)
return 0;
else
return (int) o;
}
set
{
EnsureChildControls();
ViewState["InitialValue"] = value;
txtValue.Text = value.ToString();
}
}
public int MaxValue
{
get
{
object o = ViewState["MaxValue"];
if (o == null)
return 0;
else
return (int)o;
}
set
{
ViewState["MaxValue"] = value;
}
}
public int MinValue
{
get
{
object o = ViewState["MinValue"];
if (o == null)
return 0;
else
return (int)o;
}
set
{
ViewState["MinValue"] = value;
}
}
public int Increment
{
get
{
object o = ViewState["Increment"];
if (o == null)
return 1;
else
return (int)o;
}
set
{
ViewState["Increment"] = value;
}
}
public int SelectedValue
{
get
{
return int.Parse(txtValue.Text);
}
}
#endregion
protected override void CreateChildControls()
{
base.CreateChildControls();
txtValue = new TextBox();
txtValue.ID = "txtValue";
txtValue.Text = InitialValue.ToString();
Controls.Add(txtValue);
}
protected override void Render(HtmlTextWriter htw)
{
EnsureChildControls();
DataAccess.Organization org = DataAccess.Organization.Get(Components.Classes.User.OrgID);
OutputLine(htw, IndentionType.IncreaseIndent, "<div>");
OutputLine(htw, IndentionType.NoIndent, "<script type=\"text/javascript\">");
OutputLine(htw, IndentionType.IncreaseIndent, "$(function() { FLTK.slidercontrol.init(\"" + this.ID + "\"," + MaxValue + "," + MinValue + "," + Increment + ");});");
OutputLine(htw, IndentionType.DescreaseIndent, "</script>");
OutputLine(htw, IndentionType.NoIndent, "<div id=\"" + this.ID + "\" class=\"ui-slider-container\" width=\"" + this.Width + "\">");
OutputLine(htw, IndentionType.IncreaseIndent, "<div class=\"sliderdisplayvalue\"></div>");
OutputLine(htw, IndentionType.NoIndent, "<div class=\"slider\"></div>");
OutputLine(htw, IndentionType.NoIndent, "<div class=\"slidervalue\">");
OutputLine(htw, IndentionType.IncreaseIndent, txtValue);
OutputLine(htw, IndentionType.DescreaseIndent, "</div>");
OutputLine(htw, IndentionType.DescreaseIndent, "</div>");
OutputLine(htw, IndentionType.DescreaseIndent, "</div>");
}
#region Output Routines...
private void OutputLine(HtmlTextWriter output, IndentionType indention, string text)
{
if (indention == IndentionType.IncreaseIndent)
output.Indent++;
else if (indention == IndentionType.DescreaseIndent)
output.Indent--;
output.WriteLine(text);
}
private void OutputLine(HtmlTextWriter output, IndentionType indention, Control control)
{
if (indention == IndentionType.IncreaseIndent)
output.Indent++;
else if (indention == IndentionType.DescreaseIndent)
output.Indent--;
try
{
control.RenderControl(output);
}
catch
{
output.WriteLine("*************************<BR>");
}
}
private void OutputLine(HtmlTextWriter output, IndentionType indentionBefore, IndentionType indentionAfter, Control control)
{
if (indentionBefore == IndentionType.IncreaseIndent)
output.Indent++;
else if (indentionBefore == IndentionType.DescreaseIndent)
output.Indent--;
try
{
control.RenderControl(output);
}
catch
{
output.WriteLine("*******<BR>");
}
if (indentionAfter == IndentionType.IncreaseIndent)
output.Indent++;
else if (indentionAfter == IndentionType.DescreaseIndent)
output.Indent--;
}
private enum IndentionType
{
NoIndent = 0,
IncreaseIndent = 1,
DescreaseIndent = 2
}
#endregion
}
}
The JavaScript that makes this work is here:
slidercontrol = {
init: function (cntrl, maxValue, minValue, step) {
var sliderCntrl = $("#" + cntrl);
var thisDiv = sliderCntrl.find(".ui-slider-container");
var currVal = sliderCntrl.find(".slidervalue :input").val();
sliderCntrl.find(".slider").slider(
{
value: currVal,
max: maxValue,
min: minValue,
step: step,
change: function (event, ui) { FLTK.slidercontrol.updateSlider(sliderCntrl, ui.value); },
slide: function (event, ui) { FLTK.slidercontrol.updateSlider(sliderCntrl, ui.value); }
});
slidercontrol.updateSlider(sliderCntrl, currVal);
},
updateSlider: function (cntrl, value) {
cntrl.find(".slidervalue :input").val(value);
cntrl.find(".sliderdisplayvalue").text(value);
}
}
And here’s my CSS:
/* Slider */
.ui-slider { position: relative; text-align: left; }
.ui-slider .ui-slider-handle { position: absolute; z-index: 2; width: 1.25em; height: 1.5em; cursor: default; }
.ui-slider .ui-slider-range { position: absolute; z-index: 1; font-size: .7em; display: block; border: 0; background-position: 0 0; }
.ui-slider .ui-state-default { height: 1.7em; }
.ui-slider-standard-width { width: 400px; }
.ui-slider-container { }
.ui-slider-container .slider { margin-left: 25px; padding-top: 3px;}
.ui-slider-container .slidervalue { display: none; }
.ui-slider-container .sliderdisplayvalue { float:left; text-align: right; }
.ui-slider-horizontal { height: 1.2em; }
.ui-slider-horizontal .ui-slider-handle { top: -.3em; margin-left: -.6em; }
.ui-slider-horizontal .ui-slider-range { top: 0; height: 100%; }
.ui-slider-horizontal .ui-slider-range-min { left: 0; }
.ui-slider-horizontal .ui-slider-range-max { right: 0; }
.ui-slider-vertical { width: .8em; height: 100px; }
.ui-slider-vertical .ui-slider-handle { left: -.3em; margin-left: 0; margin-bottom: -.6em; }
.ui-slider-vertical .ui-slider-range { left: 0; width: 100%; }
.ui-slider-vertical .ui-slider-range-min { bottom: 0; }
.ui-slider-vertical .ui-slider-range-max { top: 0; }
I use this on my Custom ASP.NET Chart Control to specify a variety of options.