如何結(jié)合MS AJAX將js文件編譯到動態(tài)鏈接庫_AJAX教程
推薦:解讀AJAX在Post中文的時候解決亂碼的方法加上設(shè)置字符編碼的方法: response.setHeader(charset,gb2312); ******************************************** 看到的說明原文如下: 用Ajax來GET回一個頁面時,RESPONSETEXT里面的中文多半會出現(xiàn)亂碼,這是因為xmlhttp在處理返回的responseText的時候
為了使javascript代碼不被竊取,我們可以將js文件編譯成動態(tài)鏈接庫(dll)文件。下面為了演示這一功能,創(chuàng)建了一個控件。程序代碼:http://www.cnblogs.com/Files/hblynn/SampleControlsCS.rar
一、創(chuàng)建一個類庫項目,命名為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的屬性,把高級中的“生成操作”屬性設(shè)置成“嵌入的資源”。
六、向項目中添加一個類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")]
九、生成項目。
控件演示:
一、創(chuàng)建一個Ajax-enabled類型的網(wǎng)站項目。
二、向網(wǎng)站跟目錄下添加bin目錄。
三、從控件項目的bin\Debug或 bin\Release目錄拷貝UpdateAnimate.dll到網(wǎng)站bin目錄里。
四、替換Default.aspx的內(nèi)容并運(yùn)行程序:
<%@ 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
分享:談在AJAX中GET回的ResponseText中文亂碼的最簡解決辦法用Ajax來GET回一個頁面時,RESPONSETEXT里面的中文多半會出現(xiàn)亂碼,這是因為xmlhttp在處理返回的responseText的時候,是把resposeBody按UTF-8編碼進(jìn)解碼考形成的,如果服務(wù)器送出的確實是UTF-8的數(shù)據(jù)流的時候漢字會正確顯示,而送出了GBK編碼流的時候就亂了。
- Ajax中瀏覽器的緩存問題解決方法
- AJAX和WebService實現(xiàn)省市縣三級聯(lián)動具體代碼
- ajax 登錄功能簡單實現(xiàn)(未連接數(shù)據(jù)庫)
- AJAX和WebService實現(xiàn)郵箱驗證(無刷新驗證郵件地址是否合法)
- AJAX和三層架構(gòu)實現(xiàn)分頁功能具體思路及代碼
- 使用AJAX返回WebService里的集合具體實現(xiàn)
- AJAX獲取服務(wù)器當(dāng)前時間及時間格式輸出處理
- ajax傳遞多個參數(shù)具體實現(xiàn)
- ajax傳遞一個參數(shù)具體實現(xiàn)
- 滑輪滾動到頁面底部ajax加載數(shù)據(jù)配合jsonp實現(xiàn)探討
- jQery ajax——load()方法示例介紹
- jQuery+Ajax實現(xiàn)表格數(shù)據(jù)不同列標(biāo)題排序(為表格注入活力)
- 相關(guān)鏈接:
- 教程說明:
AJAX教程-如何結(jié)合MS AJAX將js文件編譯到動態(tài)鏈接庫
。