This article describes the implementation of C# client pop-up message box encapsulation class. Share it for your reference. The details are as follows:
Running on the server side, a dialog box cannot be popped up on the server side, but C# can achieve the effect of popping up a message box by outputting JS code on the page. This C# class encapsulates the commonly used message box popping up a JS code, which can be called on the server side and displays the dialog box on the client side. Not only can it display the JS warning box, it can also display the mode window, which is very convenient.
using System; using ; using ; using ; namespace { /// <summary> /// Common methods for page packaging /// </summary> public class ShowMessageBox { #region Information display /// <summary> /// Show prompt information /// </summary> /// <param name="message"></param> public static void ShowMG(string message) { WriteScript("alert('" + message + "');"); } /// <summary> /// Show prompt information /// </summary> /// <param name="message">Prompt message</param> public static void ShowMessage(string message) { ShowMessage("System Prompt", 180, 120, message); } /// <summary> /// Show prompt information /// </summary> /// <param name="message">Prompt message</param> public static void ShowMessage_link(string message, string linkurl) { ShowMessage_link("System Prompt", 180, 120, message, linkurl, 8000, -1); } /// <summary> /// Show prompt information /// </summary> /// <param name="title"></param> /// <param name="width"></param> /// <param name="height"></param> /// <param name="message">Prompt message</param> private static void ShowMessage(string title, int width, int height, string message) { ShowMessage(title, width, height, message, 3000, -1); } /// <summary> /// Show prompt information /// </summary> /// <param name="title"></param> /// <param name="width"></param> /// <param name="height"></param> /// <param name="message"></param> /// <param name="delayms"></param> /// <param name="leftSpace"></param> private static void ShowMessage(string title, int width, int height, string message, int delayms, int leftSpace) { WriteScript(("popMessage({0},{1},'{2}','{3}',{4},{5});", width, height, title, message, delayms, leftSpace == -1 ? "null" : ())); } /// <summary> /// Show prompt information /// </summary> /// <param name="title"></param> /// <param name="width"></param> /// <param name="height"></param> /// <param name="message"></param> /// <param name="delayms"></param> /// <param name="leftSpace"></param> private static void ShowMessage_link(string title, int width, int height, string message, string linkurl, int delayms, int leftSpace) { WriteScript(("popMessage2({0},{1},'{2}','{3}','{4}',{5},{6});", width, height, title, message, linkurl, delayms, leftSpace == -1 ? "null" : ())); } #endregion #region Display exception information /// <summary> /// Display exception information /// </summary> /// <param name="ex"></param> public static void ShowExceptionMessage(Exception ex) { ShowExceptionMessage(); } /// <summary> /// Display exception information /// </summary> /// <param name="message"></param> public static void ShowExceptionMessage(string message) { WriteScript("alert('" + message + "');"); //("Error prompt", 210, 125, message); } /// <summary> /// Display exception information /// </summary> /// <param name="title"></param> /// <param name="width"></param> /// <param name="height"></param> /// <param name="message"></param> private static void ShowExceptionMessage(string title, int width, int height, string message) { WriteScript(("setTimeout(\"showAlert('{0}',{1},{2},'{3}')\",100);", title, width, height, message)); } #endregion #region Display modal window /// <summary> /// Return the script that displays the modal window with the specified link address /// </summary> /// <param name="wid"></param> /// <param name="title"></param> /// <param name="width"></param> /// <param name="height"></param> /// <param name="url"></param> public static string GetShowModalWindowScript(string wid, string title, int width, int height, string url) { return ("setTimeout(\"showModalWindow('{0}','{1}',{2},{3},'{4}')\",100);", wid, title, width, height, url); } /// <summary> /// Display the modal window with the specified link address /// </summary> /// <param name="wid">Window ID</param> /// <param name="title">Title</param> /// <param name="width">width</param> /// <param name="height">height</param> /// <param name="url">link address</param> public static void ShowModalWindow(string wid, string title, int width, int height, string url) { WriteScript(GetShowModalWindowScript(wid, title, width, height, url)); } /// <summary> /// Bind foreground scripts for specified controls: Display modal window /// </summary> /// <param name="control"></param> /// <param name="eventName"></param> /// <param name="wid"></param> /// <param name="title"></param> /// <param name="width"></param> /// <param name="height"></param> /// <param name="url"></param> /// <param name="isScriptEnd"></param> 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;" : ""; [eventName] = ("showModalWindow('{0}','{1}',{2},{3},'{4}');" + script, wid, title, width, height, url); } /// <summary> /// Bind foreground scripts for specified controls: Display modal window /// </summary> /// <param name="cell"></param> /// <param name="eventName"></param> /// <param name="wid"></param> /// <param name="title"></param> /// <param name="width"></param> /// <param name="height"></param> /// <param name="url"></param> /// <param name="isScriptEnd"></param> 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;" : ""; [eventName] = ("showModalWindow('{0}','{1}',{2},{3},'{4}');" + script, wid, title, width, height, url); } #endregion #region Display client confirmation window /// <summary> /// Display the client confirmation window /// </summary> /// <param name="control"></param> /// <param name="eventName"></param> /// <param name="message"></param> public static void ShowCilentConfirm(WebControl control, string eventName, string message) { ShowCilentConfirm(control, eventName, "System Prompt", 210, 125, message); } /// <summary> /// Display the client confirmation window /// </summary> /// <param name="control"></param> /// <param name="eventName"></param> /// <param name="title"></param> /// <param name="width"></param> /// <param name="height"></param> /// <param name="message"></param> public static void ShowCilentConfirm(WebControl control, string eventName, string title, int width, int height, string message) { [eventName] = ("return showConfirm('{0}',{1},{2},'{3}','{4}');", title, width, height, message, ); } #endregion /// <summary> /// Write javascript scripts /// </summary> /// <param name="script">Script content</param> public static void WriteScript(string script) { Page page = GetCurrentPage(); // NDGridViewScriptFirst(, page); ((), ().ToString(), script, true); } /// <summary> /// Get the current page object instance /// </summary> /// <returns></returns> public static Page GetCurrentPage() { return (Page); } } }
I hope this article will be helpful to everyone's C# programming.