惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Y
Y Combinator Blog
博客园 - 司徒正美
TaoSecurity Blog
TaoSecurity Blog
Martin Fowler
Martin Fowler
T
Threat Research - Cisco Blogs
Blog — PlanetScale
Blog — PlanetScale
S
Secure Thoughts
博客园 - 三生石上(FineUI控件)
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
K
Kaspersky official blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Cisco Talos Blog
Cisco Talos Blog
H
Help Net Security
博客园 - 叶小钗
爱范儿
爱范儿
GbyAI
GbyAI
I
Intezer
M
MIT News - Artificial intelligence
Latest news
Latest news
Schneier on Security
Schneier on Security
T
Tor Project blog
Simon Willison's Weblog
Simon Willison's Weblog
I
InfoQ
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
C
CXSECURITY Database RSS Feed - CXSecurity.com
罗磊的独立博客
N
News and Events Feed by Topic
T
The Blog of Author Tim Ferriss
V2EX - 技术
V2EX - 技术
B
Blog
T
Tailwind CSS Blog
N
Netflix TechBlog - Medium
Security Latest
Security Latest
V
V2EX
F
Fortinet All Blogs
Forbes - Security
Forbes - Security
Application and Cybersecurity Blog
Application and Cybersecurity Blog
The Hacker News
The Hacker News
Scott Helme
Scott Helme
P
Privacy International News Feed
P
Palo Alto Networks Blog
H
Heimdal Security Blog
C
Cisco Blogs
T
The Exploit Database - CXSecurity.com
博客园 - Franky
酷 壳 – CoolShell
酷 壳 – CoolShell
G
Google Developers Blog
W
WeLiveSecurity
L
LINUX DO - 最新话题

博客园 - Caesar

t-sql中的随机数 服务器应用程序不可用[解决办法] 配置错误:未能使用提供程序“RsaProtectedConfigurationProvider”进行解密。提供程序返回错误信息为: 打不开 RSA 密钥容器。 让指定的按钮获取文本框的回车键 正则表达式的语法表 JS几种常用的表单判断 .aspx中写的Page_Load不执行 安装VS2005 SP1补丁方法(转) 读取Cookie出现乱码的解决办法. 数据库的备份与恢复 ASP.NET基于角色的窗体安全认证机制 用Ajax读取Text格式的数据(转) JQuery参考文档 asp.net过滤HTML标签的几个函数 ASP.NET 嵌套Repeater 验证日期的正则表达式 去掉所有HTML标记 将GridView中内容导入到Word C#文件操作
asp.net中使用javascript
Caesar · 2008-01-02 · via 博客园 - Caesar

转载http://www.cnblogs.com/fubeidong/archive/2007/01/19/624529.html

1.使用Page.ClientScript.RegisterClientScriptBlock

使用 Page.ClientScript.RegisterClientScriptBlock可以防止javascript 函数放置在page的顶部

对于该输出,请注意:通过使用 RegisterClientScriptBlock,JavaScript 函数紧跟在 HTML 代码中开启元素 <form> 的后面。

下面就是个例子

前置文件4-10.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="4-10.aspx.cs" Inherits="_4_10" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    
<title>Untitled Page</title>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
    
<asp:Button ID="button1" runat="server" Text="click" OnClientClick="AlertHello()" />
    
</div>
    
</form>
</body>
</html>

后置文件4-10.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;public partial class _4_10 : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    {
        String myScript 
= @"function AlertHello(){alert('hello World');}";
        Page.ClientScript.RegisterClientScriptBlock(
this.GetType(), "MyScript", myScript, true);
    }
}

运行一下看到什么??点击 "click"按钮得到了一个alert。不过你仔细检查我们代码看到什么?发现我们没有写<script type="text/javascript"></script> 这样的开始终止呢。原来Page.ClientScript.RegisterClientScriptBlock已经自动的帮我们生成了。当然这个是在你的function还算是比较大的情况下使用这。

上面一个是正确的例子,来分析一个异常的例子

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="4-10.aspx.cs" Inherits="_4_10" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    
<title>Untitled Page</title>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
    
<asp:TextBox ID="TextBox1" runat="server">Hello ASP.NET</asp:TextBox>
    
</div>
    
</form>
</body>
</html>

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;public partial class _4_10 : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    {
        String myScript 
= @"alert(document.forms[0]['TextBox1'].value);";
        Page.ClientScript.RegisterClientScriptBlock(
this.GetType(), "MyScript", myScript, true);
    }
}

一运行发生了一个错误,根本不弹出窗口,原因就是当Page.ClientScriptRegisterClientScriptBlock已经生效的时候,textbox还没有开始生成呢。所以根本就无办法找到TextBox1

 这个时候只是需要修改一下Page.ClientScript.RegisterClientScriptBlock变成Page.ClientScript.RegisterStartupScript

2.Page.ClientScript.RegisterStartupScript()

当您有一个想要在页面加载时启动的 JavaScript 函数时.

 RegisterStartupScript 方法的两个可能结构如下: 
• RegisterStartupScript (type, key, script)
 
• RegisterStartupScript (type, key, script, script tag specification)
 

3.

这个是用来include js 文件的,以前我们都是写个<script type......>个头在html中,现在不需要了

直接在asp.net 页面中书写以下代码,比如我们有个myjs.js 文件

string myScript ="myjs.js";
page.ClientScript.RegisterClientScriptInclude(
"mykey",myScript);