Welcome to AspAdvice Sign in | Join | Help

Alessandro Gallo

.NET & Beyond
Implementing a Slider control with the Atlas framework

Though the Atlas framework is in its CTP stage, there are already many controls available for use. For example, the Agility team is doing a great job with the Atlas Control Toolkit, a set of Atlas controls that can be used through extenders.

On the other hand, some classic client-side controls like a menu, a calendar or a slider are missing at the moment (but if you have coded one using the Atlas framework, please let me know). For this reason, I've decided to implement the last of these three controls: the slider.

Download the code

Usage
The slider control follows the "classic" Atlas approach of upgrading a portion of static html into an Atlas control. The static html should have the following structure:

<div id="slider" class="sliderRail">
   <div id="handle" class="sliderHandle"><img src="Images/slider.handle.h.gif" alt="" /></div>
</div>

In the above code, we declared two layers: the first one is the "rail" or "line" container, and we should set its appearance through CSS and the background-image property (check the example that I've included in the archive). The other layer is declared as a child layer and represents the "handle" of the slider. In this case, while it would be better to set its appearance through the background-image property, I've used an img element to prevent an annoying IE issue that makes the image flicker if used through CSS (more info here). Once the static html has been declared, we can upgrade it into a Slider control with a declaration like the following, to be put in a xml-script section:

<slider id="horizSlider"
        handle="slider2"
        minimum="0"
        maximum="100"
        orientation="Horizontal"
        value="54"
        decimals="0"
        enableSlideAnimation="true"
        slideAnimationDuration="0.2"
        />

The slider control exposes the following properties (attributes in the declarative form):

 

  • id, this has to be set to the id of the layer that represents the slider's rail.
  • handle, has to be set to the id of the layer that represents the slider's handle.
  • minimum, the minimum value that can be assigned to the slider.
  • maximum, the maximum value that can be assigned to the slider.
  • orientation, can be Horizontal or Vertical.
  • value, used to get or set the slider's value.
  • decimals, specifies the number of decimal digits used to represent the slider's value when getting it.
  • steps, is used to obtain a slider with a "steps" movement instead of a continuous handle movement. The number of steps are specified setting this property.
  • enableHandleAnimation, if set to true causes the handle to be animated when setting the value by clicking with the mouse on the slider's rail.
  • handleAnimationDuration, specifies the duration of the handle animation, in seconds.

Finally, the control raises a valueChanged event every time the slider's value changes.

The problem: implementing a horizontal or vertical drag
One of the first thing that I've tried to implement when exploring the Atlas' drag and drop features was an horizontal or vertical drag. I thought that this would be an easy job, but then I found that the framework is totally responsible for setting the position of the control being dragged during a drag operation. So, no easy way to force an unidirectional dragging (or maybe you could have found an easy way to do that... in this case please email me). To work around this limitation, I had the idea to create an "invisible" drag handle: this drag handle is the object that the user is dragging around, but what the user sees moving is the "real" handle, i.e. the element specified through the handle property. Actually, the invisible handle is used to move the control that is supposed to be the real handle (if you browse through the code, you'll find a _initializeDragHandle method; try to change the style of the drag handle and you'll see what's happening). This has made possibile to implement an horizontal and vertical drag, and I've applied this concept to implement my slider control.

The code: a couple of interesting things
When building an Atlas control, the recommended approach is to "upgrade" some static html dropped on the page. To do this, is often necessary to supply elements' ids by setting control's attributes. The Atlas framework allows to directly reference a DOM element through its id, rather than passing its id and then getting the reference in the code. To do this, we have to simply declare a property and then specify in the getDescriptor method that that property will hold a reference to a DOM element. This feature has been used to specify the slider's handle:

td.addProperty('handle', Object, false, Sys.Attributes.Element, true);

Using the above code, the private member _handle will contain a reference to the corresponding DOM element.

 

The Sys.UI.LengthAnimation class, that is part of the visual effects framework defined in the AtlasUIGlitz.js file, allows to animate a property that is specified using a unit (for example, pixels). It allows to specify both the property to be animated and the unit to use (default is "px", i.e. pixels), and it's been used to implement the slide animation.

The Sys.UI.LayoutBehavior class, located in the AtlasUIGlitz.js file, allows to reference the layout properties of the associated control (left, top, height, width) through the corresponding properties, and it's been used in conjunction with the LengthAnimation class to implement the slide animation.

Conclusions
This was my first attempt to build a rather complex control using the Atlas framework, since I've also used animations and drag'n'drop features. This control is to be considered a work in progress at the moment, so be sure to often check this blog and the DotNetSlackers Community (where you'll find the code for all the releases) because I hope your feedback will drive the future releases of this control and help to improve it.

Posted: Friday, June 30, 2006 2:08 PM by Garbin

Comments

More Atlas stuff said:

Garbin has written a nice slider client control for Atlas and explains how he built it. Source code included.
# June 30, 2006 3:21 PM

Wayne Lambright said:

please consider offering a LIVE demo, some of us need to see it first before we invest the time to download, install and read.

chers

wayne
# July 1, 2006 12:08 AM

Garbin said:

Wayne, thanks for the suggestion, I'll try to setup a demo-page somewhere and will add the link to the post.
Garbin
# July 1, 2006 7:35 AM

Eric said:

Wayne,

Please consider making this available in a place whereyou don't need to login to download it.
# July 1, 2006 9:43 AM

The Coding Hillbilly said:

# July 1, 2006 2:39 PM

Tom Robinson said:

At first glance your implementation seems to lack the ability to degrade gracefully for browsers or users that can't support a graphical slider. Have you seen the WebFX slider (http://webfx.eae.net/dhtml/slider/slider.html) which 'upgrades' a standard text input control.
# July 1, 2006 3:08 PM

Garbin said:

Hi Tom, thanks for your suggestion, I'll consider it for the next release of the control.
Garbin
# July 4, 2006 3:35 AM

נרות said:

thanks
# July 6, 2006 9:58 AM

rich said:

The vertical slider works really well for what i need. Thanks. However i am unable to find a way to assoicate the scroll bar with a panel. The scroll bar works however it does not scroll the panel or div.

Can you provide an example or tips of how to do this.

thanks

Rich
# July 14, 2006 11:44 AM

Deepesh said:

thanks for the slider.
Can you help me out in making two way slider control to narrow down the values.
# July 21, 2006 2:07 AM

Deepesh said:

Can you help in binding the slider control with the values coming dynamic from the MS SQL database.
# July 21, 2006 2:55 AM

Garbin said:

rich: the scroll bar is actually an emulation, it is a slider with two arrow images that when clicked update the slider's value. No scroll events are being raised.

Deepesh: you can bind the slider control only on client side, for example using an Atlas binding and the slider's "value" property. Check the quickstarts for the details:
http://atlas.asp.net/docs/atlas/doc/data/default.aspx
# July 21, 2006 8:07 AM

Nutz said:

Is there a way to access the value of the control on postback, for serverside access and processing? Or maintain the viewstate of the slider?
# July 27, 2006 11:33 AM

Kori Francis said:

Hey garbin,

I found this on digg, javascript functionality for the mouse wheel. This could (imo) add a bit to your slider project ..

http://adomas.org/javascript-mouse-wheel/
# July 28, 2006 8:54 AM

Garbin said:

Nutz: you can wrap the slider with an Extender control. More info at http://atlas.asp.net/default.aspx?tabid=47&subtabid=477

Kori Francis: thanks for your suggestion, I'm considering it for one of the next releases.
# July 28, 2006 9:04 AM

KFrancis said:

Hey Garbin,

Extender created. Works in IE/Firefox.

http://www.project-atlas.com/ExtendedSlider.aspx

# July 28, 2006 8:31 PM

KFrancis said:

I've posted an AspAdvice article about the extender, which I already integrated the wheel support into.

View it here:
http://aspadvice.com/blogs/i_3_atlas/archive/2006/07/29/20063.aspx
# July 29, 2006 1:04 AM

Fcerqueira said:

Conforme anunciando em primeira m&#227;o em nosso blog j&#225; esta dispon&#237;vel uma nova vers&#227;o do ATLAS.Para os...
# August 3, 2006 10:44 AM

Deepesh Verma said:

hi,
I have implemented the dual silder on move of the silder I am binding the grid using sqldatasource.
As the slider moves I am taking the values in the hidden text box and calling the button1.Click event.The value of that hidden input is passed in SqlDataSource.Now the probelm is that While I move the silders the image of silder flickes a lot and the Page refreshes and the images are loaded again.
I dont want that.
How this can be done.
# August 4, 2006 6:48 AM

Garbin said:

Deepesh Verma: I don't know how you are implementing the "dual slider". My code uses an image element for the handle also to avoid the nasty flickering issue with CSS and IE6. For more informations, please check the following article by Scott Guthrie:
http://weblogs.asp.net/scottgu/archive/2006/05/29/Eliminating-CSS-Image-Flicker-with-IE6.aspx
# August 4, 2006 3:23 PM

fsyed said:

Hi Garbin
Cool stuff. I just want to one thing. I am able to bind the slider to datasource. But what I actually want is that on the valueChange event or drag movement, I want to be able to reload the datasource. I tried many things but nothing worked. Like in button control, we have click event, is this control has any event with which a javascript can be binded which in turn reloads the datasource.
# August 7, 2006 8:42 AM

scottt40 said:

Thanks KFrancis for your work on creating the sliderExtender making my life alot easier.  You web example does not work, there is some kind of server error.
# August 12, 2006 12:07 PM

Garbin said:

fsyed: the valueChanged event is raised for the purpose of attaching an handler that in turn could fire a postback or invoke a service method.
# August 12, 2006 2:31 PM

Kori Francis said:

scottt40: I'll fix that tonight, I was playing around with it on the weekend. Apparently I trashed the app doing so.

Garbin: Here's another suggestion .. another property that determines "which direction is positive". For example, in a vertical slider .. the positive direction could be either up or down. I'm also having some CSS problems with the handle being drawn off the div .. seems to be an offset problem .. as when the slider is at a 0 value, the handle is at the 50% mark. So when the value gets half way, the handle is off the track .. and it just keeps going. This seems to only happen when I don't specify the position of the horizontal divs as absolute ..
# August 14, 2006 1:59 PM

Garbin said:

Kori: thanks for the suggestion, but what's the purpose of deterimining a positive direction? You could set the slider's interval so that it's centered at zero (I mean, -50 : 50, for example).
Regarding the layout issues, I'm working on a new version of the slider that solves some problems with the box model. Also, be sure to set the handle element to have position:relative.
# August 17, 2006 5:22 AM

Qun Wang said:

Can anybody show me some tips to implement a dural slider?

# August 23, 2006 4:12 AM

Deepesh Verma said:

Qun Wang: visit this link

http://www.blueshoes.org/en/javascript/slider/

example 9 is important for you.

# August 25, 2006 8:17 AM

robinhood said:

Hi:

 I want to  bind a js method to silder

 how can i do?

thank you very much!

# August 27, 2006 3:19 AM

Garbin said:

robinhood: you can handle the valueChanged event raised by the slider. In the example of declaration that I gave in the post, you could add an attribute valueChanged="myValueChangedHandler" to call a js function called "myValueChangedHandler" every time the slider's value changes.

# August 27, 2006 1:52 PM

Atlas notes said:

I&#39;m pretty excited about it. The slider control evolved to a SliderExtender and seems ready to be

# September 12, 2006 6:59 AM

jxn said:

Garbin~

Would it be possible to store the current slider position in viewstate, and have the slider value default to that position on a full refresh?  I am using atlas components, but the page still requires a full refresh at times for reasons id rather not go into...  When i do a full refresh, the slider value is getting reset.  The previously stored value remains correct through the code, but when the page draws I lose the value and it gets reset to the default.  

Is this just a javascript change that needs to happen, so the default value is taken from the stored value?  What function would have to change?

You can probably tell I am a novice, so any help you could give is greatly appreciated!!!!!!

# September 12, 2006 12:46 PM

Garbin said:

jxn: I suggest you to use the slider extender included in the Control Toolkit (at the moment is in the development releases), because it extends an asp:TextBox, thus you can get and set values from the TextBox and they will be persisted across postbacks.

# September 12, 2006 1:04 PM

Zoli said:

Hi,

I tried this control and it's work fine, but I found a bug.

In that case puting a Slider Extender in a web user control, and then put this into a page it isn't working. When open the page a javascript error occurs.

I think the problem will be the control ID's filed because, if You put a control to a web user control, and then put this into a page, the control ID changes after the rendering. Get a Prefix tag which is the Web user Control Id.

Any idea to solve this bug?

Tnanks

# September 14, 2006 8:30 AM

Alan Le said:

I haven't seen an official announcement from the team yet, but it looks like a new release (marked...

# September 18, 2006 5:36 PM

jxn said:

Got the slider extender working perfectly and keeping its post-refresh value, but I had another question...  

How would I trigger an event when I stop moving the slider handle, such as when a mouseup event occurs.  Im a little confused because the slider is a text box, so I dont know how to trigger the event.  Right now hitting enter after changing the slider value triggers what happens next, it just needs to happen on a mouseup also.  

Thanks again!

# September 19, 2006 10:08 PM

Garbin said:

@jxn: this should be the purpose of the SlideEnd event that the slider behavior should raise when you stop moving the slider. Unfortunately, I've discovered a bug with this event after the control toolkit release, so I'll make sure to fix this for the next release. Thank you for reporting this.

# September 23, 2006 3:00 PM

Garbin said:

@Zoli: which JavaScript error are you getting?

# September 23, 2006 3:19 PM

jxn said:

garbin-

in IE, dragging down on a vertical slider causes the whole page to scroll.  In mozilla, this doesnt occur.  

is it possible to disalow the slider to scroll the page for IE?  how might I accomplish this?

thx again!

# October 14, 2006 12:44 AM

Garbin said:

@jxn: I'm investigating that, I'll let you know.

# October 14, 2006 2:38 PM

jxn said:

Garbin-

I have a slider in an update panel.  Also in this panel, an asp:panel gets dynamically sized and moved around through javascript css positioning based on events from other panels.  

When I use Internet Explorer, the height of this dynamically sizing/positioning panel is somehow being influenced by the text boxes for the slider extender, so that I cannot set the height for this element to be less than the height of the default slider value text box.  It is not a javascript problem, because the height is being passed correctly, just IE decides that it wont draw it the proper height.  

This problem does not occur in Mozilla/netscape.  

Setting the height settings and spacing settings for the update panel that contains the asp panel and slider extender does not help.  I have the width and height of the text boxes set to 0, so they shouldnt show up.  

Is there a setting somewhere inside your extender that may be causing this override of my panel's height attribute?  Even after setting the width and height of the text box to 0, the text box still appears briefly on the screen at its default height before changing into the slider images.  

Thanks alot!

# October 16, 2006 4:26 PM

jxn said:

Garbin-

False alarm there.  Turns out IE wont draw asp:panels smaller than a certain height, though Mozilla will.  Annoying, but fixed by using a blank asp:image instead.  

thx!

# October 16, 2006 5:35 PM

jxn said:

garbin-

have you found anything out regarding preventing the page from scrolling in IE when you drag the slider vertically?  Fitting the entire page so there are no scrollbars is not an option for me, which seems to be the only recommendation I can find.  

Thank you!

# November 1, 2006 2:23 PM

jxn said:

garbin-

have you found anything out regarding preventing the page from scrolling in IE when you drag the slider vertically?  Fitting the entire page so there are no scrollbars is not an option for me, which seems to be the only recommendation I can find.  

Thank you!

# November 1, 2006 2:31 PM

Garbin said:

jxn: this issue is related to the use of the GenericDragDropManager in IE. Now that the code has moved to the Toolkit I can investigate this and try to fix it for the next release.

# November 1, 2006 4:15 PM

jxn said:

Garbin-

Is there a way to disable dragging the handle in the meantime?  Turning the enableanimation property to false doesnt do it.  

Thanks!

# November 2, 2006 2:29 PM

jxn said:

garbin-

I have upgraded my toolkit and Ajax to Beta 2, and the new slider works great!  I can get it working perfect in a new standalone page.  

Im having a problem updating my existing application though.  I have created a new ajax control toolkit template project, copied my code in, and the slider behavior doesnt reflect the new toolkit.  Removed all old references and dlls, added them back in.  Even removed the slider code from the aspx, and recreated it.  Checked web.config to ensure correct toolkit is being referenced.  

The only way I can get it to work is to add a another slider at the end of the page, referencing textboxes that I add as well.  Doing this makes the slider that was working incorrectly work the way it should.  

Do you have any ideas/recommendations for what might be causing this/how to fix it?  Do I need to rebuild my page from scratch, adding one component in at a time, to make the new toolkit components work?  Could other components be reading the incorrect references as well?

Thanks for your help!!!!

# November 30, 2006 1:33 PM

jxn said:

garbin-

one note-  I dont have to use this workaround in Mozilla, only Internet Exploder.  

-jxn

# November 30, 2006 3:40 PM

jxn said:

garbin-

after deploying the new toolkit with slider to my web server, I am getting an error: Internet explorer cannot open the internet site xxx.  Operation aborted.  see www.themapstore.com/thewinemap using internet explorer.  then, choose wine tourist from the dropdown, and hit submit.  When the new page loads, the error will occur.  This error does not occur in Mozilla.  

this page seems to describe exactly what is happening:

http://weblogs.asp.net/infinitiesloop/archive/2006/11/02/Dealing-with-IE-_2600_quot_3B00_Operation-Aborted_2600_quot_3B002E00_-Or_2C00_-how-to-Crash-IE.aspx#comments

It seems to be occuring immediately as the slider changes from a text box to the slider images.  From what I have read this is maybe a problem with executing javascript before the page has been parsed?

Do you know what is causing this?  Do you know of any way to fix this?

Thank you!

# December 3, 2006 12:21 PM

jxn said:

this error goes away if I remove the Slider Extender.  This is the only control Im using from the control toolkit.  

thx

# December 3, 2006 1:25 PM

jxn said:

I get these javascript errors when loading the page the first time.  After that, the issue I describe in the previous post occurs.  

sys.invalidoperationexception: ScriptLoader.loadScripts cannot be called while the ScriptLoader

is already loading scripts.

then i get a sys.invalidoperationexception: Two components with the same id can't be added to the application

The id they refer to only exists once in the document, its an id of a collapsable panel extender, which the slider is not inside.  

maybe these errors could provide some clues...

# December 3, 2006 2:37 PM

Garbin said:

it's hard to say what's going on. If you manage to reproduce the error with a simple page, please use the contact form and I'll give a look at it.

# December 3, 2006 3:45 PM

Greg said:

I have created an update panel to surround a few components that need to appear and disappear when a linkbutton is selected, included in this panel is a slider.  When I change the values of the other components in the update panel , the ajax slider resets itself to 0, but displays the original value it was set to.  This appears to be only happening in IE (seems to be a running theme on this page).

Any help would be greatly appreciated.

# January 18, 2007 3:58 AM

Garbin said:

Greg: did you try to download the latest development release from CodePlex? That issue should be fixed. However, if you find a problem, it would be great if you could add a new work item on CodePlex, and maybe provide a simple repro; in this way I can easily track and fix it.

# January 18, 2007 4:39 AM

Greg said:

I am currently using 61214.  If there is a more recent release, could you please give me the URL?  Otherwise, I think I'll add a work item on CodePlex.

It's strange, I found the original work item which stated that it was closed.  Has this been tested for in IE 7?  

Thanks.

# January 19, 2007 2:46 AM

Garbin said:

Greg: you'll find the development releases here:

http://www.codeplex.com/AtlasControlToolkit/SourceControl/ListDownloadableCommits.aspx

If the original work item is closed, then the fix should appear in the next release (probably the next week).

# January 19, 2007 3:10 AM

Frank Patton said:

Hi Garbin!

Can you tell me where I can find either an example in the toolkit or in your 0.8 zip of last June 30th the following: I am using the December ctp .. hope all is well .. Frank

RailCssClass             - CSS class for the slider's rail.

HandleCssClass           - CSS class for the slider's handle.

HandleImageURL           - URL of the image to display as the slider's handle.

# January 20, 2007 2:15 PM

Frank Patton said:

Maximum="100"

Minimum="0"

length="375"

Value="35"

When I put the value item in the slider definition, unlike the maximum, minimum, and length properties, I get a not valid attribute of element message.

What I would like to do is to be able to format both the handle and the slider using css or "element.style.etc etc", set the begining value, capture the ending value, and change the visibility between visible and hidden, all using javscript. Am I barking up the wrong tree?

Go Bears!!

Frnk from Chicago

# January 21, 2007 7:46 AM

yuha said:

I understand that Garbin has marked the related bug as fixed and stated that there was no need to add a DefaultValue property back in Dec 2006. But actually it still doesn't work in the latest release (10618). When TextBox (BoundControl)'s value is updated, the slider just won't move. Here is a copy of my code on client asp page:

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

<ContentTemplate>

<asp:TextBox ID="Slider1" runat="server" style="right:0px" Text="0" Width="256px"/>

<asp:TextBox ID="Slider1_BoundControl" runat="server" Width="100px" Text="120"/>

<cc1:sliderextender ID="SliderExtender1" runat="server"

TargetControlID="Slider1"

Length="260"

Maximum="100"

Minimum="0"

BehaviorID="Slider1"

BoundControlID="Slider1_BoundControl"

Orientation="Horizontal"

EnableHandleAnimation="true"

TooltipText="Please slide to change the value."

Decimals="2"

/>

</ContentTemplate>

</asp:UpdatePanel>

on the server Page_Load(Object sender, EventArgs e) method, I simply did this:

Slider1_BoundControl.Text = "30";

As I tried so many time, when page is refeshed/reloaded, the Slider_BoundControl value briefly (in several miliseconds) gets set to value 30 then back to 0 , hence the slider did not move at all. It seems that the value was set initialy but the subsequent action voids the previous action and resets the value back to 0.

# July 25, 2007 10:18 AM

Garbin said:

Hi,

the default value should be set on the extended textbox. The bound textbox is supposed to be used for dynamically changing the slider value on the client side.

# August 7, 2007 6:16 AM

TopCoder said:

Is there a way to freeze the slider from moving? If you wanted to show a read only version of a page that included a slider.

# August 23, 2007 12:59 AM

Garbin said:

Hi,

at present there's no way to make the slider read-only. This is in the list of planned features.

# August 23, 2007 5:24 AM
New Comments to this post are disabled