天天看點

Asp.net中用來代替Response.Write("<script>alert('錯誤資訊');</script>");

如果直接在頁面中使用Response.Write("<script>alert('錯誤資訊');</script>"); 點選确定以後,可能會造成頁面“錯位”;

 把代碼封裝到一個類中(就叫PageHelper.cs吧),在其他頁面也可以友善的調用;

 例如在其他頁面調用:Page.Alert(this,"錯語資訊"); 效果和上面的代碼是一樣的,要說不同點吧?就是這個寫法比上面的完美好多。

調用的時候也可以有第二個參數,即轉跳位址:  Page.Alert(this,"警告提示~","~/Default.aspx");

/**

 * 

 * 功明說明:頁面助手類,實作彈出警告對話框;

 * Design By: 追憶;

 * 建立日期: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 script = String.Format("alert('{0}');window.location='{1}';", message, url);

    }

}

繼續閱讀