ASP.NET2.0中Page.ClientScript.RegisterClientScriptBlock与RegisterClientScriptBlock
最近的项目用到这样一段代码:
//要根据后台取的值初始化页面显示
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { GetGovShiftSetInfo();//此方法作用:取数据库数据来设置rbTwo状态 if (rbTwo.Checked) { //Page.RegisterStartupScript("", "<script>istwo();</script>"); //1.0的语法构造,已过时 Page.ClientScript.RegisterStartupScript(this.GetType(), "MyScript", "istwo();", true); } } }
----------------------------------------------------------------------------------------
//放在前台的JS:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <script type="text/javascript"> 2 3 function istwo() 4 { 5 var obj1=window.document.getElementById("ctl00_MainContent_panelPmB"); 6 var obj2=window.document.getElementById("ctl00_MainContent_panelPmE"); 7 var obj3=window.document.getElementById("ctl00_MainContent_lbAmB"); 8 var obj4=window.document.getElementById("ctl00_MainContent_lbAmE"); 9 var obj5=window.document.getElementById("ctl00_MainContent_panelCard"); 10 obj1.style.visibility = "hidden"; 11 obj2.style.visibility = "hidden"; 12 obj3.style.visibility = "hidden"; 13 obj4.style.visibility = "hidden"; 14 obj5.style.visibility = "hidden"; 15 var no3= parseInt(window.document.getElementById("ctl00_MainContent_txtValue").value); 16 if (no3 < 5 || no3 > 500 ) 17 { 18 alert("上下班刷卡有效时限:[5-500]分钟内"); 19 window.document.getElementById("ctl00_MainContent_rbFour").focus(); 20 window.document.getElementById("ctl00_MainContent_rbFour").checked=true; 21 isfour(); 22 } 23 24 }
--------------------------------------------------------------------------------------------------------
之所以没用Page.ClientScript.RegisterClientScriptBlock而用 Page.ClientScript.RegisterStartupScript是因为:RegisterStartupScript 把script放置在ASP.NET page的底部,而RegisterClientScriptBlock把script放置在ASP.NET page的顶部,用RegisterClientScriptBlock会报错,javascript函数不到对象
下面为相同例子,来源http://zhhui.cnblogs.com/archive/2006/03/22/355855.aspx
1.使用 Page.ClientScript.RegisterClientScriptBlock
Listing 4-10: Using the RegisterClientScriptBlock method
c#
1 <%@ Page Language=”C#” %> 2 <script runat=”server”> 3 protected void Page_Load(object sender, EventArgs e) 4 { 5 string myScript = @”function AlertHello() { alert(‘Hello ASP.NET’); }”; 6 Page.ClientScript.RegisterClientScriptBlock(this.GetType(), 7 “MyScript”, myScript, true); 8 } 9 </script>
运行结果如下:
1 <html xmlns=”http://www.w3.org/1999/xhtml” > 2 <head><title> 3 Adding JavaScript 4 </title></head> 5 <body> 6 <form method=”post” action=”JavaScriptPage.aspx” id=”form1”> 7 <div> 8 <input type=”hidden” name=”__VIEWSTATE” 9 value=”/wEPDwUKMTY3NzE5MjIyMGRkiyYSRMg+bcXi9DiawYlbxndiTDo=” /> 10 </div> 11 <script type=”text/javascript”> 12 <!-- 13 function AlertHello() { alert(‘Hello ASP.NET’); }// --> 14 </script> 15 <div> 16 <input type=”submit” name=”Button1” value=”Button” onclick=”AlertHello();” 17 id=”Button1” /> 18 </div> 19 </form> 20 </body> 21 </html>
2.使用Page.ClientScript.RegisterStartupScript
RegisterStartupScript方法与RegisterClientScriptBlock方法最大的不同是:RegisterStartupScript 把script放置在ASP.NET page的底部,而RegisterClientScriptBlock把script放置在ASP.NET page的顶部
如果你的页面中有如下代码:
1 <asp:TextBox ID=”TextBox1” Runat=”server”>Hello ASP.NET</asp:TextBox>
Listing 4-11: Improperly using the RegisterClientScriptBlock method
c#
1 protected void Page_Load(object sender, EventArgs e) 2 { 3 string myScript = @”alert(document.forms[0][‘TextBox1’].value);”; 4 Page.ClientScript.RegisterClientScriptBlock(this.GetType(), 5 “MyScript”, myScript, true); 6 }
此页面运行时会报错,原因是JavaScript function先于text box被安放于浏览器。因此JavaScript function找不到TextBox1。
Listing 4-12: Using the RegisterStartupScript method
c#
1 protected void Page_Load(object sender, EventArgs e) 2 { 3 string myScript = @”alert(document.forms[0][‘TextBox1’].value);”; 4 Page.ClientScript.RegisterStartupScript(this.GetType(), 5 “MyScript”, myScript, true); 6 }
这段代码把JavaScript function放置于ASP.NET page底部,因此JavaScript运行时它能找到TextBox1。
3.使用Page.ClientScript.RegisterClientScriptInclude
许多开发者把JavaScript放置在.js文件中,使用RegisterClientScriptInclude方法可以注册.js文件中的JavaScript。
Listing 4-13: Using the RegisterClientScriptInclude method
c#
1 string myScript = “myJavaScriptCode.js” 2 Page.ClientScript.RegisterClientScriptInclude(“myKey”, myScript);
这将在ASP.NET页面产生如下结构:
<script src=”myJavaScriptCode.js” type=”text/javascript”></script>