Server Intellect

Slider AJAX control in ASP.NET 2.0 and C#

Category: AJAX

Slider AJAX control in ASP.NET 2.0 and C#

Introduction
In this article I will explain how you can easily create an AJAX enabled web site within Visual Studio 2005 utilizing C# and how to utilize the AJAX framework to add a simple control from the list of controls available in the AJAX Control Toolkit.
The AJAX control we will be working with in this article will be the “Slider” control. We will also learn that the Slider control can be manipulated via its properties to display horizontal or vertical as well as allowing you to have incremental steps or a smooth non-step motion.

We chose Server Intellect for its dedicated servers, for our web hosting. They have managed to handle virtually everything for us, from start to finish. And their customer service is stellar.

This article expands on the previous article “DropShadow AJAX control in ASP.NET 2.0 and C#” and adds functionality to the Slider we added to the Panel in that example.
In the previous articles we learned the following about AJAX and what you can do with it?
“ASP.NET AJAX is a free framework for quickly creating efficient and interactive Web applications that work across all popular browsers.”

ASP.NET AJAX:

  • Create interfaces with AJAX components that can be reused.
  • Upgrade your existing pages with AJAX controls.
  • AJAX comes with support for all modern browsers
  • AJAX comes as part of Visual Studio 2008 and does not need to be downloaded and installed as in Visual Studio 2005.

What we will learn in this article:

  • How to Create an AJAX enabled Web Site
  • How to utilize the Slider control

Please Note:
AJAX functionality is integrated in ASP.NET 3.5 and does not require any additional downloads. If you are using ASP.NET 3.5 certain portions of this article may not apply to you.

Getting Started with our AJAX Application
Before we begin coding we will need to download the AJAX framework and install it onto our system in order to have the framework available for us to create our first AJAX web project.
In a previous article titled “Getting Started with AJAX, ASP.NET 2.0 and C#“ we went through the steps involved to install AJAX and how to AJAX enable Visual Studio. We explained where to go to find out about AJAX and that you could download step by step tutorials that will guide you through using every control within the AJAX Control Toolkit.
Here is the AJAX web site for you to add to your bookmarks in your favorite browser:
http://www.asp.net/

We migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect's help, we were able to avoid any headaches!

Continuing with our example:
We previously added a TextBox called “txtSlider” to the Panel in the previous example. That is the control we are extending with the SliderExtender.

<asp:TextBox ID="txtSlider" runat="server" Width="125px" AutoPostBack="True" OnTextChanged="txtSlider_TextChanged">1</asp:TextBox>

We now need to add another TextBox called “txtValue” which will display the current value of the Slider as you are moving it from side to side.

<asp:TextBox ID="txtValue" runat="server" Width="25px" AutoPostBack="True" ReadOnly="True"></asp:TextBox>

Please Note:

Both of these Textboxes have a property called AutoPostBack and that property is set to “True”. We need this property so the application will trigger the correct events to help us maintain the current value and to change the colors of the Panel as the bar is moving.

Setting the AutoPostBack to “True” will force the page to automatically reload as well for each Post Back. Because we are developing an AJAX web page we do not want to see any Post Back at all, which warrants one more addition to the previous example. The addition is an AJAX UpdatePanel control. We will move all of the controls within this UpdatePanel to prevent the visual effects of a PostBack.

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

<cc1:SliderExtender BehaviorID="txtSlider" ID="SliderExtender1" runat="server" BoundControlID="txtValue" TargetControlID="txtSlider" Minimum="1" Maximum="5" Steps="5" RaiseChangeOnlyOnMouseUp="True" />

<cc1:DropShadowExtender ID="DropShadowExtender1" runat="server" Rounded="True" TargetControlID="Panel1" TrackPosition="True" Width="8" />

<asp:Panel ID="Panel1" runat="server" BackColor="#00C0C0" Height="254px" Width="242px" >

<table style="left: 35px; width: 125px; position: relative; top: 25px">
<tr>
<td style="width: 195px; height: 29px">

<asp:TextBox ID="txtSlider" runat="server" Width="125px" AutoPostBack="True" OnTextChanged="txtSlider_TextChanged">1</asp:TextBox>

