使用 ClientScriptManager 类的 RegisterClientScriptBlock方法添加客户端脚本

<%@ Page Language="C#"%>

<script runat="server">

public void Page_Load(Object sender, EventArgs e)

{

//定义页面客户端脚本的名字和类型.

String csname1 = "PopupScript";

String csname2 = "ButtonClickScript";

Type cstype = this.GetType();

/从Page类中获取一个lientScriptManager引用.

ClientScriptManager cs = Page.ClientScript;

//检查启动脚本是否已注册.

if (!cs.IsStartupScriptRegistered(cstype, csname1))

{

String cstext1 = "alert('Hello World');";

cs.RegisterStartupScript(cstype, csname1, cstext1, true);
      //不设置addScriptTags(指示是否添加脚本标记的布尔值)为true将直接在网页显示脚本而不是显示提示信息 } //检查启动脚本是否已注册
if (!cs.IsClientScriptBlockRegistered(cstype, csname2)) { StringBuilder cstext2 = new StringBuilder(); cstext2.Append("<script type=text/javascript> function DoClick() {"); cstext2.Append("Form1.Message.value='Text from client script.'} </"); cstext2.Append("script>"); cs.RegisterClientScriptBlock(cstype, csname2, cstext2.ToString(), false); } } </script> <html> <head> <title>ClientScriptManager Example</title> </head> <body> <form id="Form1" runat="server"> <input type="text" id="Message"> <input type="button" value="ClickMe" onclick="DoClick()"> </form> </body> </html>

你可能感兴趣的:(manager)