asp.net注册Javascript的方法
在网上看到有关注册Javascript,主要用到了ClientScript的三个方法。
1、IsStartupScriptRegistered:对象是否注册了启用脚本。
2、IsClientScriptBlockRegistered:对象是否注册了客户端脚本块。
3、IsClientScriptIncludeRegistered:对象是否注册了客户端脚本包含。
第一个例子:
//判断事件是否被注册,写入区域:<body>----<script></body>
if (!Page.ClientScript.IsStartupScriptRegistered("test"))
{
//动态注册并执行Javascript
Page.ClientScript.RegisterStartupScript(this.GetType(), "test", info);
}
第二个例子:
<input id="btnSubmit" type="button" value="确定" onclick="salert(txtUserName.value)" />
cs文件:
string info = "function salert(userName){alert('你的名字:' + userName)}";
//判断test是否已注册
if (!Page.ClientScript.IsClientScriptBlockRegistered(this.GetType(), "test"))
{
//动态注册Javascript
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "test", info, true);
}
第三个例子:
<input id="btnSubmit" type="button" value="确定" onclick="salert(txtUserName.value)" />
Jscript.js
function salert(userName)
{
alert("你的名字" + userName);
}
cs文件:
//判断test是否已注册
if (!Page.ClientScript.IsClientScriptIncludeRegistered(this.GetType(), "test"))
{
//动态注册Javascript
Page.ClientScript.RegisterClientScriptInclude("test", "Jscript.js");
}
您可能感兴趣的文章:
重新注册asp.net 2.0的方法
asp.net注册Javascript的方法
解决ASP.NET 4.0尚未在 Web 服务器上注册的出现场景问题
C# 注册右键菜单、文件夹与文件的实现代码
ASP.NET在IIS上注册报0x800702e4错误的解决方法
C#生成注册码的小例子
js读取注册表键值的小例子
c#读写注册表代码一例
asp.net 注册脚本的代码分享
Asp.Net事件模型总结