Page.ClientScript.RegisterClientScriptBlock可以在服务器端把javascript function放在页面的顶部。
protected void Page_Load(object sender, EventArgs e)
{
string myScript = @"function AlertHello() { alert('Hello ASP.NET'); }";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(),
"MyScript", myScript, true);
}
Page.ClientScript.RegisterStartupScript
The
RegisterStartupScript
method is not that different from the
RegisterClientScriptBlock
method. The big difference is that the
RegisterStartupScript
places the script at the bottom of the
ASP.NET page instead of at the top. In fact, the
RegisterStartupScript
method even takes the same
constructors as the
RegisterClientScriptBlock
method:
➤
RegisterStartupScript
(type,
key,
script)
➤
RegisterStartupScript
(type,
key,
script,
script tag specif cation)
protected void Page_Load(object sender, EventArgs e)
{
string myScript = @"alert(document.forms[0]['TextBox1'].value);";
Page.ClientScript.RegisterStartupScript(this.GetType(),
"MyScript", myScript, true);
}
Page.ClientScript.RegisterStartupScript
是把javascript方法放在页面的底部。
本文介绍了如何使用ASP.NET中的Page.ClientScript.RegisterClientScriptBlock和Page.ClientScript.RegisterStartupScript方法来在页面的不同位置放置JavaScript代码。前者将代码放置于页面顶部,而后者则将其置于页面底部。

4790

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



