ASP.NET后台输出js的三种方式(写给初学者)
2014-08-04 18:40
891 查看
ASP.NET提供了三种后台输出JS的方式:
一、后台输出已有js文件
首先创建 js文件testjs.js
if (!Page.ClientScript.IsClientScriptIncludeRegistered(this.GetType(), "keys"))//判断keys是否已注册过
{
Page.ClientScript.RegisterClientScriptInclude("keys", "testjs.js");
}
二、输出js代码块
string scriptstrs = "";//此处只作为演示,如代码需多次拼接应采用StringBuilder方式
scriptstrs += "function test(str)";
scriptstrs+="{alert(str);}";
if (!Page.ClientScript.IsClientScriptBlockRegistered(this.GetType(), "keys"))
{
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "keys", scriptstrs, true);
}
三、 输出一次性使用的js代码
string scriptstrs = "<script>alert('欢迎光临!');</script>";
if (!Page.ClientScript.IsStartupScriptRegistered(this.GetType(),"welcome"))
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "welcome", scriptstrs);
}
此外,运用Response.Write("<script>alert('ww');</script>"); 方式也可输出简单js代码,但我个人不提倡采用此种方式。因为在以前开发中遇到有些情况下此种方式会导致弹出提示信息后页面字号改变的现象,所以安全起见建议采用上述三种方式。
一、后台输出已有js文件
首先创建 js文件testjs.js
if (!Page.ClientScript.IsClientScriptIncludeRegistered(this.GetType(), "keys"))//判断keys是否已注册过
{
Page.ClientScript.RegisterClientScriptInclude("keys", "testjs.js");
}
二、输出js代码块
string scriptstrs = "";//此处只作为演示,如代码需多次拼接应采用StringBuilder方式
scriptstrs += "function test(str)";
scriptstrs+="{alert(str);}";
if (!Page.ClientScript.IsClientScriptBlockRegistered(this.GetType(), "keys"))
{
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "keys", scriptstrs, true);
}
三、 输出一次性使用的js代码
string scriptstrs = "<script>alert('欢迎光临!');</script>";
if (!Page.ClientScript.IsStartupScriptRegistered(this.GetType(),"welcome"))
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "welcome", scriptstrs);
}
此外,运用Response.Write("<script>alert('ww');</script>"); 方式也可输出简单js代码,但我个人不提倡采用此种方式。因为在以前开发中遇到有些情况下此种方式会导致弹出提示信息后页面字号改变的现象,所以安全起见建议采用上述三种方式。
相关文章推荐
- 【转】ASP.NET后台输出js的三种方式
- ASP.NET提供了三种后台输出JS的方式
- ASP.NET后台输出js大全,页面顶部、form表单中前面与后面、和UpdatePanel(ScriptManager、AJAX)输出JS
- ASP.NET后台调用前台JS函数的三种常见方法
- net 后台输出js asp.net 后台输出js 方法
- ASP.NET后台调用前台JS函数的三种常见方法
- ASP.NET后台输出js
- asp.net 后台输出js方法
- ASP.NET后台输出js脚本代码
- Asp.net前台js调用后台方法,并且局部刷新的方法。
- ASP.net输出JS脚步的类(无AJAX框架)(原作者发布地址http://blog.csdn.net/zhoufoxcn/archive/2008/04/21/2312440.aspx)
- 在ASP.Net中利用JS调用Aspx页面的输出
- ASP.net输出JS脚步的类(无AJAX框架)(原作者发布地址http://blog.csdn.net/zhoufoxcn/archive/2008/04/21/2312440.aspx)
- asp.net 页面用js调用后台函数
- ASP.NET WebForm页面内容输出方式
- Asp.net 后台调用js方法(转)
- ASP.NET中页面跳转的三种方式比较
- asp.net中使用ajax中的三种方式
- 【asp.net 】使用js结合hidden控件实现在后台运行一段程序后提示确认,确认通过后继续执行后台代码~~~
- 使用C#创建webservice及三种调用方式 (ASP.NETweb编程常用到的27个函数集)