首页 > 代码库 > ASP.NET后台输出js
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=http://www.mamicode.com/"跳转\" 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 = http://www.mamicode.com/‘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代码(不建议)
ASP.NET后台输出js
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。