为了使javascript代码不被窃取,我们可以将js文件编译成动态链接库(dll)文件。下面为了演示这一功能,创建了一个控件。
一、创建一个类库项目,命名为updateanimate。
二、向项目中添加引用system.web, system.drawing, system.web.extensions
三、向项目中添加一个jscript的文件updatepanelanimation.js
四、向文件中添加如下代码:
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);
}
}
这短代码中,包含一段临时改变updatepanel控件样式的方法
五、解决方案资源管理器中,右键查看updatepanelanimation.js的属性,把高级中的“生成操作”属性设置成“嵌入的资源”。
六、向项目中添加一个类customcontrol
七、替换类中的代码:
八、向assemblyinfo.cs文件中添加如下行:
[assembly: system.web.ui.webresource("updateanimate.updatepanelanimation.js", "application/x-javascript")]
九、生成项目。
控件演示:
一、创建一个ajax-enabled类型的网站项目。
二、向网站跟目录下添加bin目录。
三、从控件项目的bin/debug或 bin/release目录拷贝updateanimate.dll到网站bin目录里。
四、替换default.aspx的内容并运行程序:
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);
闽公网安备 35060202000074号