Language

Using the Slider Control With Auto-Postback (C#)

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 make the slider autopostback once its value changes.

Overview

The Slider control in the AJAX Control Toolkit provides a graphical slider that can be controlled using the mouse. It is possible to make the slider autopostback once its value changes.

Steps

In order to make the slider automatically postback upon a change, both text boxes need the attribute AutoPostBack="true": The text box that will become the slider itself, and the text box that holds the slider's position. Here is the required markup for that:

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

The SliderExtender control from the ASP.NET AJAX Control Toolkit assigns the slider functionality to the two text boxes:

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

An additional label element will later be used to inform the user of a postback:

<asp:Label ID="LastUpdate" runat="server" />

Finally, the ScriptManager control of ASP.NET AJAX loads the required JavaScript for the Control Toolkit to work:

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

Now the slider is posting back; on the server-side, this event may be caught and acted upon:

<script runat="server"> void Page_Load() { if (Page.IsPostBack) { LastUpdate.Text = "Last update: " + DateTime.Now.ToLongTimeString(); } } </script>

Moving the slider triggers a postback (Click to view full-size image)

Afterwards, the date of this change is written in the label (Click to view full-size image)

This article was originally created on June 2, 2008

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.