您可以创建一个呈现javascript的控件:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
namespace WebApplication1
{
[DefaultProperty("EffectType"), ToolboxData("<{0}:jQueryAnimation runat=server></{0}:jQueryAnimation>")]
public class jQueryAnimation : System.Web.UI.WebControls.WebControl
{
private const string SCRIPT_KEY = "jQueryAnimation";
[Bindable(true), Category("Appearance"), DefaultValue("Bounce")]
public string EffectType { get; set; }
public string ControlId { get; set; }
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
RegisterAnimationScript();
}
protected override void RenderContents(HtmlTextWriter writer)
{
base.RenderContents(writer);
}
private void RegisterAnimationScript()
{
if (!Page.ClientScript.IsClientScriptBlockRegistered(SCRIPT_KEY))
{
StringBuilder script = new StringBuilder();
script.Append("<script type= text/javascript >");
script.Append(Environment.NewLine);
script.Append("$(document).ready(function () {");
script.Append(Environment.NewLine);
script.Append("var options = {};");
script.Append(Environment.NewLine);
script.Append("$( #");
script.Append(this.ControlId);
script.Append(" ).effect( ");
script.Append(this.EffectType);
script.Append(" , options, 1500, ");
script.Append(this.ControlId);
script.Append("_callback);");
script.Append(Environment.NewLine);
script.Append("function ");
script.Append(this.ControlId);
script.Append("_callback() {");
script.Append(Environment.NewLine);
script.Append("setTimeout(function() {");
script.Append("$( #");
script.Append(this.ControlId);
script.Append(" ).removeAttr( style ).hide().fadeIn();");
script.Append("}, 1000 );};");
script.Append(Environment.NewLine);
script.Append("});");
script.Append(Environment.NewLine);
script.Append("</script>");
Page.ClientScript.RegisterClientScriptBlock(Page.GetType(),SCRIPT_KEY,script.ToString());
}
}
}
}
I have defined a property EffectType which is the effect name you want to execute; ControlId is the id of the HTML element you want to animate.
RegisterAnimationScript outputs the javascript to do the animation.
In your ASPX you have to register the control
<%@ Register assembly="WebApplication1" namespace="WebApplication1" tagprefix="cc1" %>
然后放下你的控件(它应该出现在你的工具箱中)
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.9/jquery-ui.min.js"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<div id="test" style="width:100px;height:100px;background-color:Red"> </div>
<cc1:jQueryAnimation ID="jQueryAnimation1" runat="server" EffectType="Shake" ControlId="test"></cc1:jQueryAnimation>
</div>
</form>
</body>
</html>
Here, I ve bound my serer-control jQueryAnimation to the div test (ControlId="test"
) and I ve defined the effect I want to apply (EffectType="Shake"
).
It s not very easy to manage javascript in C#, though ;-)