ASP.NET后台輸出js


一、使用RegisterClientScriptBlock輸出js代碼塊

 StringBuilder sb = new StringBuilder();
  sb.Append("<input type=\"text\" id=\"txtGo\" name=\"txtGo\" style=\" width:30px\" >");
                sb.Append("<input type=\"button\" id=\"btnGo\" name=\"btnGo\" value=\"跳轉\" onclick=\"pagego();\" >");
 string scriptstrs = "";
                scriptstrs += "function pagego(){";
                scriptstrs += " var val = document.getElementById('txtGo').value;";
                scriptstrs +=" if (val >"+PageCount+") {alert('不能大於'+"+PageCount+");}";
                scriptstrs += "else {window.location.href = 'WordManager.aspx?page='+val;}}";
                if (!Page.ClientScript.IsClientScriptBlockRegistered(this.GetType(), "keys"))
                {

                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "keys", scriptstrs, true);

                }

 

二、使用 RegisterStartupScript輸出一次性使用的js代碼

      

  string scriptstrs = "<script>alert('歡迎光臨!');</script>";
        if (!Page.ClientScript.IsStartupScriptRegistered(this.GetType(),"welcome"))
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "welcome", scriptstrs);
        }

 

三、使用RegisterClientScriptInclude后台輸出已有js文件

首先創建 js文件test.js

if (!Page.ClientScript.IsClientScriptIncludeRegistered(this.GetType(), "keys"))//判斷keys是否已注冊過
{
   Page.ClientScript.RegisterClientScriptInclude("keys", "testjs.js");     
} 

四、運用Response.Write("<script>alert('ww');</script>"); 方式也可輸出簡單js代碼(不建議

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2026 CODEPRJ.COM