1、RegisterClientScriptBlock方法
向 Page 对象注册客户端脚本。将js函数放在页面的顶部
例子:
ClientScript.RegisterClientScriptBlock(this.GetType(), "RefreshPage", "<script>"
+ "function RefreshPage(){"
+ " alert('test') "
+ "}</script>");
2、RegisterClientScriptInclude方法
RegisterClientScriptInclude(type,key,url)
ClientScript.RegisterClientScriptInclude(this.GetType(),"GetSingleSelectData", "../Script/GetSingleSelectData.js");
或者:
Page.ClientScript.RegisterClientScriptInclude(this.GetType(),"testInclude", Page.ResolveUrl("~/Script/Script1.js"));
注意:不能直接这样写
Page.ClientScript.RegisterClientScriptInclude(this.GetType(),"testInclude", "~/Script/Script1.js");
这里我发现url不能直接使用类似~/...形式(这种形式是服务器端控件才能解析的),但是可以使用Page.ResolveUrl("~/Script/Script1.js")来代替。
3、RegisterStartupScript方法(这个方法一般不采用)
向 Page 对象注册启动脚本。将js函数放在页面的底部
例子:
Page.ClientScript.RegisterStartupScript(this.GetType(), "ShowMessage", "<script>alert('" + paramMessage + "');</script>");