RSS
热门关键字:  Linux  图形  项目管理  LAMP  java
当前位置 : 主页>开源技术>AJAX技术>列表

结合MS AJAX将js文件编译到动态链接库

来源:中国开源社区 作者:开源爱好者 时间:2007-10-21 点击:

为了使javascript代码不被窃取,我们可以将js文件编译成动态链接库(dll)文件。下面为了演示这一功能,创建了一个控件。

字串4

程序代码:http://www.cnblogs.com/Files/hblynn/SampleControlsCS.rar 字串5

一、创建一个类库项目,命名为UpdateAnimate。

字串6

二、向项目中添加引用System.Web, System.Drawing, System.Web.Extensions 字串7

三、向项目中添加一个Jscript的文件UpdatePanelAnimation.js 字串5

四、向文件中添加如下代码:

字串4


BorderAnimation = function(color)
{
this._color = color;
} 字串5

BorderAnimation.prototype =
{
animate: function(panelElement)
{
var s = panelElement.style;
s.borderWidth = '2px';
s.borderColor = this._color;
s.borderStyle = 'solid'; 字串2

window.setTimeout(
function()
{
{
s.borderWidth = 0;
}
},
500);
}
}

字串7


这段代码中,包含一段临时改变UpdatePanel控件样式的方法

字串5

字串8

五、解决方案资源管理器中,右键查看UpdatePanelAnimation.js的属性,把高级中的“生成操作”属性设置成“嵌入的资源”。 字串5

六、向项目中添加一个类CustomControl 字串7

七、替换类中的代码: 字串3

using System;
using System.Drawing;
using System.Web.UI;
using System.Web;
using System.Globalization; 字串1

namespace UpdateAnimate
{
public class UpdatePanelAnimationWithClientResource : Control
{
private string _updatePanelID;
private Color _borderColor;
private Boolean _animate;
public Color BorderColor
{
get
{
return _borderColor;
}
set
{
_borderColor = value;
}
}

字串4

public string UpdatePanelID
{
get
{
return _updatePanelID;
}
set
{
_updatePanelID = value;
}
} 字串6

public Boolean Animate
{
get
{
return _animate;
}
set
{
_animate = value;
}
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
if (Animate)
{ 字串9

UpdatePanel updatePanel = (UpdatePanel)FindControl(UpdatePanelID);

字串9

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));

字串2


ScriptManager.RegisterStartupScript(
this,
typeof(UpdatePanelAnimationWithClientResource),
ClientID,
script,
true);
}
}
}
}

字串5

字串2

字串9

八、向AssemblyInfo.cs文件中添加如下行: 字串5

[assembly: System.Web.UI.WebResource("UpdateAnimate.UpdatePanelAnimation.js", "application/x-javascript")]

字串8

九、生成项目。 字串2

字串6

控件演示: 字串8

一、创建一个AJAX-enabled类型的网站项目。 字串4

二、向网站跟目录下添加bin目录。

字串5

三、从控件项目的bin\Debug或 bin\Release目录拷贝UpdateAnimate.dll到网站bin目录里。

字串1

四、替换Default.aspx的内容并运行程序:

字串6

字串3

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> 字串9

<%@ Register TagPrefix="Samples" Namespace="UpdateAnimate" Assembly="UpdateAnimate" %> 字串1

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 字串7

<script runat="server">

字串5

</script> 字串7

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>ScriptReference</title>
</head>
<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>

最新评论共有 0 位网友发表了评论
发表评论
评论内容:不能超过250字,需审核,请自觉遵守互联网相关政策法规。
用户名: 密码:
匿名?
注册