ASP.NET中后臺注冊js腳本使用的方法對比
更新時間:2013年04月28日 16:27:59 作者:
接下來為大家介紹下使用Page.ClientScript.RegisterClientScriptBlock 和Page.ClientScript.RegisterStartupScript:區(qū)別
用Page.ClientScript.RegisterClientScriptBlock 和Page.ClientScript.RegisterStartupScript:區(qū)別:
1.使用Page.ClientScript.RegisterClientScriptBlock
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>
運(yùn)行結(jié)果如下:
<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>
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);
}
此頁面運(yùn)行時會報錯,原因是JavaScript function先于text box被安放于瀏覽器。因此JavaScript function找不到TextBox1。
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);
}
這段代碼把JavaScript function放置于ASP.NET page底部,因此JavaScript運(yùn)行時它能找到TextBox1。
3.使用Page.ClientScript.RegisterClientScriptInclude
許多開發(fā)者把JavaScript放置在.js文件中,使用RegisterClientScriptInclude方法可以注冊.js文件中的JavaScript。
c#
string myScript = “myJavaScriptCode.js”
Page.ClientScript.RegisterClientScriptInclude(“myKey”, myScript);
這將在ASP.NET頁面產(chǎn)生如下結(jié)構(gòu):
<script src=”myJavaScriptCode.js” type=”text/javascript”></script>
1.使用Page.ClientScript.RegisterClientScriptBlock
c#代碼
復(fù)制代碼 代碼如下:
<%@ 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>
運(yùn)行結(jié)果如下:
復(fù)制代碼 代碼如下:
<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的頂部。
如果你的頁面中有如下代碼:
復(fù)制代碼 代碼如下:
<asp:TextBox ID=”TextBox1” Runat=”server”>Hello ASP.NET</asp:TextBox>
c#
復(fù)制代碼 代碼如下:
protected void Page_Load(object sender, EventArgs e)
{
string myScript = @”alert(document.forms[0][‘TextBox1'].value);”;
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), “MyScript”, myScript, true);
}
此頁面運(yùn)行時會報錯,原因是JavaScript function先于text box被安放于瀏覽器。因此JavaScript function找不到TextBox1。
c#
復(fù)制代碼 代碼如下:
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運(yùn)行時它能找到TextBox1。
3.使用Page.ClientScript.RegisterClientScriptInclude
許多開發(fā)者把JavaScript放置在.js文件中,使用RegisterClientScriptInclude方法可以注冊.js文件中的JavaScript。
c#
復(fù)制代碼 代碼如下:
string myScript = “myJavaScriptCode.js”
Page.ClientScript.RegisterClientScriptInclude(“myKey”, myScript);
這將在ASP.NET頁面產(chǎn)生如下結(jié)構(gòu):
復(fù)制代碼 代碼如下:
<script src=”myJavaScriptCode.js” type=”text/javascript”></script>
您可能感興趣的文章:
- IIS6 中"ASP 0201 錯誤 無效的默認(rèn)腳本語言" 的解決方法
- 使用ASP控制指定站點解析腳本語言函數(shù)
- 用InstallShield打包ASP程序的腳本
- asp分頁生成html的程序腳本代碼
- 隱藏修改文件時間和文件屬性的ASP腳本
- ASP.NET調(diào)用javascript腳本的常見方法小結(jié)
- 從一個網(wǎng)站扒下的asp生成靜態(tài)頁面的代碼 腳本之家特供版
- iis、apache與nginx禁止目錄執(zhí)行asp、php腳本的實現(xiàn)方法
- ASP上傳漏洞之利用CHR(0)繞過擴(kuò)展名檢測腳本
- ASP中Server.Execute和Execute實現(xiàn)動態(tài)包含(include)腳本的區(qū)別
- ASP程序中常用的腳本語言
相關(guān)文章
.NET Core使用EF生成數(shù)據(jù)庫出錯的解決方法
這篇文章介紹了.NET Core使用EF生成數(shù)據(jù)庫出錯的解決方法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-01-01
FreeTextBox(版本3.1.6)在ASP.Net 2.0中使用方法
在ASP.Net 2.0中使用,只需要2個文件:FreeTextBox.DLL和ftb.imagegallery.aspx2009-11-11
.Net?Core使用Coravel實現(xiàn)任務(wù)調(diào)度的完整步驟
最近在使用調(diào)度程序創(chuàng)建簡單的服務(wù),該服務(wù)將執(zhí)行一些重復(fù)的IO操作,使用的是Coravel調(diào)度庫,下面這篇文章主要給大家介紹了關(guān)于.Net?Core使用Coravel實現(xiàn)任務(wù)調(diào)度的完整步驟,需要的朋友可以參考下2022-08-08
asp.net的web頁面(aspx)數(shù)據(jù)量過多時提交失敗對策
asp.net的web頁面,數(shù)據(jù)量過多時提交失敗的情況想必有很多朋友都有遇到過吧,下面與大家分享下詳細(xì)的解決方法2013-05-05
ASP.NET技巧:數(shù)據(jù)島出到Excel最為簡易的方法
ASP.NET技巧:數(shù)據(jù)島出到Excel最為簡易的方法...2006-09-09

