📄 webprovider.cs
字号:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Xml.Serialization;
using System.Web.SessionState;
namespace isqlweb
{
/// <summary>
/// Web处理类
/// </summary>
public static class WebProvider
{
#region CurrentHttpContext 当前Http上下文
/// <summary>
/// 当前Http上下文
/// </summary>
public static HttpContext CurrentHttpContext
{
get
{
try
{
return HttpContext.Current;
}
catch
{
return null;
}
}
}
#endregion
#region CurrentSession 当前Session
/// <summary>
/// 当前Session
/// </summary>
public static HttpSessionState CurrentSession
{
get
{
try
{
return CurrentHttpContext.Session;
}
catch
{
return null;
}
}
}
#endregion
#region CurrentPage 当前页面
/// <summary>
/// 当前页面
/// </summary>
public static Page CurrentPage
{
get
{
try
{
if (typeof(Page).IsInstanceOfType(CurrentHttpContext.CurrentHandler))
return (Page)CurrentHttpContext.CurrentHandler;
else
return null;
}
catch
{
return null;
}
}
}
#endregion
#region Serialize 序列化对象为XML字符串
/// <summary>
/// 序列化对象为XML字符串
/// </summary>
public static string Serialize<T>(T obj)
{
StringWriter w = new StringWriter();
new XmlSerializer(typeof(T)).Serialize(w, obj);
return w.GetStringBuilder().ToString();
}
#endregion
#region Deserialize 反序列化XML字符串为对象
/// <summary>
/// 反序列化XML字符串为对象
/// </summary>
public static T Deserialize<T>(string str)
{
StringReader r = new StringReader(str);
object o = new XmlSerializer(typeof(T)).Deserialize(r);
return (T)o;
}
#endregion
#region Alert 弹出对话框
public static void Alert(string msg, params object[] objs)
{
Alert(CurrentPage, msg, objs);
}
/// <summary>
/// 弹出对话框
/// </summary>
public static void Alert(Page Page, string msg, params object[] objs)
{
msg = string.Format(msg, objs);
string strscript = "alert(\"" + msg.Replace("\r\n", "\\r\\n").Replace("\n","\\n").Replace("\"", "\\\"") + "\");";
Page.ClientScript.RegisterStartupScript(typeof(string), "alert-" + Guid.NewGuid().ToString(), strscript, true);
}
#endregion
#region AlertRedirect 弹出对话框并跳转
/// <summary>
/// 弹出对话框并跳转
/// </summary>
public static void AlertRedirect(string msg, string path)
{
AlertRedirect(CurrentPage, msg, path);
}
/// <summary>
/// 弹出对话框并跳转
/// </summary>
public static void AlertRedirect(Page Page, string msg, string path)
{
AlertRedirect(Page, msg, path, "location");
}
/// <summary>
/// 弹出对话框并跳转
/// </summary>
public static void AlertRedirect(string msg, string path, string location)
{
AlertRedirect(CurrentPage, msg, path, location);
}
/// <summary>
/// 弹出对话框并跳转
/// </summary>
public static void AlertRedirect(Page Page, string msg, string path, string location)
{
string strscript = "alert(\"" + msg.Replace("\r\n", "\\r\\n").Replace("\n", "\\n").Replace("\"", "\\\"") + "\");";
strscript += location + ".href=\"" + path.Replace("\"", "\\\"") + "\";";
Page.ClientScript.RegisterStartupScript(typeof(string), "alert-" + Guid.NewGuid().ToString(), strscript, true);
}
#endregion
#region ShowMessage 显示消息
/// <summary>
/// 显示消息
/// </summary>
/// <param name="message">消息内容</param>
public static void ShowMessage(string message)
{
ShowMessage(message, "消息");
}
/// <summary>
/// 显示错误消息
/// </summary>
public static void ShowMessage(Exception ex)
{
ShowMessage(ex.Message, "错误");
}
/// <summary>
/// 显示消息
/// </summary>
/// <param name="message">消息内容</param>
/// <param name="caption">消息标题</param>
public static void ShowMessage(string message, string caption)
{
MessageContent = message;
MessageCaption = caption;
try
{
//WebProvider.CurrentPage.Response.Redirect("MessageBox.aspx", false);
//WebProvider.CurrentHttpContext.ApplicationInstance.CompleteRequest();
WebProvider.CurrentHttpContext.Response.Redirect("MessageBox.aspx");
}
catch { }
}
#region MessageContent 消息内容
/// <summary>
/// 消息内容
/// </summary>
public static string MessageContent
{
get
{
try
{
return Utils.Parse<string>(WebProvider.CurrentSession["MessageContent"]);
}
catch
{
return string.Empty;
}
}
set
{
WebProvider.CurrentSession["MessageContent"] = value;
}
}
#endregion
#region MessageCaption 消息标题
/// <summary>
/// 消息标题
/// </summary>
public static string MessageCaption
{
get
{
try
{
return Utils.Parse<string>(WebProvider.CurrentSession["MessageCaption"]);
}
catch
{
return string.Empty;
}
}
set
{
WebProvider.CurrentSession["MessageCaption"] = value;
}
}
#endregion
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -