Mike Borozdin's Blog

A blog about programming, web and IT in general

Adding Client-Side Events To Extender Controls

In the previous articles I wrote about how to use the extender controls available in ASP.NET AJAX Control Toolkit, how to process them on the server, validate the data. It mainly dealt with a server-side part. Today, I'll show you how to add client-side events to the extender controls.

As an example, I'll use Slider again. But unlike the previous article where we processed its value on the server, we'll use its value directly on the page with JavaScript to resize an image, although it won't actually resize the picture, but it will just resize it on the page, i.e., just change the width and height attributes of the <img /> tag.

Let's start, add some image, two sliders for width and height, two text boxes and two labels.

<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
    Width:<br />
    <asp:TextBox ID="txtWidth" runat="server" />
    <asp:Label ID="lblWidth" runat="server" />
    <cc1:SliderExtender ID="sliderWidth" runat="server" TargetControlID="txtWidth"
        BoundControlID="lblWidth" Minimum="100" Maximum="800">
    </cc1:SliderExtender>
    <br />
    
    Height:<br />
    <asp:TextBox ID="txtHeight" runat="server" />
    <asp:Label ID="lblHeight" runat="serBver" />
    <cc1:SliderExtender ID="sliderHeight" runat="server" TargetControlID="txtHeight"
        BoundControlID="lblHeight" Minimum="100" Maximum="532">
    </cc1:SliderExtender>
    <br />
    
    <asp:Image ID="imgPanda" runat="server" ImageUrl="http://upload.wikimedia.org/wikipedia/commons/thumb/3/3c/Giant_Panda_2004-03-2.jpg/800px-Giant_Panda_2004-03-2.jpg" />
</div>
</form>

 

We want to resize the image on moving the slider, since when we move the slider handler, the actual value is being changed in the assigned TextBox, we should add the onChanged even to the TextBox.

 

protected void Page_Load(object sender, EventArgs e)
{
    txtWidth.Attributes.Add("onChange", "ChangeWidth()");
    txtHeight.Attributes.Add("onChange", "ChangeHeight()");
}

 

And add the JavaScript event handlers.

<script type="text/javascript">
    function ChangeWidth()
    {
        $get("imgPanda").width = $get("txtWidth").value;
    }
    
    function ChangeHeight()
    {
        $get("imgPanda").height = $get("txtHeight").value;
    }
</script>

By the way, please note the we are using the $get() shortcut here that actually invokes document.getElementById()

Let's test it. The image gets really resized but only when you release the button, but not when you just move the slider handle. This can be corrected by setting the RaiseChangeOnlyOnMouseUp to false.

<cc1:SliderExtender ID="sliderWidth" runat="server" TargetControlID="txtWidth"
    BoundControlID="lblWidth" Minimum="100" Maximum="800" RaiseChangeOnlyOnMouseUp="false">
</cc1:SliderExtender>
<%-- some other tags--%>
<cc1:SliderExtender ID="sliderHeight" runat="server" TargetControlID="txtHeight"
    BoundControlID="lblHeight" Minimum="100" Maximum="532" RaiseChangeOnlyOnMouseUp="false">
</cc1:SliderExtender>

Conclusion

In this tutorial we have learnt how to add how add client-side events to the extender controls, how to handle them. We have also learnt about the RaiseChangeOnlyOnMouseUp attribute of the Slider control.

Your feedback is appretiated.


Tags: ,
Posted by Mike Borozdin on Sunday, August 03, 2008 10:15 AM GMT
  Shout it Kick it!  
Permalink | Comments (3) | Post RSSRSS comment feed

Comments

DotNetKicks.com

Sunday, August 03, 2008 3:19 AM GMT

trackback

Trackback from DotNetKicks.com

Adding Client-Side Events To Extender Controls

Matthew J United States

Sunday, August 03, 2008 3:33 AM GMT

Matthew J

Very nice article.

It seems that you are a dedicated fan of the slider control. lol

Hamish Australia

Tuesday, March 31, 2009 11:51 PM GMT

Hamish

Thanks Mike - just what I needed.
I was then struggling however to work how how to have a vertical orientation where the 100 is at the top, and the 0 is down the bottom (like a volume control, for example).
It didn't seem to be possible so have had to go with a horizontal slider for now unfortunately.

Comments are closed