</td>
<td style="width: 195px; height: 29px">
<asp:TextBox ID="txtValue" runat="server" Width="25px" AutoPostBack="True" ReadOnly="True"></asp:TextBox>

</td>
</tr>
</table>
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>

We moved our web sites to Server Intellect and have found them to be incredibly professional. Their setup is very easy and we were up and running in no time.

Now that we have added our Textboxes we now need to set the remaining properties on the Slider control that we will be using for this example. We do that by adding the following entries into the Markup.

<cc1:SliderExtender ID="SliderExtender1" runat="server"
BehaviorID="txtSlider"
BoundControlID="txtValue"
TargetControlID="txtSlider"
Minimum="1"
Maximum="5"
Steps="5"
RaiseChangeOnlyOnMouseUp="True" />

The properties in the above code example do not reflect the entire list of properties available for this control. These are the properties that we are using for this example to give you an insight on how to use this control.

Slider Properties

  • BehaviorID="txtSlider"
    • This is the control that you are extending with the Slider control.
  • BoundControlID="txtValue"
    • ID of the TextBox or Label that dynamically displays the slider's value.
  • TargetControlID="txtSlider"
    • This is the control that you are extending with the Slider control.
  • Minimum="1"
    • This is the minimum value of the Slider control.
  • Maximum="5"
    • This is the maximum value of the Slider control.
  • Steps="5"
    • This is the number of discrete values within the Slider’s range.
  • RaiseChangeOnlyOnMouseUp="True"
    • If this property is set to true, the Slider fires the change event on the extended TextBox only when the left mouse button is released.

Need help with Windows Dedicated Hosting? Try Server Intellect. I'm a happy customer!

We need to create the Array of colors that we will be using for each value on the Slider. This is a simple “ArrayList” that has a length of 5. We will be adding this to the “Page_Load” event of the form and for simplicity and illustration purposes we will load it each time there is a PostBack [In a production application you will only want to load this one time and the page load event would not be where you would want this code]. We will also store this “ArrayList” within the Session so we will be able to retrieve at anytime.

protected void Page_Load(object sender, EventArgs e)
{
Session["Colors"] = new ArrayList();
((ArrayList)Session["Colors"]).Add(System.Drawing.Color.Blue);
((ArrayList)Session["Colors"]).Add(System.Drawing.Color.Green);
((ArrayList)Session["Colors"]).Add(System.Drawing.Color.Yellow);
((ArrayList)Session["Colors"]).Add(System.Drawing.Color.Red);
((ArrayList)Session["Colors"]).Add(System.Drawing.Color.Orange);
}

Now that the GUI is in place, and the Markup has been completed we need to add the Event that we will trigger each time we move the Slider to complete our code behind. That event is the “TextChanged” event for the “txtSlider” control.

protected void txtSlider_TextChanged(object sender, EventArgs e)
{
Random r = new Random();
int i = r.Next(5);
Panel1.BackColor = ((System.Drawing.Color)((ArrayList)Session["Colors"])[i]);
}

What our example application will look like

We have now covered all of the pertinent points of using the Slider control within the AJAX Control Toolkit. We are ready to run our example and see how the application works. When you move the Slider within this application the “BackColor” property of the Panel will change colors randomly.
The following screen shot will show what the application will look like.

I just signed up at Server Intellect and couldn't be more pleased with my Windows Server! Check it out and see for yourself.

Fig. 1

1

What we have Learned

We have learned that we can easily create an AJAX enabled web site and that we can add AJAX controls to it very easily. In our example we added the Slider control and explained how we can change the color of the Panel each time we move the Slider.

References

I use this site frequently and so should you, go to www.asp.net. Their reference material was very helpful when it came to researching this article.

Attachments



Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!

Leave a Comment

Comments on this Article

Post a Comment
Name:
Website:
Email:
Comments:

#1 anderson

Posted By: anderson | 12.11.2008 at 11:07 AM

if you define orientation as vertical, this component creates a scale so that negative numbers are up there and positive number ... down.. how to invert it??

#2 Instant Activation Hosting

Posted By: Instant Activation Hosting | 5.18.2009 at 7:03 AM

