//菜鸟学习,纯属记录,只求会用,难求甚解,欢迎交流指点!
老版本过时的:
Page.RegisterClientScriptBlock("showDiv", "<script>ShowDiv()</script>")
Page.RegisterStartupScrip(string key,string scriptstring);//scriptstring要包含在<script></script>标签对中。
新版本:
第二种:
第三种:ClientScriptManager.RegisterClientScriptBlock("");
ClientScriptManager.RegisterStartupScript("");
使用示例:
string str = "function()";//前台函数名
ClientScriptManager cs = Page.ClientScript;
cs.RegisterStartupScript(this.GetType(), "", str, true);
<html> <head> <script language="C#" runat="server"> public void Page_Load(Object sender, EventArgs e) { if (!this.IsClientScriptBlockRegistered("clientScript")) { // Form the script that is to be registered at client side. String scriptString = "<script language=JavaScript> function DoClick() {"; scriptString += "myForm.show.value='Welcome to Microsoft .NET'}<"; scriptString += "/"; scriptString += "script>";
// Page.** 和 Reg**都可以
this.RegisterClientScriptBlock("clientScript", scriptString);
}
}
</script>
</head>
<body topmargin="20" leftmargin="10">
<form id="myForm" runat="server">
<input type="text" id="show" style="width=200">
<input type="button" value="ClickMe" οnclick="DoClick()"> </form> </body></html>
注意事项:
1、Response.Write输出后的位置在源文件的第一行.
RegisterClientScriptBlock输出后的位置会在<form>的下一行.(在asp.net自带的脚本和一些隐藏域之下).
RegisterStartupScript输出后的位置会在</form>的上一行.
2、Response.Write和他们有什么本质区别?都是向前台输出script脚本,Response.Write()不能调用前台function?