結合MS AJAX將js檔案編譯到動態連結程式庫

來源:互聯網
上載者:User

為了使javascript代碼不被竊取,我們可以將js檔案編譯成動態連結程式庫(dll)檔案。下面為了示範這一功能,建立了一個控制項。

程式碼:http://www.cnblogs.com/Files/hblynn/SampleControlsCS.rar

一、建立一個類庫項目,命名為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

七、替換類中的代碼:

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

八、向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的內容並運行程式:

<%@ 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>

<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>
</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>
</html>
http://www.cnblogs.com/hblynn/archive/2007/02/01/637312.html

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.