C#向前台页面中输出JS代码
2012-09-18 10:14
387 查看
[csharp] view
plaincopy
//需要注意的是
//当向页面输出带有相同的Type和Key的的参数时可以避免重复输出
------------------------------------------------------------------------------------------------------------------------------------------------
//在UpdatePanel以外按钮输出JS方法
ClientScriptManager cs = this.ClientScript;
cs.RegisterArrayDeclaration("Hello", "1, 2, 3");
//输出结果:
//<script type="text/javascript">
//var Hello = new Array(1, 2, 3);
//</script>
cs.RegisterClientScriptInclude("HelloWorld", "HelloWorld.js");
//输出结果:
//<script src="HelloWorld.js" type="text/javascript"></script>
//// cs.RegisterClientScriptResource(
cs.RegisterExpandoAttribute(this.Button1.ClientID, "Hello", "World");
//输出结果:
//向Button1按钮注册一个Hello的属性
//<script type="text/javascript">
//var Button1 = document.all ? document.all["Button1"] : document.getElementById("Button1");
//Button1.Hello = "World";
//</script>
cs.RegisterHiddenField("hello", "world");
//输出结果:
//<input type="hidden" name="hello" id="hello" value="world" />
cs.RegisterOnSubmitStatement(this.GetType(), "HelloWorld", "return window.confirm('Do you really want to submit the form?')");
//输出结果:
//(OnSubmit)事件,点击按钮指出提示框
//<script type="text/javascript">
//function WebForm_OnSubmit() {
//return window.confirm('Do you really want to submit the form?');
//return true;
//}
//</script>
cs.RegisterClientScriptBlock(this.GetType(), "HelloWorld", "function helloWorld(){alert(1);}", true);
//输出结果:
//在页面顶部
//<script type="text/javascript">
//function helloWorld(){alert(1);}
//</script>
cs.RegisterStartupScript(this.GetType(), "HelloWorld", "<script>alert('The page has loaded!')</script>");
//输出结果:
//在页面底部
//<script>alert('The page has loaded!')</script>
--------------------------------------------------------------------------------------------------------------------------------------------
//在UpdatePanel以内按钮输出JS方法
//为某个UpdatePanel输出JS脚本
//注:只有当按钮所在的UpdatePanel更新时此方法生效
ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "UpdatePanel1", "alert(1)", true);
//为当前页面输出JS脚本
//注:无论当前UpdatePanel是否更新都向页面输出JS脚本
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "UpdatePanel2", "alert(2)", true);
plaincopy
//需要注意的是
//当向页面输出带有相同的Type和Key的的参数时可以避免重复输出
------------------------------------------------------------------------------------------------------------------------------------------------
//在UpdatePanel以外按钮输出JS方法
ClientScriptManager cs = this.ClientScript;
cs.RegisterArrayDeclaration("Hello", "1, 2, 3");
//输出结果:
//<script type="text/javascript">
//var Hello = new Array(1, 2, 3);
//</script>
cs.RegisterClientScriptInclude("HelloWorld", "HelloWorld.js");
//输出结果:
//<script src="HelloWorld.js" type="text/javascript"></script>
//// cs.RegisterClientScriptResource(
cs.RegisterExpandoAttribute(this.Button1.ClientID, "Hello", "World");
//输出结果:
//向Button1按钮注册一个Hello的属性
//<script type="text/javascript">
//var Button1 = document.all ? document.all["Button1"] : document.getElementById("Button1");
//Button1.Hello = "World";
//</script>
cs.RegisterHiddenField("hello", "world");
//输出结果:
//<input type="hidden" name="hello" id="hello" value="world" />
cs.RegisterOnSubmitStatement(this.GetType(), "HelloWorld", "return window.confirm('Do you really want to submit the form?')");
//输出结果:
//(OnSubmit)事件,点击按钮指出提示框
//<script type="text/javascript">
//function WebForm_OnSubmit() {
//return window.confirm('Do you really want to submit the form?');
//return true;
//}
//</script>
cs.RegisterClientScriptBlock(this.GetType(), "HelloWorld", "function helloWorld(){alert(1);}", true);
//输出结果:
//在页面顶部
//<script type="text/javascript">
//function helloWorld(){alert(1);}
//</script>
cs.RegisterStartupScript(this.GetType(), "HelloWorld", "<script>alert('The page has loaded!')</script>");
//输出结果:
//在页面底部
//<script>alert('The page has loaded!')</script>
--------------------------------------------------------------------------------------------------------------------------------------------
//在UpdatePanel以内按钮输出JS方法
//为某个UpdatePanel输出JS脚本
//注:只有当按钮所在的UpdatePanel更新时此方法生效
ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "UpdatePanel1", "alert(1)", true);
//为当前页面输出JS脚本
//注:无论当前UpdatePanel是否更新都向页面输出JS脚本
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "UpdatePanel2", "alert(2)", true);
相关文章推荐
- C#向前台页面中输出JS代码
- C#向前台页面中输出JS代码 .
- C#向前台页面中输出JS代码
- C# 输出pdf文件流在页面上显示
- vs2003 asp.net中直接输出wml,并利用C#做动态页面的控制,不使用微软的移动控件
- c#(asp.net)几个不同从前台页面操作后台
- C#.Net MVC 前台页面Js调用FileReader实现图片预览
- C#页面前台<%%><%#%><%=%>
- ASP.NET页面输出XML(C#)
- c#(asp.net)几中不同从前台页面操作后台
- 获取控制台输出的信息,发送给前台页面
- 前台页面控制台输出日志和弹出html代码
- C#后台调用前台页面js方法
- 通过后台控制前台页面输出,弹窗时出现乱码
- C#页面前台<%%><%#%><%=%>
- 【转】C#后台调用前台页面js方法
- C# 输出信息到页面 MessageBox类
- C#页面前台<%%><%#%><%=%>
- C#页面后台前台传递参数
- c# 调用控制台程序并传参 获取控制台输出(解释器的前台处理)