ClientScriptManager用法
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ClientScriptManager.aspx.cs" Inherits="Demo4_ClientScriptManager" %>
<!DOCTYPE html PUBLIC "-//W
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>ClientScriptManager</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</form>
</body>
</html>
.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 Demo4_ClientScriptManager : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
ClientScriptManager cs = this.ClientScript;
cs.RegisterArrayDeclaration("Hello", "1, 2, 3");//#1
cs.RegisterClientScriptBlock(this.GetType(), "HelloWorld", "function helloWorld(){alert(1);}", true);//#2
cs.RegisterClientScriptInclude("HelloWorld", "HelloWorld.js");//#3
cs.RegisterExpandoAttribute(this.Button1.ClientID, "Hello", "World");//#4
cs.RegisterHiddenField("hello", "world");//#5
cs.RegisterOnSubmitStatement(this.GetType(), "HelloWorld", "return window.confirm('Do you really want to submit the form?')");//#6
cs.RegisterStartupScript(this.GetType(), "HelloWorld", "<script>alert('The page has loaded!')</script>");//#7
}
}
生成页面的HTML代码
<!DOCTYPE html PUBLIC "-//W
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head><title>ClientScriptManager</title></head>
<body>
<form
"id="form1"
name="form1"
method="post"
action="ClientScriptManager.aspx"
onsubmit="javascript:return
WebForm_OnSubmit();>
<div>
<!-- #5 -->
<input type="hidden" name="hello" id="hello" value="world" />
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKMTQ2OTkzNDMyMWRkdcWxeVaF9qGYsIaDUa9Rcaihgnk=" />
</div>
<!-- #2 -->
<script type="text/javascript">
<!--
function helloWorld(){alert(1);}// -->
</script>
<!-- #3 -->
<script src="HelloWorld.js" type="text/javascript"></script>
<!-- #6 -->
<script type="text/javascript">
<!--
function WebForm_OnSubmit() {
return window.confirm('Do you really want to submit the form?');
return true;
}
// -->
</script>
<input type="submit" name="Button1" value="Button" id="Button1" />
<!-- #1 -->
<script type="text/javascript">
<!--
var Hello = new Array(1, 2, 3);
// -->
</script>
<!-- #4 -->
<script type="text/javascript">
<!--
var Button1 = document.all ? document.all["Button1"] : document.getElementById("Button1");
Button1.Hello = "World";
// -->
</script>
<div>
<input
type="hidden"
name="__EVENTVALIDATION" id="__EVENTVALIDATION"
value="/wEWAgKet4KWBgKM54rGBikWLuZSHp4emnxNA
</div>
<!-- #7 -->
<script>alert('The page has loaded!')</script>
</form>
</body>
</html>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)