如果你是一个新手,如果你刚接触MVC,如果你跟着置顶的那个项目,我们肯定会用到这里面的几个帮助类

它们都在Common类库下,大家一定要记住要点:取其精华去其糟粕,切勿拿来主义~

ApplicationCache.cs

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web; namespace Common
{
public interface ICache
{
/// <summary>
/// 获取全局应用缓存
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
object GetApplicationCache(string key);
/// <summary>
/// 设置全局应用缓存
/// </summary>
/// <param name="key"></param>
/// <param name="obj"></param>
void SetApplicationCache(string key, object obj);
/// <summary>
/// 删除全局应用缓存
/// </summary>
/// <param name="key"></param>
void RemoveApplicationCache(string key);
}
/// <summary>
/// 全局应用缓存
/// </summary>
public class ApplicationCache:ICache
{
#region ICache 成员 public object GetApplicationCache(string key)
{
return HttpContext.Current.Application[key];
} public void SetApplicationCache(string key, object obj)
{
HttpContext.Current.Application.Add(key, obj);
} public void RemoveApplicationCache(string key)
{
HttpContext.Current.Application.Remove(key);
}
#endregion
}
}

BindDataControl.cs

 using System.Web.UI.WebControls;
using System.Web.UI;
using System.Data;
using System.Data.SqlClient; namespace Common
{
/// <summary>
/// 数据展示控件 绑定数据类
/// </summary>
public class BindDataControl
{
#region 绑定服务器数据控件 简单绑定DataList
/// <summary>
/// 简单绑定DataList
/// </summary>
/// <param name="ctrl">控件ID</param>
/// <param name="mydv">数据视图</param>
public static void BindDataList(Control ctrl, DataView mydv)
{
((DataList)ctrl).DataSourceID = null;
((DataList)ctrl).DataSource = mydv;
((DataList)ctrl).DataBind();
}
#endregion #region 绑定服务器数据控件 SqlDataReader简单绑定DataList
/// <summary>
/// SqlDataReader简单绑定DataList
/// </summary>
/// <param name="ctrl">控件ID</param>
/// <param name="mydv">数据视图</param>
public static void BindDataReaderList(Control ctrl, SqlDataReader mydv)
{
((DataList)ctrl).DataSourceID = null;
((DataList)ctrl).DataSource = mydv;
((DataList)ctrl).DataBind();
}
#endregion #region 绑定服务器数据控件 简单绑定GridView
/// <summary>
/// 简单绑定GridView
/// </summary>
/// <param name="ctrl">控件ID</param>
/// <param name="mydv">数据视图</param>
public static void BindGridView(Control ctrl, DataView mydv)
{
((GridView)ctrl).DataSourceID = null;
((GridView)ctrl).DataSource = mydv;
((GridView)ctrl).DataBind();
}
#endregion /// <summary>
/// 绑定服务器控件 简单绑定Repeater
/// </summary>
/// <param name="ctrl">控件ID</param>
/// <param name="mydv">数据视图</param>
public static void BindRepeater(Control ctrl, DataView mydv)
{
((Repeater)ctrl).DataSourceID = null;
((Repeater)ctrl).DataSource = mydv;
((Repeater)ctrl).DataBind();
}
}
}

CacheHelper.cs

 using System;
using System.Web;
using System.Collections; namespace Common
{
/// <summary>
/// 缓存辅助类
/// </summary>
public class CacheHelper
{
/// <summary>
/// 获取数据缓存
/// </summary>
/// <param name="CacheKey">键</param>
public static object GetCache(string CacheKey)
{
System.Web.Caching.Cache objCache = HttpRuntime.Cache;
return objCache[CacheKey];
} /// <summary>
/// 设置数据缓存
/// </summary>
public static void SetCache(string CacheKey, object objObject)
{
System.Web.Caching.Cache objCache = HttpRuntime.Cache;
objCache.Insert(CacheKey, objObject);
} /// <summary>
/// 设置数据缓存
/// </summary>
public static void SetCache(string CacheKey, object objObject, TimeSpan Timeout)
{
System.Web.Caching.Cache objCache = HttpRuntime.Cache;
objCache.Insert(CacheKey, objObject, null, DateTime.MaxValue, Timeout, System.Web.Caching.CacheItemPriority.NotRemovable, null);
} /// <summary>
/// 设置数据缓存
/// </summary>
public static void SetCache(string CacheKey, object objObject, DateTime absoluteExpiration, TimeSpan slidingExpiration)
{
System.Web.Caching.Cache objCache = HttpRuntime.Cache;
objCache.Insert(CacheKey, objObject, null, absoluteExpiration, slidingExpiration);
} /// <summary>
/// 移除指定数据缓存
/// </summary>
public static void RemoveAllCache(string CacheKey)
{
System.Web.Caching.Cache _cache = HttpRuntime.Cache;
_cache.Remove(CacheKey);
} /// <summary>
/// 移除全部缓存
/// </summary>
public static void RemoveAllCache()
{
System.Web.Caching.Cache _cache = HttpRuntime.Cache;
IDictionaryEnumerator CacheEnum = _cache.GetEnumerator();
while (CacheEnum.MoveNext())
{
_cache.Remove(CacheEnum.Key.ToString());
}
}
}
}

