Welcome to AspAdvice Sign in | Join | Help

Extending Garbin's Slider Control (MouseWheel too!)

So, a while back, AspAdvice's own Garbin posted an article "Implementing a Slider control with the Atlas framework" and it was a really well written article. Basically he had developed a slider control using the Atlas framework.

I decided to take his work even further, and hopefully that these changes make it into a future release of the AtlasControlTookit. I'm not going to explain the "down and dirty" details of how the slider works, because Garbin's article already explains it very well.

I created a control extender, which uses Garbin's slider functionality with some additional features (LIKE WHEEL MOUSE SUPPORT). It also works well in IE/Firefox. If someone wanted to test other browsers, let me know. I'll be happy to make any changes.

LIVE DEMO!
Source Code

Updated on August 29: Cleaned up the code for event handling.

Note: The Slider that this is based off of was recently introduced into the AtlasControlToolkit by Garbin.

To use:
First, register the control for the page:


<%@ Register Assembly="Slider" Namespace="Slider" TagPrefix="foo" %>


Then, there are only really two elements to a slider. The slider, and the handle. These can be defined on the page like this:

    <asp:Panel ID="verticalSlider" runat="server">
        <div id="verticalSliderHandle">
            <img src="..." />
        </div>
    </asp:Panel>


Then, add the extender to the page:

    <foo:SliderExtender ID="sliderExtender1" runat="server">
        <foo:SliderProperties ID="verticalBehavior"
                                        TargetControl="verticalSlider"
                                        DragHandleID="verticalSliderHandle"
                                        MinimumValue="0"
                                        MaximumValue="100"
                                        Orientation="Vertical"
                                        Value="50"
                                        DecimalCount="0"
                                        EnableHandleAnimation="true"
                                        HandleAnimationDuration="0.2"
                                        TextBoxControlID="txtVerticalValue"
                                        ValueChangedScript="someFunction" />
    </foo:SliderExtender>

Let me explain the properties:

ID - This is the ID of the behavior, which you can use to access the extended behavior. For example, if you
     a button that you wanted to update the value on the client, you could use the following block:

    <script type="text/javascript" language="javascript">
        function btnUpdateVertical_Click(sender, e) {
            var slider = $object('verticalBehavior');
            slider.set_value(20);
        }
    </script>

TargetControl - This is the ID of the panel that will show the slider 'bar'.

DragHandleID - This is the ID of the DOM element that contains the slider 'handle'.

MinimumValue/MaximumValue - These are the limits of the slider.

Orientation - This is an enumerated property, where the slider can be set to "Horizonal" or "Vertical" modes.

Value - This is the value the slider will show when it is rendered.

DecimalCount - This is the number of significant digits that the slider will use in its value.

EnableHandleAnimation - This is a boolean setting, that enables/disables the "dragging of the slider handle".

HandleAnimationDuration - This is the amount of time that the handle takes to move between values. For example, if you click on the slider where the handle isn't, it will move to the clicked spot in 'handleAnimationDuration' amount of time.

TextBoxControlID - If you want to bind the value of the slider to the value in a textbox (a two way binding), then use this property. The extender wont try and bind if this property is not set. Then whenever the slider moves, the text will reflect the value of the slider.
       
Because I completely create the Atlas control wrapper internally, there's no existing one on the page for you to use (unless you add it through xml-script). But if you don't want to add it, and still want the value, then just access the get_bindingControlText() method which will return the text if any. For example:

            <script type="text/javascript" language="javascript">
                function btnUpdateVertical_Click(sender, e) {
                    var slider = $object('verticalBehavior');
                    slider.set_value(slider.get_bindingControlText());
                }
            </script>

           Which will set the slider to the value in the bound textbox.

ValueChangedScript - This is the javascript that will be called when the value of the slider is changed. For example:

        <script type="text/javascript" language="javascript">
            function someFunction(e) {
                // Get the value and do something with it.
                debug.trace(e.get_value());
            }
        </script>

