栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C# > C#教程

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

C#教程 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

本文实例讲述了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
{
  /// 
  /// 页面常用方法包装
  /// 
  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;
    }
  }
}

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/125627.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号