国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

ASP.NET中后臺注冊js腳本使用的方法對比_.Net教程

2024-07-10 12:52:28
字體:
供稿:網(wǎng)友

推薦:在ASP.NET中連接SQL Server的簡單方法
在ASP.NET中訪問SQL Server數(shù)據(jù)庫有兩種方法,它們是System.Data.OleDb和System.Data.SqlClient.下面這段程序以System.Data.SqlClient為例訪問本地數(shù)據(jù)庫服務(wù)器.

用Page.ClientScript.RegisterClientScriptBlock 和Page.ClientScript.RegisterStartupScript:區(qū)別:
1.使用Page.ClientScript.RegisterClientScriptBlock
c#代碼
復(fù)制代碼 代碼如下:www.CuoXIn.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>

運行結(jié)果如下:
復(fù)制代碼 代碼如下:www.CuoXIn.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的頂部。
如果你的頁面中有如下代碼:
復(fù)制代碼 代碼如下:www.CuoXIn.com

<asp:TextBox ID=”TextBox1” Runat=”server”>Hello ASP.NET</asp:TextBox>

c#
復(fù)制代碼 代碼如下:www.CuoXIn.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);
}

此頁面運行時會報錯,原因是JavaScript function先于text box被安放于瀏覽器。因此JavaScript function找不到TextBox1。
c#
復(fù)制代碼 代碼如下:www.CuoXIn.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運行時它能找到TextBox1。
3.使用Page.ClientScript.RegisterClientScriptInclude
許多開發(fā)者把JavaScript放置在.js文件中,使用RegisterClientScriptInclude方法可以注冊.js文件中的JavaScript。
c#
復(fù)制代碼 代碼如下:www.CuoXIn.com

string myScript = “myJavaScriptCode.js”
Page.ClientScript.RegisterClientScriptInclude(“myKey”, myScript);

這將在ASP.NET頁面產(chǎn)生如下結(jié)構(gòu):
復(fù)制代碼 代碼如下:www.CuoXIn.com

  <script src=”myJavaScriptCode.js” type=”text/javascript”></script>

分享:VB.NET進(jìn)度條的方法代碼
VB.NET進(jìn)度條的方法代碼,需要的朋友可以參考一下

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 乐平市| 茌平县| 安远县| 萍乡市| 正宁县| 承德县| 万源市| 桦川县| 资溪县| 鹰潭市| 台山市| 句容市| 石首市| 黄浦区| 天水市| 砀山县| 东源县| 慈溪市| 新巴尔虎左旗| 芦溪县| 竹溪县| 娄底市| 旬邑县| 金阳县| 翁源县| 孟津县| 惠来县| 东乌珠穆沁旗| 许昌县| 雅安市| 平罗县| 临邑县| 临猗县| 苏州市| 蕉岭县| 恩施市| 上饶县| 石河子市| 同心县| 镇平县| 大姚县|