CookieHelper.cs

 using System;
using System.Web; namespace Common
{
/// <summary>
/// Cookie辅助类
/// </summary>
public class CookieHelper
{
/// <summary>
/// 清除指定Cookie
/// </summary>
/// <param name="cookiename">cookiename</param>
public static void ClearCookie(string cookiename)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[cookiename];
if (cookie != null)
{
TimeSpan ts = new TimeSpan(-, , , );
cookie.Expires = DateTime.Now.Add(ts);
HttpContext.Current.Response.AppendCookie(cookie);
HttpContext.Current.Request.Cookies.Remove(cookiename);
}
}
/// <summary>
/// 获取指定Cookie值
/// </summary>
/// <param name="cookiename">cookiename</param>
/// <returns></returns>
public static string GetCookieValue(string cookiename)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[cookiename];
string str = string.Empty;
if (cookie != null)
{
str = cookie.Value;
}
return str;
}
/// <summary>
/// 获取cookie
/// </summary>
/// <param name="cookiename"></param>
/// <returns></returns>
public static HttpCookie GetCookie(string cookiename)
{
return HttpContext.Current.Request.Cookies[cookiename];
}
/// <summary>
/// 添加一个Cookie,默认浏览器关闭过期
/// </summary>
public static void SetCookie(string cookiename, System.Collections.Specialized.NameValueCollection cookievalue, int? days)
{
var cookie = HttpContext.Current.Request.Cookies[cookiename];
if (cookie == null)
{
cookie = new HttpCookie(cookiename);
}
ClearCookie(cookiename);
cookie.Values.Add(cookievalue);
var siteurl = System.Configuration.ConfigurationManager.AppSettings["siteurl"];
if (!string.IsNullOrEmpty(siteurl))
{
cookie.Domain = siteurl.Replace("www.", "");
}
if (days != null && days > ) { cookie.Expires = DateTime.Now.AddDays(Convert.ToInt32(days)); }
HttpContext.Current.Response.AppendCookie(cookie); }
/// <summary>
/// 添加一个Cookie
/// </summary>
/// <param name="cookiename">cookie名</param>
/// <param name="cookievalue">cookie值</param>
/// <param name="expires">过期时间 null为浏览器过期</param>
public static void SetCookie(string cookiename, string cookievalue, int? expires)
{
var cookie = HttpContext.Current.Request.Cookies[cookiename];
if (cookie == null)
{
cookie = new HttpCookie(cookiename);
}
ClearCookie(cookiename);
cookie = new HttpCookie(cookiename);
cookie.Value = cookievalue;
var siteurl = System.Configuration.ConfigurationManager.AppSettings["siteurl"];
if (!string.IsNullOrEmpty(siteurl))
{
cookie.Domain = siteurl.Replace("www.", "");
}
if (expires != null && expires > ) { cookie.Expires = DateTime.Now.AddDays(Convert.ToInt32(expires)); }
HttpContext.Current.Response.AppendCookie(cookie); }
}
}

