ASP.NET使用新增的Page.ClientScript属性在ASP.NET页面上注册JavaScript函数
使用Page.ClientScript.ResgisterClientScriptBlock方法:
<%@ Page Language="C#" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
string myscript = @"function AlertHello(){alert('This is ASP.NET');}";
//获取注册脚本对象
Page.ClientScript.RegisterClientScriptBlock(this.GetType(),"Myscript",myscript,true);
}
</script>
<!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>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="btnSubmit" type="button" value="点击注册脚本" onclick="AlertHello()" />
</div>
</form>
</body>
</html>