Language

Databinding the Slider Control (VB)

By Christian Wenz|

Download Code or Download PDF

The Slider control in the AJAX Control Toolkit provides a graphical slider that can be controlled using the mouse. It is possible to bind the current position of the slider to another ASP.NET control.

Overview

The Slider control in the AJAX Control Toolkit provides a graphical slider that can be controlled using the mouse. It is possible to bind the current position of the slider to another ASP.NET control.

Steps

In order to activate the functionality of ASP.NET AJAX and the Control Toolkit, the ScriptManager control must be put anywhere on the page (but within the <form> element):

<asp:ScriptManager ID="asm" runat="server" />

Next, add two TextBox controls to the page. One will be transformed into a graphical slider, and the other one will hold the position of the slider.

<asp:TextBox ID="Slider1" runat="server" /> <asp:TextBox ID="SliderValue" runat="server" />

The next step is already the final step. The SliderExtender control from the ASP.NET AJAX Control Toolkit makes a slider out of the first text box and automatically updates the second text box when the slider position changes. In order for that to work, The SliderExtender's TargetControlID attribute must be set to the ID of the first text box; the BoundControlID attribute must be set to the ID of the second text box.

<ajaxToolkit:SliderExtender ID="se1" runat="server" TargetControlID="Slider1" BoundControlID="SliderValue" />

As you can see in the browser, the data binding works in both directions: entering a new value in the text box updates the slider's position. If you make the second text box read only, you may add a weak protection to the text field so that it is harder for the user to manually update the value in there.

Slider and text box are in sync (Click to view full-size image)

Author Information

Christian Wenz

Christian Wenz – Christian Wenz is an author, trainer, and consultant. His main focus of working and writing is on web technologies and security. Christian has written or co-written over 100 books for various publishers. He works with both open source and closed source web technologies. This leads to the unusual situation that he has both been awarded a Microsoft MVP for ASP/ASP.NET and is listed in Zend's Who is Who of PHP. He is also listed in Mozilla's credits (about:credits) and is considered an expert in browser-agnostic JavaScript.