ASP.NET中注册客户端脚本的三种方式

简介: 1. RegisterClientScriptBlock   把Javascript函数放在页面顶部 代码如下: protected void Page_Load(object sender, EventArgs e){  string myScript = @"function Ale...

1. RegisterClientScriptBlock

  把Javascript函数放在页面顶部

代码如下:

protected   void  Page_Load( object  sender, EventArgs e)
{
  
string  myScript  =   @" function AlertHello() { 
                      var oText = document.getElementById('TextBox1');
                      alert(oText.value); }
" ;
  Page.ClientScript.RegisterClientScriptBlock(
this .GetType(),
               
" MyScript " , myScript,  true );
}

 

生成的html代码如下:

< html  xmlns ="http://www.w3.org/1999/xhtml"   >
...
< body >
< form >

... 
 
< script  type ="text/javascript" >  
// <![CDATA[
function  AlertHello() { 
  
var  oText  =  document.getElementById ( ' TextBox1 ' );
  alert(oText.value); }
// ]]>
</ script >
        
< input  name ="TextBox1"  type ="text"   id ="TextBox1"   />
< input  type ="submit"  name ="Button1"  value ="Button"  onclick ="AlertHello();"  id ="Button1"   />
       
...
 
</ form >
</ body >
</ html >

 

2. RegisterStartupScript

把Javascript函数放在页面底部

代码如下:

protected   void  Page_Load( object  sender, EventArgs e)
{
  
string  myScript  =   @" document.getElementById('TextBox1').value = 'Hello ASP.NET.';
                      var oText = document.getElementById('TextBox1');
                      alert(oText.value); 
" ;
  Page.ClientScript.RegisterStartupScript(
this .GetType(),
               
" MyScript " , myScript,  true );
}

 

 

生成的html代码如下:

< html >
...
< body >
< form >

...        

< input  name ="TextBox1"  type ="text"  id ="TextBox1"   />
    
< script  type ="text/javascript" >  
// <![CDATA[
document.getElementById( ' TextBox1 ' ).value  =   ' Hello ASP.NET. ' ;
var  oText  =  document.getElementById( ' TextBox1 ' );
alert(oText.value); 
// ]]>
</ script >

</ form >
</ body >
</ html >

 

3. RegisterClientScriptInclude

注册外部的Javascript脚本文件

代码如下:

protected   void  Page_Load( object  sender, EventArgs e)
{
  
string  myScript  =   " ../JS/myJavaScriptCode.js " ;
  Page.ClientScript.RegisterClientScriptInclude(
" MyScript " , myScript);
}

 

Javascript代码如下:

function  AlertHello() {
    
var  oText  =  document.getElementById( ' TextBox1 ' );
    alert(oText.value);
}

 

HTML中调用如下:

<asp:Button ID="Button1" Runat="server" Text="Button"
            OnClientClick="AlertHello()" />

 

它也是把script脚本放在代码底部

 

目录
相关文章
|
Linux Android开发 iOS开发
基于.Net开发的ChatGPT客户端,兼容Windows、IOS、安卓、MacOS、Linux
基于.Net开发的ChatGPT客户端,兼容Windows、IOS、安卓、MacOS、Linux
234 0
|
4月前
|
开发框架 .NET 开发者
简化 ASP.NET Core 依赖注入(DI)注册-Scrutor
Scrutor 是一个简化 ASP.NET Core 应用程序中依赖注入(DI)注册过程的开源库,支持自动扫描和注册服务。通过简单的配置,开发者可以轻松地从指定程序集中筛选、注册服务,并设置其生命周期,同时支持服务装饰等高级功能。适用于大型项目,提高代码的可维护性和简洁性。仓库地址:&lt;https://github.com/khellang/Scrutor&gt;
94 5
|
5月前
|
开发框架 .NET C#
在 ASP.NET Core 中创建 gRPC 客户端和服务器
本文介绍了如何使用 gRPC 框架搭建一个简单的“Hello World”示例。首先创建了一个名为 GrpcDemo 的解决方案,其中包含一个 gRPC 服务端项目 GrpcServer 和一个客户端项目 GrpcClient。服务端通过定义 `greeter.proto` 文件中的服务和消息类型,实现了一个简单的问候服务 `GreeterService`。客户端则通过 gRPC 客户端库连接到服务端并调用其 `SayHello` 方法,展示了 gRPC 在 C# 中的基本使用方法。
95 5
在 ASP.NET Core 中创建 gRPC 客户端和服务器
|
6月前
|
存储 消息中间件 NoSQL
Redis 入门 - C#.NET Core客户端库六种选择
Redis 入门 - C#.NET Core客户端库六种选择
151 8
|
6月前
|
网络协议 Unix Linux
一个.NET开源、快速、低延迟的异步套接字服务器和客户端库
一个.NET开源、快速、低延迟的异步套接字服务器和客户端库
149 4
|
10月前
|
NoSQL 大数据 Redis
分享5款.NET开源免费的Redis客户端组件库
分享5款.NET开源免费的Redis客户端组件库
150 1
|
11月前
|
JSON 编解码 Go
Golang深入浅出之-HTTP客户端编程:使用net/http包发起请求
【4月更文挑战第25天】Go语言`net/http`包提供HTTP客户端和服务器功能,简化高性能网络应用开发。本文探讨如何发起HTTP请求,常见问题及解决策略。示例展示GET和POST请求的实现。注意响应体关闭、错误处理、内容类型设置、超时管理和并发控制。最佳实践包括重用`http.Client`,使用`context.Context`,处理JSON以及记录错误日志。通过实践这些技巧,提升HTTP编程技能。
128 1
|
11月前
|
Go 开发者
Golang深入浅出之-HTTP客户端编程:使用net/http包发起请求
【4月更文挑战第24天】Go语言的`net/http`包在HTTP客户端编程中扮演重要角色,但使用时需注意几个常见问题:1) 检查HTTP状态码以确保请求成功;2) 记得关闭响应体以防止资源泄漏;3) 设置超时限制,避免长时间等待;4) 根据需求处理重定向。理解这些细节能提升HTTP客户端编程的效率和质量。
126 1
|
11月前
|
SQL 开发框架 .NET
ASP.NET Web——GridView完整增删改查示例(全篇幅包含sql脚本)大二结业考试必备技能
ASP.NET Web——GridView完整增删改查示例(全篇幅包含sql脚本)大二结业考试必备技能
111 0
|
缓存 NoSQL 大数据
4款.NET开源的Redis客户端驱动库
4款.NET开源的Redis客户端驱动库
193 0

热门文章

最新文章