Very good tutorial thanks I love this stuff.However, when using a vertical slider,how can I ensure that the max value is shown on the Top not on the Bottom? Not very logical to increase a value by dragging a slider downwards now is it?

#3 Instant Activation Hosting

Posted By: Instant Activation Hosting | 6.05.2009 at 12:41 AM

Really that's a great site thanks for the sharing your site

#4 texas hold em poker online game

Posted By: texas hold em poker online game | 7.15.2009 at 6:39 AM

Great code, I hope, that it will help my in my project, but any way, you have done great work.

#5 ARE

Posted By: ARE | 10.07.2009 at 3:25 AM

Please Note:

Both of these Textboxes have a property called AutoPostBack and that property is set to “True”. We need this property so the application will trigger the correct events to help us maintain the current value and to change the colors of the Panel as the bar is moving.

Setting the AutoPostBack to “True” will force the page to automatically reload as well for each Post Back. Because we are developing an AJAX web page we do not want to see any Post Back at all, which warrants one more addition to the previous example. The addition is an AJAX UpdatePanel control. We will move all of the controls within this UpdatePanel to prevent the visual effects of a PostBack.

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

<ContentTemplate>

<cc1:SliderExtender BehaviorID="txtSlider" ID="SliderExtender1" runat="server" BoundControlID="txtValue" TargetControlID="txtSlider" Minimum="1" Maximum="5" Steps="5" RaiseChangeOnlyOnMouseUp="True" />

<cc1:DropShadowExtender ID="DropShadowExtender1" runat="server" Rounded="True" TargetControlID="Panel1" TrackPosition="True" Width="8" />

<asp:Panel ID="Panel1" runat="server" BackColor="#00C0C0" Height="254px" Width="242px" >

<table style="left: 35px; width: 125px; position: relative; top: 25px">

<tr>

<td style="width: 195px; height: 29px">

<asp:TextBox ID="txtSlider" runat="server" Width="125px" AutoPostBack="True" OnTextChanged="txtSlider_TextChanged">1</asp:TextBox>

</td>

<td style="width: 195px; height: 29px">

<asp:TextBox ID="txtValue" runat="server" Width="25px" AutoPostBack="True" ReadOnly="True"></asp:TextBox>

</td>

</tr>

</table>

</asp:Panel>

</ContentTemplate>

</asp:UpdatePanel>

We moved our web sites to Server Intellect and have found them to be incredibly professional. Their setup is very easy and we were up and running in no time.

Now that we have added our Textboxes we now need to set the remaining properties on the Slider control that we will be using for this example. We do that by adding the following entries into the Markup.

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

BehaviorID="txtSlider"

BoundControlID="txtValue"

TargetControlID="txtSlider"

Minimum="1"

Maximum="5"

Steps="5"

RaiseChangeOnlyOnMouseUp="True" />

The properties in the above code example do not reflect the entire list of properties available for this control. These are the properties that we are using for this example to give you an insight on how to use this control.

Slider Properties

BehaviorID="txtSlider"

This is the control that you are extending with the Slider control.

BoundControlID="txtValue"

ID of the TextBox or Label that dynamically displays the slider's value.

TargetControlID="txtSlider"

This is the control that you are extending with the Slider control.

Minimum="1"

This is the minimum value of the Slider control.

Maximum="5"

This is the maximum value of the Slider control.

Steps="5"

This is the number of discrete values within the Slider’s range.

RaiseChangeOnlyOnMouseUp="True"

If this property is set to true, the Slider fires the change ev

#6 Child Counseling Newport Beach

Posted By: Child Counseling Newport Beach | 12.08.2009 at 2:01 AM

This is very helpful for all the working professionals as well as students. Thanks for sharing the code details. :)

#7 Nidhi Solanki

Posted By: Nidhi Solanki | 1.12.2010 at 2:21 PM

hello, i like your post so much. I have one question it will be great if u can give some help. I am using Ajax control tool kit for slider. I want slider to move from 4 to 48 with 4,8,12,24,36,48 value. is this possible to achieve?

#8 ubaid

Posted By: ubaid | 2.23.2010 at 7:50 AM

