欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#实现客户端弹出消息框封装类实例

程序员文章站 2023-12-04 17:41:04
本文实例讲述了c#实现客户端弹出消息框封装类。分享给大家供大家参考。具体如下: asp.net在服务器端运行,是不能在服务器端弹出对话框的,但是c#可以通过在页面输出js...

本文实例讲述了c#实现客户端弹出消息框封装类。分享给大家供大家参考。具体如下:

asp.net在服务器端运行,是不能在服务器端弹出对话框的,但是c#可以通过在页面输出js代码实现弹出消息框的效果,这个c#类封装了常用的消息框弹出js代码,可以在服务器端调用,在客户端显示对话框。不但可以显示js的警告框,还可以显示模式窗口,非常方便。

using system;
using system.web;
using system.web.ui;
using system.web.ui.webcontrols;
namespace dotnet.utilities
{
  /// <summary>
  /// 页面常用方法包装
  /// </summary>
  public class showmessagebox
  {
    #region 信息显示
    /// <summary>
    /// 显示提示信息
    /// </summary>
    /// <param name="message"></param>
    public static void showmg(string message)
    {
      writescript("alert('" + message + "');");
    }
    /// <summary>
    /// 显示提示信息
    /// </summary>
    /// <param name="message">提示信息</param>
    public static void showmessage(string message)
    {
      showmessage("系统提示", 180, 120, message);
    }
    /// <summary>
    /// 显示提示信息
    /// </summary>
    /// <param name="message">提示信息</param>
    public static void showmessage_link(string message, string linkurl)
    {
      showmessage_link("系统提示", 180, 120, message, linkurl, 8000, -1);
    }
    /// <summary>
    /// 显示提示信息
    /// </summary>
    /// <param name="title"></param>
    /// <param name="width"></param>
    /// <param name="height"></param>
    /// <param name="message">提示信息</param>
    private static void showmessage(string title, int width, int height, string message)
    {
      showmessage(title, width, height, message, 3000, -1);
    }
    /// <summary>
    /// 显示提示信息
    /// </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(string.format("popmessage({0},{1},'{2}','{3}',{4},{5});", width, height, title, message, delayms, leftspace == -1 ? "null" : leftspace.tostring()));
    }
    /// <summary>
    /// 显示提示信息
    /// </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(string.format("popmessage2({0},{1},'{2}','{3}','{4}',{5},{6});", width, height, title, message, linkurl, delayms, leftspace == -1 ? "null" : leftspace.tostring()));
    }
    #endregion
    #region 显示异常信息
    /// <summary>
    /// 显示异常信息
    /// </summary>
    /// <param name="ex"></param>
    public static void showexceptionmessage(exception ex)
    {
      showexceptionmessage(ex.message);
    }
    /// <summary>
    /// 显示异常信息
    /// </summary>
    /// <param name="message"></param>
    public static void showexceptionmessage(string message)
    {
      writescript("alert('" + message + "');");
      //pagehelper.showexceptionmessage("错误提示", 210, 125, message);
    }
    /// <summary>
    /// 显示异常信息
    /// </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(string.format("settimeout(\"showalert('{0}',{1},{2},'{3}')\",100);", title, width, height, message));
    }
    #endregion
    #region 显示模态窗口
    /// <summary>
    /// 返回把指定链接地址显示模态窗口的脚本
    /// </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 string.format("settimeout(\"showmodalwindow('{0}','{1}',{2},{3},'{4}')\",100);", wid, title, width, height, url);
    }
    /// <summary>
    /// 把指定链接地址显示模态窗口
    /// </summary>
    /// <param name="wid">窗口id</param>
    /// <param name="title">标题</param>
    /// <param name="width">宽度</param>
    /// <param name="height">高度</param>
    /// <param name="url">链接地址</param>
    public static void showmodalwindow(string wid, string title, int width, int height, string url)
    {
      writescript(getshowmodalwindowscript(wid, title, width, height, url));
    }
    /// <summary>
    /// 为指定控件绑定前台脚本:显示模态窗口
    /// </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;" : "";
      control.attributes[eventname] = string.format("showmodalwindow('{0}','{1}',{2},{3},'{4}');" + script, wid, title, width, height, url);
    }
    /// <summary>
    /// 为指定控件绑定前台脚本:显示模态窗口
    /// </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;" : "";
      cell.attributes[eventname] = string.format("showmodalwindow('{0}','{1}',{2},{3},'{4}');" + script, wid, title, width, height, url);
    }
    #endregion
    #region 显示客户端确认窗口
    /// <summary>
    /// 显示客户端确认窗口
    /// </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, "系统提示", 210, 125, message);
    }
    /// <summary>
    /// 显示客户端确认窗口
    /// </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)
    {
      control.attributes[eventname] = string.format("return showconfirm('{0}',{1},{2},'{3}','{4}');", title, width, height, message, control.clientid);
    }
    #endregion
    /// <summary>
    /// 写javascript脚本
    /// </summary>
    /// <param name="script">脚本内容</param>
    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);
    }
    /// <summary>
    /// 得到当前页对象实例
    /// </summary>
    /// <returns></returns>
    public static page getcurrentpage()
    {
      return (page)httpcontext.current.handler;
    }
  }
}

希望本文所述对大家的c#程序设计有所帮助。