如何利用asp.net輸出js

來源:互聯網
上載者:User

利用asp.net輸出js我們大多數都會直接使用Respone.Write()然後根js格式的代碼,再在頁面調用時我們直接這樣是完全可以實現的,下面我來給大家介紹另一種方法

我是我最初的想法以下是程式碼片段:

Respone.Write(“hello word!”);

但是,當你查看用戶端源碼時,你會發現,輸出的內容呈現在源碼的最前端,顯然它破壞了HTML的格式,在某些情況下這是會影響到頁面配置等效果的。正確的輸出方式應該是:

this.ClientScript.RegisterStartupScript

this.ClientScript.RegisterClientScriptBlock.

this.ClientScript.RegisterStartupScript是在Form開始的第一行註冊指令碼,後者則是在Form結尾處註冊指令碼。這樣就不會破壞HTML得格式了,如:

this.ClientScript.RegisterStartupScript(this.GetType(), "scriptKey", "")

this.ClientScript.RegisterStartupScript(this.GetType(), "scriptKey", "alert('hello word!');",True)

this.ClientScript.RegisterClientScriptBlock也類似UpdatePanel

當你想在UpdatePanel內輸出一段JS時,運用以上方法就會得不到預期的效果。那麼請看一下樣本。

有一個UpdatePanel的ID是upPn

ScriptManager.RegisterClientScriptBlock(upPn,this.GetType(), "scriptKey", "alert('hello word!');",True)

ScriptManager.RegisterStartupScript(upPn,this.GetType(), "scriptKey", "alert('hello word!');",True)

這樣的話,當UpdatePanel內容載入到用戶端後,就會彈出“hello word!”對話方塊。

這樣的話,從後台輸出JS就更加方便了

還有一種辦法,就是像產生xml一樣直接產生js檔案了,這樣直接調用js檔案,就可以了,執行個體

protected override void Render(HtmlTextWriter writer){    int titleid =0;    StringWriter html = new StringWriter();    System.Web.UI.HtmlTextWriter tw = new System.Web.UI.HtmlTextWriter(html);    base.Render(tw);    StreamWriter sw;         string dir = Server.MapPath("~/js/ask/");         if (!Directory.Exists(dir))    {        Directory.CreateDirectory(dir);    }         string path = dir + "ask"+"_" + titleid + ".js";         sw = new StreamWriter(path, false, System.Text.Encoding.UTF8);         string newhtml = html.ToString().Replace(""", "").Replace("rn", "");    string lasthtml = "document.write("" + newhtml + "")";         sw.Write(lasthtml.ToString());    sw.Close();    tw.Close();}

JS檔案調用亂碼解決方案

1、 問題:後台字型倒顯示?效果如下:

原因:由於Asp.net採用UTF-8編碼,原先使用GB2312導致亂碼。

解決方案:在Web.config中添加 以下程式碼片段

<system.web>
<globalization requestEncoding="utf-8" responseEncoding="utf-8" uiCulture="zh-CN" culture="zh-CN" fileEncoding="utf-8" />
</system.web>

在解決完後台亂碼問題,接著出現前台亂碼問題,詳情問題2

2、 問題:在添加完以上節點後,系統前台頁面出現以下亂碼問題:

原因:由於添加了 fileEncoding="utf-8"該項目,造成導航無法顯示

解決方案:刪除該選項

3、 問題:由系統後台產生的JS檔案,在前台的*.aspx的頁面中調用時亂碼,效果如下:

原因:JS採用的是GB2312編碼,而*.aspx採用的UTF-8編碼方式,解決思路,統一編碼方式

解決方案:第一步:根據問題1解決方案操作,註:不要加 fileEncoding="utf-8"

第二步:在需要調用到JS的aspx頁中加入 <meta http-equiv="Content-Type" content="text/html; charset=GB2312" />

第三步:在頁面載入事件中加入下句

protected void Page_Load(object sender, EventArgs e)
{
   Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
}

查看本欄目更多精彩內容:http://www.bianceng.cnhttp://www.bianceng.cn/webkf/aspx/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.