天天看点

ASP.NET中输出JS的两种方法

第一种方法:Response.Write("<script>alert('asdf')</script>");

第二种方法,在页面上添加一个buttton1然后,写如下代码

this.Page.ClientScript.RegisterStartupScript(this.GetType(), "MyScript", "function AlertHello() { alert('你好,我亲爱的.NET');}", true);

Button1.Attributes["onclick"] = "AlertHello()";

对与这两种方法的运行中比较,

第一种会最先加载JS代码,后面是白屏,页面很不美观,有可能照成页面样式变形,JS代码会出现在源代码中中<html>标签的最上方。

继续阅读