ASP.NET中后臺(tái)注冊(cè)js腳本使用的方法對(duì)比_.Net教程
推薦:在ASP.NET中連接SQL Server的簡(jiǎn)單方法在ASP.NET中訪問(wèn)SQL Server數(shù)據(jù)庫(kù)有兩種方法,它們是System.Data.OleDb和System.Data.SqlClient.下面這段程序以System.Data.SqlClient為例訪問(wèn)本地?cái)?shù)據(jù)庫(kù)服務(wù)器.
用Page.ClientScript.RegisterClientScriptBlock 和Page.ClientScript.RegisterStartupScript:區(qū)別:1.使用Page.ClientScript.RegisterClientScriptBlock
c#代碼
復(fù)制代碼 代碼如下:m.dounai2.com
<%@ 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ù)制代碼 代碼如下:m.dounai2.com
<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的頂部。
如果你的頁(yè)面中有如下代碼:
復(fù)制代碼 代碼如下:m.dounai2.com
<asp:TextBox ID=”TextBox1” Runat=”server”>Hello ASP.NET</asp:TextBox>
c#
復(fù)制代碼 代碼如下:m.dounai2.com
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è)面運(yùn)行時(shí)會(huì)報(bào)錯(cuò),原因是JavaScript function先于text box被安放于瀏覽器。因此JavaScript function找不到TextBox1。
c#
復(fù)制代碼 代碼如下:m.dounai2.com
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)行時(shí)它能找到TextBox1。
3.使用Page.ClientScript.RegisterClientScriptInclude
許多開(kāi)發(fā)者把JavaScript放置在.js文件中,使用RegisterClientScriptInclude方法可以注冊(cè).js文件中的JavaScript。
c#
復(fù)制代碼 代碼如下:m.dounai2.com
string myScript = “myJavaScriptCode.js”
Page.ClientScript.RegisterClientScriptInclude(“myKey”, myScript);
這將在ASP.NET頁(yè)面產(chǎn)生如下結(jié)構(gòu):
復(fù)制代碼 代碼如下:m.dounai2.com
<script src=”myJavaScriptCode.js” type=”text/javascript”></script>
分享:VB.NET進(jìn)度條的方法代碼VB.NET進(jìn)度條的方法代碼,需要的朋友可以參考一下
相關(guān).Net教程:
- asp.net如何得到GRIDVIEW中某行某列值的方法
- .net SMTP發(fā)送Email實(shí)例(可帶附件)
- js實(shí)現(xiàn)廣告漂浮效果的小例子
- asp.net Repeater 數(shù)據(jù)綁定的具體實(shí)現(xiàn)
- Asp.Net 無(wú)刷新文件上傳并顯示進(jìn)度條的實(shí)現(xiàn)方法及思路
- Asp.net獲取客戶端IP常見(jiàn)代碼存在的偽造IP問(wèn)題探討
- VS2010 水晶報(bào)表的使用方法
- ASP.NET中操作SQL數(shù)據(jù)庫(kù)(連接字符串的配置及獲取)
- asp.net頁(yè)面?zhèn)髦禍y(cè)試實(shí)例代碼
- DataGridView - DataGridViewCheckBoxCell的使用介紹
- asp.net中javascript的引用(直接引入和間接引入)
- 三層+存儲(chǔ)過(guò)程實(shí)現(xiàn)分頁(yè)示例代碼
.Net教程Rss訂閱編程教程搜索
.Net教程推薦
- 解讀ASP.NET網(wǎng)站程序防SQL注入式攻擊方法
- 淺談.net程序員,該不該學(xué)IL?
- 一個(gè)不被flash、select、activex遮擋的、跨frame的無(wú)限分級(jí)菜單
- ASP.NET:設(shè)置頁(yè)面buffer引出來(lái)的問(wèn)題
- 轉(zhuǎn)換DataSet到普通xml的新法
- 一個(gè)功能齊全的DataGrid分頁(yè)例子
- 解決Asp.net MVC中頁(yè)面標(biāo)題的方法
- 在ASP.NET中的全局異常處理
- 解析.net開(kāi)發(fā)綁定到LINQ查詢的結(jié)果實(shí)例
- 遷移你的Web頁(yè)面到ASP.NET AJAX 1.0
- 相關(guān)鏈接:
- 教程說(shuō)明:
.Net教程-ASP.NET中后臺(tái)注冊(cè)js腳本使用的方法對(duì)比。