"
ASP.NET (snapshot 2017) Microsoft documentation and samples

Changing an Animation Using Client-Side Code (C#)

by Christian Wenz

Download Code or Download PDF

The Animation control in the ASP.NET AJAX Control Toolkit is not just a control but a whole framework to add animations to a control. The animation can also be changed using custom client-side JavaScript code.

Overview

The Animation control in the ASP.NET AJAX Control Toolkit is not just a control but a whole framework to add animations to a control. The animation can also be changed using custom client-side JavaScript code.

Steps

First of all, include the ScriptManager in the page; then, the ASP.NET AJAX library is loaded, making it possible to use the Control Toolkit:

[!code-aspxMain]

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

The animation will be applied to a panel of text which looks like this:

[!code-aspxMain]

   1:  <asp:Panel ID="panelShadow" runat="server" CssClass="panelClass">
   2:   ASP.NET AJAX is a free framework for quickly creating a new generation of  
   3:   more efficient, more interactive and highly-personalized Web experiences 
   4:   that work across all the most popular browsers.<br />
   5:   ASP.NET AJAX is a free framework for quickly creating a new generation of  
   6:   more efficient, more interactive and highly-personalized Web experiences 
   7:   that work across all the most popular browsers.<br />
   8:   ASP.NET AJAX is a free framework for quickly creating a new generation of 
   9:   more efficient, more interactive and highly-personalized Web experiences 
  10:   that work across all the most popular browsers.<br />
  11:  </asp:Panel>

In the associated CSS class for the panel, define a nice background color and also set a fixed width for the panel:

[!code-cssMain]

   1:  <style type="text/css">
   2:   .panelClass {background-color: lime; width: 300px;}
   3:  </style>

The actual animation is launched by an HTML button:

[!code-aspxMain]

   1:  <input type="button" id="Button1" runat="server" value="Launch Animation" />

Then, add the AnimationExtender to the page, providing an ID, the TargetControlID attribute and the obligatory runat="server":

[!code-aspxMain]

   1:  <ajaxToolkit:AnimationExtender ID="ae" runat="server" TargetControlID="Button1" />

Note that there is no <Animations> node within the AnimationExtender control. Custom JavaScript code is used to provide the animations to be used with the control.

As with the server API of AnimationExtender, there is no easy way to assign an animation to the extender yet. However the extender does expose several methods to read and write animations registered with the various events (OnClick, OnLoad, and so on). Here are some examples:

The format of the return value of the get_*() functions and the format of the argument for the set_*() functions is a JSON string, providing an object representation of what the XML markup would be. Currently, there is no way to pass an object in, but it is possible to read an object from a given animation (get_OnXXXBehavior() methods).

Here is a JSON string (without the delimiting quotes and formatted nicely) representing an animation triggered by the button, but animating the panel by resizing it and fading it out at the same time:

[!code-jsonMain]

   1:  {
   2:   "AnimationName":"Sequence",
   3:   "AnimationChildren":[
   4:   {
   5:   "AnimationName":"EnableAction",
   6:   "Enabled":"false",
   7:   "AnimationChildren":[]
   8:   },
   9:   {
  10:   "AnimationName":"Parallel",
  11:   "AnimationChildren":[
  12:   {
  13:   "AnimationName":"FadeOut",
  14:   "Duration":"1.5",
  15:   "Fps":"24",
  16:   "AnimationTarget":"Panel1",
  17:   "AnimationChildren":[]
  18:   },
  19:   {
  20:   "AnimationName":"Resize",
  21:   "Width":"1000",
  22:   "Height":"150",
  23:   "Unit":"px",
  24:   "AnimationTarget":"Panel1",
  25:   "AnimationChildren":[]
  26:   }]
  27:   }]
  28:  }

The following JavaScript code assigns this JSON descripting to the OnClick animation of the current extender and runs it:

[!code-htmlMain]

   1:  <script type="text/javascript">
   2:   function pageLoad() 
   3:   {
   4:   var ae = $find("ae");
   5:   var animation = '{"AnimationName":"Sequence","AnimationChildren":[{"AnimationName":"EnableAction","Enabled":"false","AnimationChildren":[]},{"AnimationName":"Parallel","AnimationChildren":[{"AnimationName":"FadeOut","Duration":"1.5","Fps":"24","AnimationTarget":"Panel1","AnimationChildren":[]},{"AnimationName":"Resize","Width":"1000","Height":"150","Unit":"px","AnimationTarget":"Panel1","AnimationChildren":[]}]}]}';
   6:   ae.set_OnClick(animation);
   7:   ae.OnClick();
   8:   }
   9:  </script>

The animation runs immediately, without a mouse click (and with very little markup)

The animation runs immediately, without a mouse click (and with very little markup) (Click to view full-size image)

Previous Next



Comments ( )
Link to this page: //www.vb-net.com/AspNet-DocAndSamples-2017/aspnet/web-forms/overview/ajax-control-toolkit/animation/changing-an-animation-using-client-side-code-cs.htm
< THANKS ME>