JScript.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; namespace Common
{
/// <summary>
/// 一些常用的Js调用
/// 添加新版说明:由于旧版普遍采用Response.Write(string msg)的方式输出js脚本,这种
/// 方式输出的js脚本会在html元素的&lt;html&gt;&lt;/html&gt;标签之外,破坏了整个xhtml的结构,
/// 而新版本则采用ClientScript.RegisterStartupScript(string msg)的方式输出,不会改变xhtml的结构,
/// 不会影响执行效果。
/// 为了向下兼容,所以新版本采用了重载的方式,新版本中要求一个System.Web.UI.Page类的实例。
/// </summary>
public class JScript
{
#region 旧版本
/// <summary>
/// 弹出JavaScript小窗口
/// </summary>
/// <param name="message">窗口信息</param>
public static void Alert(string message)
{
#region
string js = @"<Script language='JavaScript'>
alert('" + message + "');</Script>";
HttpContext.Current.Response.Write(js);
#endregion
} /// <summary>
/// 弹出消息框并且转向到新的URL
/// </summary>
/// <param name="message">消息内容</param>
/// <param name="toURL">连接地址</param>
public static void AlertAndRedirect(string message, string toURL)
{
#region
string js = "<script language=javascript>alert('{0}');window.location.replace('{1}')</script>";
HttpContext.Current.Response.Write(string.Format(js, message, toURL));
#endregion
} /// <summary>
/// 回到历史页面
/// </summary>
/// <param name="value">-1/1</param>
public static void GoHistory(int value)
{
#region
string js = @"<Script language='JavaScript'>
history.go({0});
</Script>";
HttpContext.Current.Response.Write(string.Format(js, value));
#endregion
} /// <summary>
/// 关闭当前窗口
/// </summary>
public static void CloseWindow()
{
#region
string js = @"<Script language='JavaScript'>
parent.opener=null;window.close();
</Script>";
HttpContext.Current.Response.Write(js);
HttpContext.Current.Response.End();
#endregion
} /// <summary>
/// 刷新父窗口
/// </summary>
public static void RefreshParent(string url)
{
#region
string js = @"<Script language='JavaScript'>
window.opener.location.href='" + url + "';window.close();</Script>";
HttpContext.Current.Response.Write(js);
#endregion
} /// <summary>
/// 刷新打开窗口
/// </summary>
public static void RefreshOpener()
{
#region
string js = @"<Script language='JavaScript'>
opener.location.reload();
</Script>";
HttpContext.Current.Response.Write(js);
#endregion
} /// <summary>
/// 打开指定大小的新窗体
/// </summary>
/// <param name="url">地址</param>
/// <param name="width">宽</param>
/// <param name="heigth">高</param>
/// <param name="top">头位置</param>
/// <param name="left">左位置</param>
public static void OpenWebFormSize(string url, int width, int heigth, int top, int left)
{
#region
string js = @"<Script language='JavaScript'>window.open('" + url + @"','','height=" + heigth + ",width=" + width + ",top=" + top + ",left=" + left + ",location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,titlebar=no,toolbar=no,directories=no');</Script>"; HttpContext.Current.Response.Write(js);
#endregion
} /// <summary>
/// 转向Url制定的页面
/// </summary>
/// <param name="url">连接地址</param>
public static void JavaScriptLocationHref(string url)
{
#region 转向按钮Js
string js = @"<Script language='JavaScript'>
window.location.replace('{0}');
</Script>";
js = string.Format(js, url);
HttpContext.Current.Response.Write(js);
#endregion
} /// <summary>
/// 打开指定大小位置的模式对话框
/// </summary>
/// <param name="webFormUrl">连接地址</param>
/// <param name="width">宽</param>
/// <param name="height">高</param>
/// <param name="top">距离上位置</param>
/// <param name="left">距离左位置</param>
public static void ShowModalDialogWindow(string webFormUrl, int width, int height, int top, int left)
{
#region 大小位置
string features = "dialogWidth:" + width.ToString() + "px"
+ ";dialogHeight:" + height.ToString() + "px"
+ ";dialogLeft:" + left.ToString() + "px"
+ ";dialogTop:" + top.ToString() + "px"
+ ";center:yes;help=no;resizable:no;status:no;scroll=yes";
ShowModalDialogWindow(webFormUrl, features);
#endregion
}
/// <summary>
/// 弹出模态窗口
/// </summary>
/// <param name="webFormUrl"></param>
/// <param name="features"></param>
public static void ShowModalDialogWindow(string webFormUrl, string features)
{
string js = ShowModalDialogJavascript(webFormUrl, features);
HttpContext.Current.Response.Write(js);
}
/// <summary>
/// 弹出模态窗口
/// </summary>
/// <param name="webFormUrl"></param>
/// <param name="features"></param>
/// <returns></returns>
public static string ShowModalDialogJavascript(string webFormUrl, string features)
{
#region 模态窗口
string js = @"<script language=javascript>
showModalDialog('" + webFormUrl + "','','" + features + "');</script>";
return js;
#endregion
}
#endregion #region 新版本
/// <summary>
/// 弹出JavaScript小窗口
/// </summary>
/// <param name="message">窗口信息</param>
/// <param name="page">Page类的实例</param>
public static void Alert(string message, Page page)
{
#region
string js = @"<Script language='JavaScript'>
alert('" + message + "');</Script>";
if (!page.ClientScript.IsStartupScriptRegistered(page.GetType(), "alert"))
{
page.ClientScript.RegisterStartupScript(page.GetType(), "alert", js);
}
#endregion
} /// <summary>
/// 弹出消息框并且转向到新的URL
/// </summary>
/// <param name="message">消息内容</param>
/// <param name="toURL">连接地址</param>
/// <param name="page">Page类的实例</param>
public static void AlertAndRedirect(string message, string toURL, Page page)
{
#region
string js = "<script language=javascript>alert('{0}');window.location.replace('{1}')</script>";
if (!page.ClientScript.IsStartupScriptRegistered(page.GetType(), "AlertAndRedirect"))
{
page.ClientScript.RegisterStartupScript(page.GetType(), "AlertAndRedirect", string.Format(js, message, toURL));
}
#endregion
} /// <summary>
/// 回到历史页面
/// </summary>
/// <param name="value">-1/1</param>
/// <param name="page">Page类的实例</param>
public static void GoHistory(int value, Page page)
{
#region
string js = @"<Script language='JavaScript'>
history.go({0});
</Script>";
if (!page.ClientScript.IsStartupScriptRegistered(page.GetType(), "GoHistory"))
{
page.ClientScript.RegisterStartupScript(page.GetType(), "GoHistory", string.Format(js, value));
}
#endregion
} /// <summary>
/// 刷新父窗口
/// </summary>
/// <param name="url">要刷新的url</param>
/// <param name="page">Page类的实例</param>
public static void RefreshParent(string url, Page page)
{
#region
string js = @"<Script language='JavaScript'>
window.opener.location.href='" + url + "';window.close();</Script>";
//HttpContext.Current.Response.Write(js);
if (!page.ClientScript.IsStartupScriptRegistered(page.GetType(), "RefreshParent"))
{
page.ClientScript.RegisterStartupScript(page.GetType(), "RefreshParent", js);
}
#endregion
} /// <summary>
/// 刷新打开窗口
/// </summary>
/// <param name="page">Page类的实例</param>
public static void RefreshOpener(Page page)
{
#region
string js = @"<Script language='JavaScript'>
opener.location.reload();
</Script>";
//HttpContext.Current.Response.Write(js);
if (!page.ClientScript.IsStartupScriptRegistered(page.GetType(), "RefreshOpener"))
{
page.ClientScript.RegisterStartupScript(page.GetType(), "RefreshOpener", js);
}
#endregion
} /// <summary>
/// 打开指定大小的新窗体
/// </summary>
/// <param name="url">地址</param>
/// <param name="width">宽</param>
/// <param name="heigth">高</param>
/// <param name="top">头位置</param>
/// <param name="left">左位置</param>
/// <param name="page">Page类的实例</param>
public static void OpenWebFormSize(string url, int width, int heigth, int top, int left, Page page)
{
#region
string js = @"<Script language='JavaScript'>window.open('" + url + @"','','height=" + heigth + ",width=" + width + ",top=" + top + ",left=" + left + ",location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,titlebar=no,toolbar=no,directories=no');</Script>";
//HttpContext.Current.Response.Write(js);
if (!page.ClientScript.IsStartupScriptRegistered(page.GetType(), "OpenWebFormSize"))
{
page.ClientScript.RegisterStartupScript(page.GetType(), "OpenWebFormSize", js);
}
#endregion
} /// <summary>
/// 转向Url制定的页面
/// </summary>
/// <param name="url">连接地址</param>
/// <param name="page">Page类的实例</param>
public static void JavaScriptLocationHref(string url, Page page)
{
#region
string js = @"<Script language='JavaScript'>
window.location.replace('{0}');
</Script>";
js = string.Format(js, url);
if (!page.ClientScript.IsStartupScriptRegistered(page.GetType(), "JavaScriptLocationHref"))
{
page.ClientScript.RegisterStartupScript(page.GetType(), "JavaScriptLocationHref", js);
}
#endregion
} /// <summary>
/// 打开指定大小位置的模式对话框
/// </summary>
/// <param name="webFormUrl">连接地址</param>
/// <param name="width">宽</param>
/// <param name="height">高</param>
/// <param name="top">距离上位置</param>
/// <param name="left">距离左位置</param>
/// <param name="page">Page类的实例</param>
public static void ShowModalDialogWindow(string webFormUrl, int width, int height, int top, int left, Page page)
{
#region
string features = "dialogWidth:" + width.ToString() + "px"
+ ";dialogHeight:" + height.ToString() + "px"
+ ";dialogLeft:" + left.ToString() + "px"
+ ";dialogTop:" + top.ToString() + "px"
+ ";center:yes;help=no;resizable:no;status:no;scroll=yes";
ShowModalDialogWindow(webFormUrl, features, page);
#endregion
}
/// <summary>
/// 弹出模态窗口
/// </summary>
/// <param name="webFormUrl"></param>
/// <param name="features"></param>
/// <param name="page">Page类的实例</param>
public static void ShowModalDialogWindow(string webFormUrl, string features, Page page)
{
string js = ShowModalDialogJavascript(webFormUrl, features);
if (!page.ClientScript.IsStartupScriptRegistered(page.GetType(), "ShowModalDialogWindow"))
{
page.ClientScript.RegisterStartupScript(page.GetType(), "ShowModalDialogWindow", js);
}
}
/// <summary>
/// 向当前页面动态输出客户端脚本代码
/// </summary>
/// <param name="javascript">javascript脚本段</param>
/// <param name="page">Page类的实例</param>
/// <param name="afterForm">是否紧跟在&lt;form&gt;标记之后输出javascript脚本,如果不是则在&lt;/form&gt;标记之前输出脚本代码</param>
public static void AppendScript(string javascript, Page page, bool afterForm)
{
if (afterForm)
{
page.ClientScript.RegisterClientScriptBlock(page.GetType(), page.ToString(), javascript);
}
else
{
page.ClientScript.RegisterStartupScript(page.GetType(), page.ToString(),javascript);
}
}
#endregion
}
}