Now. For mouse wheel control, this page describes the main method used .. but I had to slightly modify it to use the atlas method of attaching events.

The following block of code attaches a method delegate to the event of the control:

var _wheelHandler = Function.createDelegate(this, onMouseWheel);

if (this.control.element.addEventListener) {
    /* For Mozilla */
    this.control.element.addEventListener('DOMMouseScroll', _wheelHandler, false);
} else {
    /* For IE/Opera */
    this.control.element.attachEvent('onmousewheel', _wheelHandler);
}

And that works perfectly fine.
Published Saturday, July 29, 2006 12:48 AM by KFrancis

Comments

# re: Extending Garbin's Slider Control (MouseWheel too!)

Kori: well done! I'm also working on an extender for the Slider, more info soon (on the Atlas Control Toolkit project).
Saturday, July 29, 2006 3:23 AM by Garbin

# re: Extending Garbin's Slider Control (MouseWheel too!)

Hi, I am using your designed SliderExtender. From the javascript that is called on ValueChanged Event, I am trying to access an object in the page. I am using Atlas Client side coding. But I am unable to do so. Can you help me in doing so?
Monday, August 07, 2006 7:34 AM by fsyed

# re: slider help

sure. If you want email me some more specifics, then I'm sure we can sort it out.

I think, if we're talking about the sample page I included .. the "someValueChanged" function is the same function you are working with right? The function that is called when the slider value is changed?

If so, did you first give the extender property an ID? It's not a property that is too well known about, but its there. Its used to give an identifying name to a behavior .. only once the behavior is named (on the property) can you use the $object(<behaviorID>); construct ..

Just let me know how I can help ..
Monday, August 07, 2006 5:40 PM by KFrancis

# re: Extending Garbin's Slider Control (MouseWheel too!)

Hi again, This is how I have defined the extender. It is same as you have mentioned.
<%-- Horizontal slider with slide animation --%> <%-- Horizontal slider with slide animation --%>
This is the javascript function that I have ben working on. function someValueChanged(e) { //alert(e.get_value()); //var slider = $object('vertSliderBeh'); var listsSource = $object('sqlDataSource'); //var textb = $('SliderValue'); var dataItem = e.get_value(); //alert(dataItem); listsSource.get_parameters()["priceRange"] = dataItem; listsSource.load(); //textb.value = dataItem; } This is the datasource object that I have defined in Atlas script. Now when I load the page, the binding works fine. But when i try to access dataSource object from the javascript funtion that is called onValueChanged, it is unable to access it. I tried to access a control from normal Html layout too. You can see I have defined . But I am unable to access that control too. So this is the problem I am facing. I could find your email address to send this as email. If you can tell me, then I can send the detailed code to you. Thanks
Thursday, August 10, 2006 1:34 AM by fsyed

# re: Extending Garbin's Slider Control (MouseWheel too!)

Hi, I believe you may be missing a couple of finishing touches in your code. First, if I have a lot of content in the page and I use the mouse wheel, it wheels the slider, but then it continues to wheel/scroll right on down the page. To fix this, I added "e.returnValue = false;" to your _wheelHandler method. Also, I don't see where you are detaching the DOMMouseScroll event in the dispose method. I see where you are detaching the "onmousewheel" event. Shouldn't you do both, depending on the browser?
Tuesday, August 29, 2006 5:23 PM by billyzkid

# re: Extending Garbin's Slider Control (MouseWheel too!)

Thanks billyzkid, I've added those changes. Just letting you know though, that "a version" of the slider behavior has been added to the AtlasControlToolkit by Garbin .. he doesn't use the wheel event (just yet .. heh). 

I updated the site with those changes, the source has been updated as well. 

Thanks for the comment!

Tuesday, August 29, 2006 7:51 PM by KFrancis
Anonymous comments are disabled