asp.net弹出对话框函数
在进行ASP.NET开发时,经常会用到一些javascript脚本,且这些脚本是在后台执行,将刷新页面,比如:
private void Button1_Click(object sender, System.EventArgs e)
{
Response.Write( "<script language='javascript'>alert('OK');</script>") ;
}
下面介绍三种函数,实现无刷新调用脚本:
其一:
/// <summary>/// 服务器端弹出alert对话框/// </summary>
/// <param name="str_Message">提示信息</param>
/// <param name="page">Page类(表示本页,则为this)</param>
public void Alert(string str_Message, Page page)
{
if (!page.IsStartupScriptRegistered("msgOnlyAlert"))
{
page.RegisterStartupScript("msgOnlyAlert", "<script>alert('" + str_Message + "');</script>");
}
}
其二:
#region
/// <summary>
/// 弹出对话框
/// </summary>
/// <param name="page"></param>
/// <param name="msg"></param>
public static void MessageBox(Page page, string msg)
{
StringBuilder StrScript = new StringBuilder();
StrScript.Append("<script language=javascript>");
StrScript.Append("alert('" + msg + "');");
StrScript.Append("</script>");
if (!page.IsStartupScriptRegistered("MessageBox"))
{
page.RegisterStartupScript("MessageBox", StrScript.ToString());
}
}
#endregion
以上两种已过时了.推荐使用第三种函数:
其三:
/// <summary>
/// 在当前页面弹出提示框
/// </summary>
/// <param name="page"></param>
/// <param name="o"></param>
/// <param name="strText"></param>
public static void ShowMessageBox(string strText)
{
strText = strText.Replace("'", "").Replace(";", "").Replace("<", "").Replace(">", "").Trim();
Page.ClientScript.RegisterStartupScript(page.GetType(), "", alert('" + strText + "')");
}
在Cs页面中直接调用:
protected void Button1_Click(object sender, EventArgs e)
{
MessageBox(this, TextBox1.Text);
Alert(TextBox1.Text, this);
ShowMessageBox(TextBox1.Text);
}