如果直接在頁(yè)面中使用Response.Write("<script>alert('錯(cuò)誤信息');</script>"); 點(diǎn)擊確定以后,可能會(huì)造成頁(yè)面“錯(cuò)位”;
把代碼封裝到一個(gè)類中(就叫PageHelper.cs吧),在其他頁(yè)面也可以方便的調(diào)用;
例如在其他頁(yè)面調(diào)用:Page.Alert(this,"錯(cuò)語(yǔ)信息"); 效果和上面的代碼是一樣的,要說(shuō)不同點(diǎn)吧?就是這個(gè)寫(xiě)法比上面的完美好多。
調(diào)用的時(shí)候也可以有第二個(gè)參數(shù),即轉(zhuǎn)跳地址: Page.Alert(this,"警告提示~","~/Default.aspx");
/**
*
* 功明說(shuō)明:頁(yè)面助手類,實(shí)現(xiàn)彈出警告對(duì)話框;
*
* Design By: 追憶;
*
* 創(chuàng)建日期:2011-01-11
*
* */
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
namespace NewsSystem.CommonUtility
{
public static class PageHelper
{
public static void Alert(Page objPage, string message)
{
string key = "AlertMessage";
string script = string.Format("alert('{0}')", message);
objPage.ClientScript.RegisterStartupScript(typeof(Page), key, script, true);
}
public static void Alert(Page objPage, string message, string url)
{
string key = "AlertMessage";
string script = String.Format("alert('{0}');window.location='{1}';", message, url);
objPage.ClientScript.RegisterStartupScript(typeof(Page), key, script, true);
}
}
}