QueryString.cs

 using System.Web;
using System.Text.RegularExpressions; namespace Common
{
/// <summary>
/// QueryString 地址栏参数
/// </summary>
public class QueryString
{
#region 等于Request.QueryString;如果为null 返回 空“” ,否则返回Request.QueryString[name]
/// <summary>
/// 等于Request.QueryString;如果为null 返回 空“” ,否则返回Request.QueryString[name]
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static string Q(string name)
{
return Request.QueryString[name] == null ? "" : Request.QueryString[name];
}
#endregion /// <summary>
/// 等于 Request.Form 如果为null 返回 空“” ,否则返回 Request.Form[name]
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static string FormRequest(string name)
{
return Request.Form[name] == null ? "" : Request.Form[name].ToString();
}
#region 获取url中的id
/// <summary>
/// 获取url中的id
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static int QId(string name)
{
return StrToId(Q(name));
}
#endregion #region 获取正确的Id,如果不是正整数,返回0
/// <summary>
/// 获取正确的Id,如果不是正整数,返回0
/// </summary>
/// <param name="_value"></param>
/// <returns>返回正确的整数ID,失败返回0</returns>
public static int StrToId(string _value)
{
if (IsNumberId(_value))
return int.Parse(_value);
else
return ;
}
#endregion #region 检查一个字符串是否是纯数字构成的,一般用于查询字符串参数的有效性验证。
/// <summary>
/// 检查一个字符串是否是纯数字构成的,一般用于查询字符串参数的有效性验证。
/// </summary>
/// <param name="_value">需验证的字符串。。</param>
/// <returns>是否合法的bool值。</returns>
public static bool IsNumberId(string _value)
{
return QuickValidate("^[1-9]*[0-9]*$", _value);
}
#endregion #region 快速验证一个字符串是否符合指定的正则表达式。
/// <summary>
/// 快速验证一个字符串是否符合指定的正则表达式。
/// </summary>
/// <param name="_express">正则表达式的内容。</param>
/// <param name="_value">需验证的字符串。</param>
/// <returns>是否合法的bool值。</returns>
public static bool QuickValidate(string _express, string _value)
{
if (_value == null) return false;
Regex myRegex = new Regex(_express);
if (_value.Length == )
{
return false;
}
return myRegex.IsMatch(_value);
}
#endregion #region 类内部调用
/// <summary>
/// HttpContext Current
/// </summary>
public static HttpContext Current
{
get { return HttpContext.Current; }
}
/// <summary>
/// HttpContext Current HttpRequest Request get { return Current.Request;
/// </summary>
public static HttpRequest Request
{
get { return Current.Request; }
}
/// <summary>
/// HttpContext Current HttpRequest Request get { return Current.Request; HttpResponse Response return Current.Response;
/// </summary>
public static HttpResponse Response
{
get { return Current.Response; }
}
#endregion
}
}

