最近的项目用到这样一段代码:
//要根据后台取的值初始化页面显示
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetGovShiftSetInfo();//此方法作用:取数据库数据来设置rbTwo状态
if (rbTwo.Checked)
{
//Page.RegisterStartupScript("", "<script>istwo();</script>"); //1.0的语法构造,已过时
{
if (!IsPostBack)
{
GetGovShiftSetInfo();//此方法作用:取数据库数据来设置rbTwo状态
if (rbTwo.Checked)
{
//Page.RegisterStartupScript("", "<script>istwo();</script>"); //1.0的语法构造,已过时
Page.ClientScript.RegisterStartupScript(this.GetType(), "MyScript", "istwo();", true);
}
}
}
}
}
}
----------------------------------------------------------------------------------------
//放在前台的JS:
<script type="text/javascript">
function istwo()
{
var obj1=window.document.getElementById("ctl00_MainContent_panelPmB");
var obj2=window.document.getElementById("ctl00_MainContent_panelPmE");
var obj3=window.document.getElementById("ctl00_MainContent_lbAmB");
var obj4=window.document.getElementById("ctl00_MainContent_lbAmE");
var obj5=window.document.getElementById("ctl00_MainContent_panelCard");
obj1.style.visibility = "hidden";
obj2.style.visibility = "hidden";
obj3.style.visibility = "hidden";
obj4.style.visibility = "hidden";
obj5.style.visibility = "hidden";
var no3= parseInt(window.document.getElementById("ctl00_MainContent_txtValue").value);
if (no3 < 5 || no3 > 500 )
{
alert("上下班刷卡有效时限:[5-500]分钟内");
window.document.getElementById("ctl00_MainContent_rbFour").focus();
window.document.getElementById("ctl00_MainContent_rbFour").checked=true;
isfour();
}
}
--------------------------------------------------------------------------------------------------------
function istwo()
{
var obj1=window.document.getElementById("ctl00_MainContent_panelPmB");
var obj2=window.document.getElementById("ctl00_MainContent_panelPmE");
var obj3=window.document.getElementById("ctl00_MainContent_lbAmB");
var obj4=window.document.getElementById("ctl00_MainContent_lbAmE");
var obj5=window.document.getElementById("ctl00_MainContent_panelCard");
obj1.style.visibility = "hidden";
obj2.style.visibility = "hidden";
obj3.style.visibility = "hidden";
obj4.style.visibility = "hidden";
obj5.style.visibility = "hidden";
var no3= parseInt(window.document.getElementById("ctl00_MainContent_txtValue").value);
if (no3 < 5 || no3 > 500 )
{
alert("上下班刷卡有效时限:[5-500]分钟内");
window.document.getElementById("ctl00_MainContent_rbFour").focus();
window.document.getElementById("ctl00_MainContent_rbFour").checked=true;
isfour();
}
}
--------------------------------------------------------------------------------------------------------
之所以没用
Page.ClientScript.RegisterClientScriptBlock而用
Page.ClientScript.RegisterStartupScript是因为
:
RegisterStartupScript 把script放置在ASP.NET page的底部,而RegisterClientScriptBlock把script放置在ASP.NET page的顶部,用RegisterClientScriptBlock会报错,javascript函数不到对象
1.使用 Page.ClientScript.RegisterClientScriptBlock
Listing 4-10: Using the RegisterClientScriptBlock method
Listing 4-10: Using the RegisterClientScriptBlock method
c#
<%
@ Page Language
=
”C#”
%>
<
script runat
=
”server”
>
protected
void
Page_Load(
object
sender, EventArgs e)

{

string myScript = @”function AlertHello()
{ alert(‘Hello ASP.NET’); }”;
Page.ClientScript.RegisterClientScriptBlock(this.GetType(),
“MyScript”, myScript, true);
}
</
script
>
运行结果如下:
<
html
xmlns
=”http://www.w3.org/1999/xhtml”
>
<
head
><
title
>
Adding JavaScript
</
title
></
head
>
<
body
>
<
form
method
=”post”
action
=”JavaScriptPage.aspx”
id
=”form1”
>
<
div
>
<
input
type
=”hidden”
name
=”__VIEWSTATE”
value
=”/wEPDwUKMTY3NzE5MjIyMGRkiyYSRMg+bcXi9DiawYlbxndiTDo=”
/>
</
div
>

<
script
type
=”text/javascript”
>
<!--

function AlertHello()
{ alert(‘Hello ASP.NET’); }// -->
</
script
>
<
div
>
<
input
type
=”submit”
name
=”Button1”
value
=”Button”
onclick
=”AlertHello();”
id
=”Button1”
/>
</
div
>
</
form
>
</
body
>
</
html
>
2.使用Page.ClientScript.RegisterStartupScript
RegisterStartupScript方法与RegisterClientScriptBlock方法最大的不同是: RegisterStartupScript 把script放置在ASP.NET page的底部,而RegisterClientScriptBlock把script放置在ASP.NET page的顶部
如果你的页面中有如下代码:
<
asp:TextBox
ID
=”TextBox1”
Runat
=”server”
>
Hello ASP.NET
</
asp:TextBox
>
Listing 4-11: Improperly using the RegisterClientScriptBlock method
protected
void
Page_Load(
object
sender, EventArgs e)

