C#实现客户端弹出消息框封装类实例
作者:lele 时间:2022-06-26 17:33:08
本文实例讲述了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#程序设计有所帮助。
标签:C#,弹出,消息框,类
0
投稿
猜你喜欢
Java String类常用方法梳理总结
2021-08-22 17:57:03
Mybatis映射文件规则实例详解
2022-04-22 03:08:14
Android使用SmsManager实现短信发送功能
2023-08-24 17:54:20
Unity UGUI实现简单拖拽图片功能
2022-11-26 19:55:03
Android实现简单的自定义ViewGroup流式布局
2022-03-16 10:21:23
SpringBoot整合OpenApi的实践
2023-08-03 11:59:55
JAVA使用hutool工具实现查询树结构数据(省市区)
2021-11-04 18:30:09
MyBatis 如何获取子类的属性
2022-09-12 14:42:39
Springboot实现Shiro整合JWT的示例代码
2022-01-30 02:58:32
浅谈Java中replace与replaceAll区别
2021-07-05 12:56:56
44条Java代码优化建议
2023-12-22 06:03:49
Android实现WebView删除缓存的方法
2023-02-19 08:38:22
Android 详解沉浸式状态栏的实现流程
2023-12-18 23:03:31
部署Java在服务器端的EJB组件的方法
2023-08-26 13:43:38
妙解Java中的回调机制(CallBack)
2022-07-15 15:25:31
在mybatis 中使用if else 进行判断的操作
2021-11-10 23:17:11
C#正则表达式实用大全(建议收藏!)
2023-08-29 20:23:58
Mybatis控制台打印SQL语句的两种方式实现
2023-06-23 14:24:57
datagridview实现手动添加行数据
2022-03-29 16:31:09
详解APP微信支付(java后台_统一下单和回调)
2023-11-10 17:26:42