RupengPager.cs

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Diagnostics; namespace Common
{
/// <summary>
/// 分页组件调用实例
/// var pager = new Common.RupengPager();
/// pager.UrlFormat = "测试分页.aspx?pagenum={n}";//设置分页URL
/// pager.PageSize = 10; //设置每页显示个数
/// pager.TryParseCurrentPageIndex(Request["pagenum"]);//获取当前页数
/// int startRowIndex = (pager.CurrentPageIndex - 1) * pager.PageSize;//开始行号计算
/// So_KeywordLogBLL bll = new So_KeywordLogBLL();//获取分页数据
/// pager.TotalCount = bll.GetTotalCount();//计算总个数
/// Repeater1.DataSource = bll.GetPagedData(startRowIndex, startRowIndex + pager.PageSize - 1); //设置数据绑定
/// Repeater1.DataBind();
/// PagerHTML = pager.Render();//渲染页码条HTML
/// </summary>
public class RupengPager
{
/// <summary>
/// 总数据条数
/// </summary>
public int TotalCount { get; set; } /// <summary>
/// 每页数据条数
/// </summary>
public int PageSize { get; set; } /// <summary>
/// 当前页码(从1开始)
/// </summary>
public int CurrentPageIndex { get; set; } /// <summary>
/// 显示出来最多的页码数量,因为假设有100页,不可能把100页都显示到界面上
/// </summary>
public int MaxPagerCount { get; set; } /// <summary>
/// 页码链接的地址格式,页码用{n}占位。
/// </summary>
public string UrlFormat { get; set; }
/// <summary>
/// 默认初始化
/// </summary>
public RupengPager()
{
PageSize = ;
MaxPagerCount = ;
} /// <summary>
/// 尝试从字符串pn中解析当前页面赋值给CurrentPageIndex
/// </summary>
/// <param name="pn"></param>
public void TryParseCurrentPageIndex(string pn)
{
int temp;
if (int.TryParse(pn, out temp))
{
CurrentPageIndex = temp;
}
else
{
CurrentPageIndex = ;
}
} /// <summary>
/// 创建页码链接
/// </summary>
/// <param name="i">页码</param>
/// <param name="text">链接文本</param>
/// <returns></returns>
private string GetPageLink(int i,string text)
{
StringBuilder sb = new StringBuilder();
string url = UrlFormat.Replace("{n}", i.ToString());
sb.Append("<a href='").Append(url).Append("'>").Append(text).Append("</a>");
return sb.ToString();
}
public string Render()
{ StringBuilder sb = new StringBuilder();
//TotalCount=35,PageSize=10,pageCount=4 //计算总页数,如果是30条,则是3页,31条也是3页,29条也是3页,因此是
//天花板运算Ceiling
double tempCount = TotalCount / PageSize;
int pageCount = (int)Math.Ceiling(tempCount); //计算显示的页码数(当总页码大于MaxPagerCount)的起始页码
int visibleStart = CurrentPageIndex-MaxPagerCount/;
if (visibleStart <)
{
visibleStart = ;
} //计算显示的页码数(当总页码大于MaxPagerCount)的起始页码
int visibleEnd = visibleStart + MaxPagerCount;
//显示最多MaxPagerCount条
//如果算出来的结束页码大于总页码的话则调整为最大页码
if (visibleEnd >pageCount)
{
visibleEnd = pageCount;
} if (CurrentPageIndex > )
{
sb.Append(GetPageLink(, "首页"));
sb.Append(GetPageLink(CurrentPageIndex - , "上一页"));
}
else
{
sb.Append("<span>首页</span>");
//如果没有上一页了,则只显示一个上一页的文字,没有超链接
sb.Append("<span>上一页</span>");
} //绘制可视的页码链接
for (int i = visibleStart; i <= visibleEnd; i++)
{
//当前页不是超链接
if (i == CurrentPageIndex)
{
sb.Append("<span>").Append(i).Append("</span>");
}
else
{
sb.Append(GetPageLink(i,i.ToString()));
}
}
if (CurrentPageIndex < pageCount)
{
sb.Append(GetPageLink(CurrentPageIndex + , "下一页"));
sb.Append(GetPageLink(pageCount + , "末页"));
}
else
{
sb.Append("<span>下一页</span>");
sb.Append("<span>末页</span>");
}
return sb.ToString();
}
}
}

