RegisterStartupScript注册的教本是在runat=server的form中的
所以一定得在 <form id="form1" runat="server">中才能注册成功
Page.RegisterClientScriptBlock和Page.RegisterStartupScript有何区别?
RegisterClientScriptBlock一般返回的是客户端函数的包装,
而RegisterStartupScript返回得函数在document装载完成后会执行,
类似于我们平时所说的body onload="f()"里面的函数;
这两个方法在客户端呈现的代码位置不同,
RegisterClientScriptBlock在<form runat=server>之后,
而RegisterStartupScript在</form>之前。
不带form注册js
注意:这种情况下 header 一定得带式 runtserver 不然脚本会输出在第一行!
/// <summary>
/// 向客户端显示脚本信息,不需要在写<script language=\"javascript\">
/// </summary>
/// <param name="script">脚本的主体部分</param>
public static void AlertScript(string script)
{
script = getScript(script);
if (CurrentPage.Header != null)
{
LiteralControl hc = CurrentPage.Header.FindControl("scriptRegister") as LiteralControl;
if (hc == null)
{
hc = new LiteralControl();
CurrentPage.Header.Controls.Add(hc);
}
hc.Text = script;
}
else
{
HttpContext.Current.Response.Write(script);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
/// <summary>
/// 显示消息提示对话框。
/// </summary>
public sealed class MessageBox
{
private MessageBox()
{
}
/// <summary>
/// 当前页实例
/// </summary>
static Page CurrentPage
{
get
{
return (Page)HttpContext.Current.Handler;
}
}
#region 公共方法
/// <summary>
/// 显示消息提示对话框,如果页面上不具有runat="server"的header那么将采用Response.Write的方式输出脚本
/// </summary>
/// <param name="msg">要显示的消息</param>
public static void Alert(string msg)
{
msg = MyString.ReplaceStrToScript(MyString.RemoveRNString(msg));
AlertScript("alert('" + msg + "');");
}
/// <summary>
/// 控件点击 消息确认提示框
/// </summary>
/// <param name="msg">提示信息</param>
public static void AlertConfirm(System.Web.UI.WebControls.WebControl Control, string msg)
{
Control.Attributes.Add("onclick", "return confirm('" + MyString.ReplaceStrToScript(MyString.RemoveRNString(msg)) + "');");
}
/// <summary>
/// 显示消息提示对话框,并进行页面跳转
/// </summary>
/// <param name="msg">提示信息</param>
/// <param name="url">跳转的目标URL</param>
public static void AlertAndRedirect(string msg, string url)
{
msg = MyString.ReplaceStrToScript(MyString.RemoveRNString(msg));
url = MyString.ReplaceStrToScript(MyString.RemoveRNString(url));
string script = string.Format("alert('{0}');", msg);
script += string.Format("window.location.href='{0}';", url);
AlertScript(script);
}
/// <summary>
/// 显示消息提示对话框,并进行页面跳转
/// </summary>
/// <param name="msg">提示信息</param>
/// <param name="url">跳转的目标URL</param>
public static void AlertAndBack(string msg)
{
msg = MyString.ReplaceStrToScript(MyString.RemoveRNString(msg));
string script = string.Format("alert('{0}');history.go(-1)", msg);
AlertScript(script);
}
/// <summary>
/// 向客户端显示脚本信息,不需要在写<script language=\"javascript\">
/// </summary>
/// <param name="script">脚本的主体部分</param>
public static void AlertScript(string script)
{
script = getScript(script);
if (CurrentPage.Header != null)
{
LiteralControl hc = CurrentPage.Header.FindControl("scriptRegister") as LiteralControl;
if (hc == null)
{
hc = new LiteralControl();
CurrentPage.Header.Controls.Add(hc);
}
hc.Text = script;
}
else
{
HttpContext.Current.Response.Write(script);
}
}
#endregion
#region 私有方法
/// <summary>
/// 获得脚本申明
/// </summary>
static string getScript(string script)
{
StringBuilder Builder = new StringBuilder();
Builder.Append("<script language=\"javascript\">");
Builder.Append(script);
Builder.Append("</script>");
return Builder.ToString();
}
#endregion
}