{
string myScript = @”alert(document.forms[0][‘TextBox1’].value);”;
Page.ClientScript.RegisterClientScriptBlock(this.GetType(),
“MyScript”, myScript, true);
}
<%
@ Page Language
=
”C#”
%>
<
script runat
=
”server”
>
protected
void
Page_Load(
object
sender, EventArgs e)
{
string myScript = @”function AlertHello()
{ alert(‘Hello ASP.NET’); }”;
Page.ClientScript.RegisterClientScriptBlock(this.GetType(),
“MyScript”, myScript, true);
}
</
script
>
运行结果如下:
<
html
xmlns
=”http://www.w3.org/1999/xhtml”
>
<
head
><
title
>
Adding JavaScript
</
title
></
head
>
<
body
>
<
form
method
=”post”
action
=”JavaScriptPage.aspx”
id
=”form1”
>
<
div
>
<
input
type
=”hidden”
name
=”__VIEWSTATE”
value
=”/wEPDwUKMTY3NzE5MjIyMGRkiyYSRMg+bcXi9DiawYlbxndiTDo=”
/>
</
div
>

<
script
type
=”text/javascript”
>
<!--
function AlertHello()
{ alert(‘Hello ASP.NET’); }// -->
</
script
>
<
div
>
<
input
type
=”submit”
name
=”Button1”
value
=”Button”
onclick
=”AlertHello();”
id
=”Button1”
/>
</
div
>
</
form
>
</
body
>
</
html
>
2.使用Page.ClientScript.RegisterStartupScript
RegisterStartupScript方法与RegisterClientScriptBlock方法最大的不同是: RegisterStartupScript 把script放置在ASP.NET page的底部,而RegisterClientScriptBlock把script放置在ASP.NET page的顶部
如果你的页面中有如下代码:
<
asp:TextBox
ID
=”TextBox1”
Runat
=”server”
>
Hello ASP.NET
</
asp:TextBox
>
Listing 4-11: Improperly using the RegisterClientScriptBlock method
c#
protected
void
Page_Load(
object
sender, EventArgs e)
{
string myScript = @”alert(document.forms[0][‘TextBox1’].value);”;
Page.ClientScript.RegisterClientScriptBlock(this.GetType(),
“MyScript”, myScript, true);
}
此页面运行时会报错,原因是JavaScript function先于text box被安放于浏览器。因此JavaScript function找不到TextBox1。
Listing 4-12: Using the RegisterStartupScript method
Listing 4-12: Using the RegisterStartupScript method
c#
protected
void
Page_Load(
object
sender, EventArgs e)

{
string myScript = @”alert(document.forms[0][‘TextBox1’].value);”;
Page.ClientScript.RegisterStartupScript(this.GetType(),
“MyScript”, myScript, true);
}
string
myScript
=
“myJavaScriptCode.js”
Page.ClientScript.RegisterClientScriptInclude(“myKey”, myScript);
protected
void
Page_Load(
object
sender, EventArgs e)
{
string myScript = @”alert(document.forms[0][‘TextBox1’].value);”;
Page.ClientScript.RegisterStartupScript(this.GetType(),
“MyScript”, myScript, true);
}
这段代码把JavaScript function放置于ASP.NET page底部,因此JavaScript运行时它能找到TextBox1。
3.使用Page.ClientScript.RegisterClientScriptInclude
许多开发者把JavaScript放置在.js文件中,使用RegisterClientScriptInclude方法可以注册.js文件中的JavaScript。
Listing 4-13: Using the RegisterClientScriptInclude method
3.使用Page.ClientScript.RegisterClientScriptInclude
许多开发者把JavaScript放置在.js文件中,使用RegisterClientScriptInclude方法可以注册.js文件中的JavaScript。
Listing 4-13: Using the RegisterClientScriptInclude method
c#
string
myScript
=
“myJavaScriptCode.js”
Page.ClientScript.RegisterClientScriptInclude(“myKey”, myScript);
这将在ASP.NET页面产生如下结构:
<script src=”myJavaScriptCode.js” type=”text/javascript”></script>
<script src=”myJavaScriptCode.js” type=”text/javascript”></script>
本文详细介绍了ASP.NET页面中脚本注入的两种方法:Page.ClientScript.RegisterClientScriptBlock和Page.ClientScript.RegisterStartupScript的区别及应用场景。通过示例代码演示了如何正确使用这两种方法,并解释了为什么在特定情况下选择使用RegisterStartupScript更为合适。

6785

被折叠的 条评论
为什么被折叠?