SessionHelper.cs

 using System.Web;

 namespace Common
{
/// <summary>
/// Session 操作类
/// 1、GetSession(string name)根据session名获取session对象
/// 2、SetSession(string name, object val)设置session
/// </summary>
public class SessionHelper
{
/// <summary>
/// 根据session名获取session对象
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static object GetSession(string name)
{
return HttpContext.Current.Session[name];
}
/// <summary>
/// 设置session
/// </summary>
/// <param name="name">session 名</param>
/// <param name="val">session 值</param>
public static void SetSession(string name, object val)
{
HttpContext.Current.Session.Remove(name);
HttpContext.Current.Session.Add(name, val);
}
/// <summary>
/// 添加Session,调动有效期为20分钟
/// </summary>
/// <param name="strSessionName">Session对象名称</param>
/// <param name="strValue">Session值</param>
public static void Add(string strSessionName, string strValue)
{
HttpContext.Current.Session[strSessionName] = strValue;
HttpContext.Current.Session.Timeout = ;
} /// <summary>
/// 添加Session,调动有效期为20分钟
/// </summary>
/// <param name="strSessionName">Session对象名称</param>
/// <param name="strValues">Session值数组</param>
public static void Adds(string strSessionName, string[] strValues)
{
HttpContext.Current.Session[strSessionName] = strValues;
HttpContext.Current.Session.Timeout = ;
} /// <summary>
/// 添加Session
/// </summary>
/// <param name="strSessionName">Session对象名称</param>
/// <param name="strValue">Session值</param>
/// <param name="iExpires">调动有效期(分钟)</param>
public static void Add(string strSessionName, string strValue, int iExpires)
{
HttpContext.Current.Session[strSessionName] = strValue;
HttpContext.Current.Session.Timeout = iExpires;
} /// <summary>
/// 添加Session
/// </summary>
/// <param name="strSessionName">Session对象名称</param>
/// <param name="strValues">Session值数组</param>
/// <param name="iExpires">调动有效期(分钟)</param>
public static void Adds(string strSessionName, string[] strValues, int iExpires)
{
HttpContext.Current.Session[strSessionName] = strValues;
HttpContext.Current.Session.Timeout = iExpires;
} /// <summary>
/// 读取某个Session对象值
/// </summary>
/// <param name="strSessionName">Session对象名称</param>
/// <returns>Session对象值</returns>
public static string Get(string strSessionName)
{
if (HttpContext.Current.Session[strSessionName] == null)
{
return null;
}
else
{
return HttpContext.Current.Session[strSessionName].ToString();
}
} /// <summary>
/// 读取某个Session对象值数组
/// </summary>
/// <param name="strSessionName">Session对象名称</param>
/// <returns>Session对象值数组</returns>
public static string[] Gets(string strSessionName)
{
if (HttpContext.Current.Session[strSessionName] == null)
{
return null;
}
else
{
return (string[])HttpContext.Current.Session[strSessionName];
}
} /// <summary>
/// 删除某个Session对象
/// </summary>
/// <param name="strSessionName">Session对象名称</param>
public static void Del(string strSessionName)
{
HttpContext.Current.Session[strSessionName] = null;
}
/// <summary>
/// 移除Session
/// </summary>
public static void Remove(string sessionname)
{
if (HttpContext.Current.Session[sessionname] != null)
{
HttpContext.Current.Session.Remove(sessionname);
HttpContext.Current.Session[sessionname] = null;
}
}
}
}

