C#實現的用戶端彈出訊息框封裝類

來源:互聯網
上載者:User
asp.net在伺服器端運行,是不能在伺服器端彈出對話方塊的,但是C#可以通過在頁面輸出JS代碼實現彈出訊息框的效果,這個C#類封裝了常用的訊息框彈出JS代碼,可以在伺服器端調用,在用戶端顯示對話方塊。不但可以顯示JS的警告框,還可以顯示模式視窗,非常方便。

using System;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;  namespace DotNet.Utilities{    ///     /// 頁面常用方法封裝    ///     public class ShowMessageBox    {        #region 資訊顯示          ///         /// 顯示提示資訊        ///         ///         public static void ShowMG(string message)        {            WriteScript("alert('" + message + "');");        }            ///         /// 顯示提示資訊        ///         /// 提示資訊        public static void ShowMessage(string message)        {            ShowMessage("系統提示", 180, 120, message);        }            ///         /// 顯示提示資訊        ///         /// 提示資訊        public static void ShowMessage_link(string message, string linkurl)        {            ShowMessage_link("系統提示", 180, 120, message, linkurl, 8000, -1);        }          ///         /// 顯示提示資訊        ///         ///         ///         ///         /// 提示資訊        private static void ShowMessage(string title, int width, int height, string message)        {            ShowMessage(title, width, height, message, 3000, -1);        }          ///         /// 顯示提示資訊        ///         ///         ///         ///         ///         ///         ///         private static void ShowMessage(string title, int width, int height, string message, int delayms, int leftSpace)        {            WriteScript(string.Format("popMessage({0},{1},'{2}','{3}',{4},{5});", width, height, title, message, delayms, leftSpace == -1 ? "null" : leftSpace.ToString()));        }            ///         /// 顯示提示資訊        ///         ///         ///         ///         ///         ///         ///         private static void ShowMessage_link(string title, int width, int height, string message, string linkurl, int delayms, int leftSpace)        {            WriteScript(string.Format("popMessage2({0},{1},'{2}','{3}','{4}',{5},{6});", width, height, title, message, linkurl, delayms, leftSpace == -1 ? "null" : leftSpace.ToString()));        }            #endregion          #region 顯示異常資訊          ///         /// 顯示異常資訊        ///         ///         public static void ShowExceptionMessage(Exception ex)        {            ShowExceptionMessage(ex.Message);        }          ///         /// 顯示異常資訊        ///         ///         public static void ShowExceptionMessage(string message)        {            WriteScript("alert('" + message + "');");            //PageHelper.ShowExceptionMessage("錯誤提示", 210, 125, message);        }          ///         /// 顯示異常資訊        ///         ///         ///         ///         ///         private static void ShowExceptionMessage(string title, int width, int height, string message)        {            WriteScript(string.Format("setTimeout(\"showAlert('{0}',{1},{2},'{3}')\",100);", title, width, height, message));        }        #endregion          #region 顯示模態視窗          ///         /// 返回把指定連結地址顯示模態視窗的指令碼        ///         ///         ///         ///         ///         ///         public static string GetShowModalWindowScript(string wid, string title, int width, int height, string url)        {            return string.Format("setTimeout(\"showModalWindow('{0}','{1}',{2},{3},'{4}')\",100);", wid, title, width, height, url);        }          ///         /// 把指定連結地址顯示模態視窗        ///         /// 視窗ID        /// 標題        /// 寬度        /// 高度        /// 連結地址        public static void ShowModalWindow(string wid, string title, int width, int height, string url)        {            WriteScript(GetShowModalWindowScript(wid, title, width, height, url));        }          ///         /// 為指定控制項綁定前台指令碼:顯示模態視窗        ///         ///         ///         ///         ///         ///         ///         ///         ///         public static void ShowCilentModalWindow(string wid, WebControl control, string eventName, string title, int width, int height, string url, bool isScriptEnd)        {            string script = isScriptEnd ? "return false;" : "";            control.Attributes[eventName] = string.Format("showModalWindow('{0}','{1}',{2},{3},'{4}');" + script, wid, title, width, height, url);        }          ///         /// 為指定控制項綁定前台指令碼:顯示模態視窗        ///         ///         ///         ///         ///         ///         ///         ///         ///         public static void ShowCilentModalWindow(string wid, TableCell cell, string eventName, string title, int width, int height, string url, bool isScriptEnd)        {            string script = isScriptEnd ? "return false;" : "";            cell.Attributes[eventName] = string.Format("showModalWindow('{0}','{1}',{2},{3},'{4}');" + script, wid, title, width, height, url);        }        #endregion          #region 顯示用戶端確認視窗        ///         /// 顯示用戶端確認視窗        ///         ///         ///         ///         public static void ShowCilentConfirm(WebControl control, string eventName, string message)        {            ShowCilentConfirm(control, eventName, "系統提示", 210, 125, message);        }          ///         /// 顯示用戶端確認視窗        ///         ///         ///         ///         ///         ///         ///         public static void ShowCilentConfirm(WebControl control, string eventName, string title, int width, int height, string message)        {            control.Attributes[eventName] = string.Format("return showConfirm('{0}',{1},{2},'{3}','{4}');", title, width, height, message, control.ClientID);        }            #endregion          ///         /// 寫javascript指令碼        ///         /// 指令碼內容        public static void WriteScript(string script)        {            Page page = GetCurrentPage();              // NDGridViewScriptFirst(page.Form.Controls, page);              page.ClientScript.RegisterStartupScript(page.GetType(), System.Guid.NewGuid().ToString(), script, true);          }          ///         /// 得到當前頁對象執行個體        ///         ///         public static Page GetCurrentPage()        {            return (Page)HttpContext.Current.Handler;        }        }}
  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.