• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
内蒙古峰回路转armyfeng
博客园    首页    新随笔    联系   管理    订阅  订阅

Professional ASP.NET 2.0之使用JavaScript处理Pages和Server Controls


 
1.使用 Page.ClientScript.RegisterClientScriptBlock
Listing 4-10: Using the RegisterClientScriptBlock method
VB

<%@ Page Language=”VB” %>
<script runat=”server”>
Protected Sub Page_Load()Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Dim myScript As String = “Function AlertHello()function AlertHello() { alert(‘Hello ASP.NET’); }”
Page.ClientScript.RegisterClientScriptBlock(Me.GetType(), “MyScript”, _
myScript, True)
End Sub
</script>
<html xmlns=”http://www.w3.org/1999/xhtml” >
<head runat=”server”>
<title>Adding JavaScript</title>
</head>
<body>
<form id=”form1” runat=”server”>
<div>
<asp:Button ID=”Button1” Runat=”server” Text=”Button”
OnClientClick=”AlertHello()” />
</div>
</form>
</body>
</html>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
VB

Protected Sub Page_Load()Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Dim myScript As String = “alert(document.forms[0][‘TextBox1’].value);”
Page.ClientScript.RegisterClientScriptBlock(Me.GetType(), “myKey”, myScript, _
True)
End Subc#

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
VB

Protected Sub Page_Load()Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Dim myScript As String = “alert(document.forms[0][‘TextBox1’].value);”
Page.ClientScript.RegisterStartupScript(Me.GetType(), “myKey”, myScript, _
True)
End Subc#

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
VB

Dim myScript As String = “myJavaScriptCode.js”
Page.ClientScript.RegisterClientScriptInclude(“myKey”, myScript)c#

string myScript = “myJavaScriptCode.js”
Page.ClientScript.RegisterClientScriptInclude(“myKey”, myScript);这将在ASP.NET页面产生如下结构:
<script src=”myJavaScriptCode.js” type=”text/javascript”></script>
 
 
 

posted @ 2006-09-24 14:45  老冯  阅读(266)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3