To keep JavaScript code from being stolen, we can compile the JS file into a dynamic-link library (DLL) file. The following is a control that is created to demonstrate this functionality.
Program code: Http://www.cnblogs.com/Files/hblynn/SampleControlsCS.rar
First, create a class library project, named Updateanimate.
Ii. add references to the project system.web, System.Drawing, System.Web.Extensions
Third, add a JScript file to the project Updatepanelanimation.js
Four, add the following code to the file:
borderanimation = function (color)
{
This._color = color;
}
Borderanimation.prototype =
{
Animate:function (panelelement)
{
var s = panelelement.style;
S.borderwidth = ' 2px ';
S.bordercolor = This._color;
S.borderstyle = ' solid ';
Window.settimeout (
function ()
{
{
s.borderwidth = 0;
}
},
500);
}
}
This code contains a temporary way to change the style of the UpdatePanel control
V. In Solution Explorer, right-click the properties of Updatepanelanimation.js and set the Build Action property in advanced to "embedded resources."
Six, add a class to the project CustomControl
Replace the code in the class:
Using System;
Using System.Drawing;
Using System.Web.UI;
Using System.Web;
Using System.Globalization;
Namespace Updateanimate
{
public class Updatepanelanimationwithclientresource:control
{
private string _updatepanelid;
Private Color _bordercolor;
Private Boolean _animate;
Public Color BorderColor
{
Get
{
return _bordercolor;
}
Set
{
_bordercolor = value;
}
}
public string Updatepanelid
{
Get
{
return _updatepanelid;
}
Set
{
_updatepanelid = value;
}
}
Public Boolean Animate
{
Get
{
return _animate;
}
Set
{
_animate = value;
}
}
protected override void OnPreRender (EventArgs e)
{
Base. OnPreRender (e);
if (Animate)
{
UpdatePanel UpdatePanel = (UpdatePanel) FindControl (updatepanelid);
string script = String.Format (
CultureInfo.InvariantCulture,
@"
Sys.Application.add_load (function (sender, args) {{
var {0}_borderanimation = new BorderAnimation (' {1} ');
var panelelement = document.getElementById (' {0} ');
if (Args.get_ispartialload ()) {{
{0}_borderanimation.animate (panelelement);
}}
}})
",
Updatepanel.clientid,
Colortranslator.tohtml (bordercolor));
Scriptmanager.registerstartupscript (
This
typeof (Updatepanelanimationwithclientresource),
ClientID,
Script
true);
}
}
}
}
Eight, add the following line to the AssemblyInfo.cs file:
[Assembly:System.Web.UI.WebResource ("UpdateAnimate.UpdatePanelAnimation.js", "Application/x-javascript")]
Nine, build the project.
Control Demo:
First, create a ajax-enabled type of Web site project.
Add the bin directory to the site and directory.
Copy UpdateAnimate.dll from the bin\debug or bin\release directory of the control project to the Web site Bin directory.
Replace the contents of the Default.aspx and run the program:
<%@ Page language= "C #" autoeventwireup= "true" codefile= "Default.aspx.cs" inherits= "_default"%>
<%@ Register tagprefix= "Samples" namespace= "Updateanimate" assembly= "Updateanimate"%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en"
"Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
<script runat= "Server" >
</script>
<title> Scriptreference</title>
<body>
<form id= "Form1" runat= "Server"
< Div>
<asp:scriptmanager id= "ScriptManager1"
enablepartialrendering= "True"
runat= "Server"
<scripts>
<asp:scriptreference assembly= "updateanimate" name= "UpdateAnimate.UpdatePanelAnimation.js" />
</scripts>
</asp:scriptmanager>
<samples:updatepanelanimationwithclientresource
Id= "UpdatePanelAnimator1"
Bordercolor= "Green"
Animate= "true"
Updatepanelid= "UpdatePanel1"
runat= "Server" >
</Samples:UpdatePanelAnimationWithClientResource>
<asp:updatepanel id= "UpdatePanel1"
Updatemode= "Conditional"
runat= "Server" >
<ContentTemplate>
<asp:calendar id= "Calendar2"
runat= "Server" >
</asp:Calendar>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
Http://www.cnblogs.com/hblynn/archive/2007/02/01/637312.html