在写Asp.Net中写展示层(View层)代码时,时常需要组织信息提示用户,在WinForm中很简单,使用MessageBox.Show()静态方法就行了,方法也很全。
但在WebForm中,相对就比较麻烦些。因为在WebForm中,如果你想弹出对话框,你还的组织一段javascript。在组织的时候,你还必须注意message中
是否包含了例如',"等等特殊的字符。前几个小工具中,我总感觉在这块不顺手,干脆,自己写了一个类,来封装message。
这个类的功能有:
1.过滤/转义掉特殊字符。
2.组织javascript代码。
3.转换提示信息的输出。
小弟对于Html和javascript比较生疏,这个类仅仅是搭了一个框架,有些地方还不完善,需要进一步改进。

using#region using
using System;
using System.Text;
#endregion
namespace ProgrammerLife.Utility

{
/**//// <summary>
/// Message类
/// </summary>
public sealed class Message
{
private fields#region private fields
private static string _language ="JavaScript";
private static string _defaultErrorMessage ="程序出错,请查看日志或者联系程序发布者!";
#endregion

constructor#region constructor
private Message()
{
}
#endregion

private class methods#region private class methods
/**//// <summary>
/// 转义字符串中的特殊字符
/// </summary>
/// <param name="pMessage">字符串</param>
private static void transfer(ref string pMessage)
{
pMessage =pMessage.Replace("\r","").Replace("\n","").Replace(@"\",@"\\");
pMessage =pMessage.Replace("'","").Replace("\"","");
}
/**//// <summary>
/// 获取错误信息
/// </summary>
/// <param name="pEx">异常实例</param>
/// <param name="pExportByOriginally">是否原样输出</param>
/// <param name="pUseInnerException">是否使用InnerException</param>
/// <returns>错误信息</returns>
private static string getErrorMessage(Exception pEx,bool pExportByOriginally,bool pUseInnerException)
{
//declare
string errMessage =string.Empty;
Exception targetException =null;
//check
if(pEx == null)
return errMessage;
if(pUseInnerException)
{
targetException =pEx.InnerException;
}
else
{
targetException =pEx;
}
if(targetException == null)
return errMessage;
//export mechanism
if(pExportByOriginally)
errMessage =targetException.Message;
else
{
if(pEx.GetType() == typeof(ProgrammerLifeException) || pEx.GetType().IsSubclassOf(typeof(ProgrammerLifeException)))
errMessage =targetException.Message;
else
errMessage =DefaultErrorMessage;
}
//return
return errMessage;
}
#endregion

class properties#region class properties
/**//// <summary>
/// 脚本语言
/// </summary>
public static string Language
{
get
{return _language;}
set
{_language=value;}
}
/**//// <summary>
/// 缺省的错误信息
/// </summary>
public static string DefaultErrorMessage
{
get
{return _defaultErrorMessage;}
set
{_defaultErrorMessage=value;}
}
#endregion

public class methods#region public class methods

Show Methods ---for WinForm#region Show Methods ---for WinForm
/**//// <summary>
/// 组织提示信息
/// </summary>
/// <param name="pEnum">枚举类实例</param>
/// <returns>提示信息</returns>
public static string Show(System.Enum pEnum)
{
return string.Format("{0}({1})",pEnum.ToString("g"),pEnum.ToString("d"));
}
/**//// <summary>
/// 组织提示信息
/// </summary>
/// <param name="pObject">object实例</param>
/// <returns>提示信息</returns>
public static string Show(System.Object pObject)
{
if(pObject != null)
return pObject.ToString();
else
return "Object is Null!";
}
/**//// <summary>
/// 组织错误信息
/// </summary>
/// <param name="pEx">异常实例</param>
/// <param name="pExportByOriginally">是否原样输出</param>
/// <param name="pUseInnerException">是否使用InnerException</param>
/// <returns>错误信息</returns>
public static string Show(Exception pEx,bool pExportByOriginally,bool pUseInnerException)
{
//declare
string errMessage =string.Empty;
//get error message
errMessage =getErrorMessage(pEx,pExportByOriginally,pUseInnerException);
//return
return errMessage;
}
/**//// <summary>
/// 组织错误信息
/// </summary>
/// <param name="pEx">异常实例</param>
/// <param name="pExportByOriginally">是否原样输出</param>
/// <returns>错误信息</returns>
public static string Show(Exception pEx,bool pExportByOriginally)
{
return Show(pEx,pExportByOriginally,false);
}
/**//// <summary>
/// 组织错误信息
/// </summary>
/// <param name="pEx">异常实例</param>
/// <returns>错误信息</returns>
public static string Show(Exception pEx)
{
return Show(pEx,false,false);
}
#endregion

Alert Methods --- for WebForm#region Alert Methods --- for WebForm
/**//// <summary>
/// 组织提示信息javascript
/// </summary>
/// <param name="pMessage">提示信息</param>
/// <returns>javascript代码</returns>
public static string Alert(string pMessage)
{
return Alert(pMessage,true);
}
/**//// <summary>
/// 组织提示信息javascript
/// </summary>
/// <param name="pEnum">枚举类实例</param>
/// <returns>javascript代码</returns>
public static string Alert(System.Enum pEnum)
{
return Alert(string.Format("{0}({1})",pEnum.ToString("g"),pEnum.ToString("d")),false);
}
/**//// <summary>
/// 组织提示信息javascript
/// </summary>
/// <param name="pObject">object</param>
/// <returns>javascript代码</returns>
public static string Alert(System.Object pObject)
{
if(pObject != null)
return Alert(pObject.ToString(),false);
else
return Alert("Object is Null!",false);
}
/**//// <summary>
/// 组织提示信息javascript
/// </summary>
/// <param name="pMessage">提示信息</param>
/// <param name="pIsTransfer">是否进行特殊字符转义</param>
/// <returns>javascript代码</returns>
public static string Alert(string pMessage,bool pIsTransfer)
{
//declare
StringBuilder sb =new StringBuilder();
//transfer
if(pIsTransfer)
transfer(ref pMessage);
//orangize javascript string
sb.Append(string.Format("<script language={0}>",_language));
sb.Append(string.Format("alert('{0}');",pMessage));
sb.Append("</script>");
//return
return sb.ToString();
}
/**//// <summary>
/// 组织提示信息javascript
/// </summary>
/// <param name="pEx">异常实例</param>
/// <returns>javascript代码</returns>
public static string Alert(Exception pEx)
{
return Alert(pEx,false,false);
}
/**//// <summary>
/// 组织错误信息javascript
/// </summary>
/// <param name="pEx">异常实例</param>
/// <param name="pExportByOriginally">是否原样输出</param>
/// <returns>javascript代码</returns>
public static string Alert(Exception pEx,bool pExportByOriginally)
{
return Alert(pEx,pExportByOriginally,false);
}
/**//// <summary>
/// 组织错误信息javascript
/// </summary>
/// <param name="pEx">异常实例</param>
/// <param name="pExportByOriginally">是否原样输出</param>
/// <param name="pUseInnerException">是否使用InnerException</param>
/// <returns>javascript代码</returns>
public static string Alert(Exception pEx,bool pExportByOriginally,bool pUseInnerException)
{
//declare
string errMessage =string.Empty;
//get error message
errMessage =getErrorMessage(pEx,pExportByOriginally,pUseInnerException);
//return
if(errMessage == string.Empty)
return errMessage;
else
return Alert(errMessage);
}
#endregion
#endregion
}
}
浙公网安备 33010602011771号