原创文章 转载请尊重劳动成果 http://yuangang.cnblogs.com

【C#公共帮助类】 WebHelper帮助类的更多相关文章

  1. 【转】C#类的分类(静态类、实例类、嵌套类、结构、简单的抽象类、简单的密封类)

    静态类 -------------------------------------------------------------------------------- 静态类就是在class关键字前 ...

  2. java内部类、接口、集合框架、泛型、工具类、实现类

    .t1 { background-color: #ff8080; width: 1100px; height: 40px } 一.内部类 1.成员内部类. (1)成员内部类的实例化: 外部类名.内部类 ...

  3. python之继承、抽象类、新式类和经典类

    一.上节补充1.静态属性静态属性 : 类的属性,所有的对象共享这个变量 如果用对象名去修改类的静态属性:在对象的空间中又创建了一个属性,而不能修改类中属性的值 操作静态属性应该用类名来操作 例1:请你 ...

  4. UML类图与类间六种关系表示

    UML类图与类间六种关系表示 1.类与类图 类封装了数据和行为,是面向对象的重要组成部分,它是具有相同属性,操作,关系的对象集合的总称. 类图是使用频率最高的UML图之一. 类图用于描述系统中所包含的 ...

  5. c++——派生类和基类转换(类型兼容性原则)

    基类也叫父类,派生类也叫子类. 类之间的继承关系继承关系是类之间的父子关系. 继承关系的特点如下:A. 子类拥有父类的所有属性和行为B. 子类也是一种特殊的父类C. 子类对象可以当父类对象使用D. 子 ...

  6. C++——类继承以及类初始化顺序

    对于类以及类继承, 几个主要的问题:1) 继承方式: public/protected/private继承. 这是c++搞的, 实际上继承方式是一种允许子类控制的思想. 子类通过public继承, 可 ...

  7. 适用于app.config与web.config的ConfigUtil读写工具类 基于MongoDb官方C#驱动封装MongoDbCsharpHelper类(CRUD类) 基于ASP.NET WEB API实现分布式数据访问中间层(提供对数据库的CRUD) C# 实现AOP 的几种常见方式

    适用于app.config与web.config的ConfigUtil读写工具类   之前文章:<两种读写配置文件的方案(app.config与web.config通用)>,现在重新整理一 ...

  8. C++派生类与基类的关系

    派生类与基类有这些关系: 1.公有派生类从基类继承所有成员和成员函数 2.公有派生类无法直接访问从基类继承的私有成员,但可以通过继承的公共接口访问. 3.公有派生类无法继承基类的友元函数. 4.基类先 ...

  9. Java实验项目三——编程实现Person类,学生类的设计及其继承关系

    Program: 编程实现Person类,学生类的设计及其继承关系 代码如下: 定义抽象类Person 1 /* 2 * Description:建立抽象类 3 * 4 * Written By:Ca ...

  10. JS面向对象(1) -- 简介,入门,系统常用类,自定义类,constructor,typeof,instanceof,对象在内存中的表现形式

    相关链接: JS面向对象(1) -- 简介,入门,系统常用类,自定义类,constructor,typeof,instanceof,对象在内存中的表现形式 JS面向对象(2) -- this的使用,对 ...

随机推荐

  1. trigger事件模拟

    事件模拟trigger 在操作DOM元素中,大多数事件都是用户必须操作才会触发事件,但有时,需要模拟用户的操作,来达到效果. 需求:页面初始化时触发搜索事件并获取input控件值,并打印输出(效果图如 ...

  2. 【JS基础】对象

    delete 可以删除对象属性及变量 function fun(){ this.name = 'mm'; } var obj = new fun(); console.log(obj.name);// ...

  3. [转载]敏捷开发之Scrum扫盲篇

    现在敏捷开发是越来越火了,人人都在谈敏捷,人人都在学习Scrum和XP...      为了不落后他人,于是我也开始学习Scrum,今天主要是对我最近阅读的相关资料,根据自己的理解,用自己的话来讲述S ...

  4. AngularJS 系列 学习笔记 目录篇

    目录: AngularJS 系列 01 - HelloWorld和数据绑定 AngularJS 系列 02 - 模块 (持续更新)

  5. [原创]ubuntu16.04LTS使用细节

    如何给自己安装的应用创建桌面图标 拿php开发神器phpstorm为例,找到可执行文件所在路径. 这里是/home/haive/PhpStorm/bin/phpstorm.sh 打开dash,搜索&q ...

  6. NYOJ 998

    这道题是欧拉函数的使用,这里简要介绍下欧拉函数. 欧拉函数定义为:对于正整数n,欧拉函数是指不超过n且与n互质的正整数的个数. 欧拉函数的性质:1.设n = p1a1p2a2p3a3p4a4...pk ...

  7. 洛谷P1547 Out of Hay

    题目背景 奶牛爱干草 题目描述 Bessie 计划调查N (2 <= N <= 2,000)个农场的干草情况,它从1号农场出发.农场之间总共有M (1 <= M <= 10,0 ...

  8. Mono+Jexus让C#运行在Linux(centos7_x64),学习笔记

    .h2cls { background: #6fa833 none repeat scroll 0 0 !important; color: #fff; font-family: "微软雅黑 ...

  9. iOS开发系列--App扩展开发

    概述 从iOS 8 开始Apple引入了扩展(Extension)用于增强系统应用服务和应用之间的交互.它的出现让自定义键盘.系统分享集成等这些依靠系统服务的开发变成了可能.WWDC 2016上众多更 ...

  10. Thinking in Unity3D:基于物理着色(PBS)的材质系统

    关于<Thinking in Unity3D> 笔者在研究和使用Unity3D的过程中,获得了一些Unity3D方面的信息,同时也感叹Unity3D设计之精妙.不得不说,笔者最近几年的引擎 ...