i m satisfy from this website so helpfull for me it is very good web

#9 premium bid directory

Posted By: premium bid directory | 3.07.2010 at 3:43 AM

Such a cool post. Thank you for sharing this article..

#10 220-702

Posted By: 220-702 | 3.19.2010 at 4:34 AM

Great code, I hope, that it will help my in my project, but any way, you have done great work.

#11 testking 642-974

Posted By: testking 642-974 | 5.03.2010 at 5:37 AM

I like your blog very much.

#12 testking 642-974

Posted By: testking 642-974 | 5.03.2010 at 5:40 AM

Your blog is great.

#13 testking 646-671

Posted By: testking 646-671 | 5.03.2010 at 5:41 AM

Thanks for sharing.

#14 rat trap

Posted By: rat trap | 5.03.2010 at 7:03 AM

i like your post so much. I have one question it will be great if u can give some help. I am using Ajax control tool kit for slider. I want slider to move from 4 to 48 with 4,8,12,24,36,48 value. is this possible to achieve

#15 wholesale laptop adapter

Posted By: wholesale laptop adapter | 5.21.2010 at 1:52 AM

explain how you can easily create an AJAX enabled web site within Visual Studio 2005 utilizing C# and how to utilize the AJAX framework to add a simple control from the list of controls available in the AJAX Control Toolkit.

#16 coach handbags

Posted By: coach handbags | 5.24.2010 at 3:21 AM

The Far East aristocrat becomes fashion new influence … “the Russian always to choose best goods - - Chanel in the best brand the handbag and the coat,

#17 vibram five fingers

Posted By: vibram five fingers | 6.05.2010 at 9:32 AM

That great!Great article!useful That what i'm looking for!

#18 nike air max

Posted By: nike air max | 6.05.2010 at 9:34 AM

This article give me a great help!thank you very much!

#19 cheap mbt shoes

Posted By: cheap mbt shoes | 6.18.2010 at 9:00 PM

good post!!thank you

#20 grow taller 4 idiots blog

Posted By: grow taller 4 idiots blog | 6.23.2010 at 6:38 AM

This is a great inspiring article.I am pretty much pleased with your good work.You put really very helpful information. Keep it up. Keep blogging. Looking to reading your next post.

#21 links of london

Posted By: links of london | 7.07.2010 at 3:18 AM

If necklace must match the pendant , necklaces should not be too long or too short

#22 tiffany co

Posted By: tiffany co | 7.21.2010 at 2:52 AM

For classic and quality genuine silver jewelry, many people choose Tiffany & Co brand.Now,we present you the hottest Tiffany silver jewelry .get more http://www.tiffanyonsale.com/

#23 tiffany jewelry

Posted By: tiffany jewelry | 7.21.2010 at 2:52 AM

For classic and quality genuine silver jewelry, many people choose Tiffany & Co brand.Now,we present you the hottest Tiffany silver jewelry .get more http://www.tiffanyonsale.com/

#24 tiffany jewellery

Posted By: tiffany jewellery | 7.21.2010 at 2:52 AM

For classic and quality genuine silver jewelry, many people choose Tiffany & Co brand.Now,we present you the hottest Tiffany silver jewelry .get more http://www.tiffanyonsale.com/

#25 tiffany ring

Posted By: tiffany ring | 7.26.2010 at 7:00 AM

This is exactly cheap tiffany jewelry you can get cheap Tiffany Rings, Necklaces, Errings, Bracelets and other Tiffany Jewellery.get more http://www.tiffanyonsale.com/

#26 tiffany bracelet

Posted By: tiffany bracelet | 7.26.2010 at 7:00 AM

This is exactly cheap tiffany jewelry you can get cheap Tiffany Rings, Necklaces, Errings, Bracelets and other Tiffany Jewellery.get more http://www.tiffanyonsale.com/

#27 tiffany necklace

Posted By: tiffany necklace | 7.26.2010 at 7:00 AM

This is exactly cheap tiffany jewelry you can get cheap Tiffany Rings, Necklaces, Errings, Bracelets and other Tiffany Jewellery.get more http://www.tiffanyonsale.com/