本文共 2952 字,大约阅读时间需要 9 分钟。
参考资料:
因为使用ObjectDataSource自带的缓存机制无法实现排序功能,苦苦寻觅终于找到了解决方案。参考后觉得还是自己实操一下比较安心,下面是对《GridView实战二:使用ObjectDataSource数据源控件》的改进!!
CL代码:
public class CL{ private OdsDataManager om = new OdsDataManager(); private static string[] mainKey = {"ods"}; public CL() { } public DataTable GetRecord(int maximumRows, int startRowIndex, string sortExpression) { DataTable dt = HttpRuntime.Cache[GetCacheKey(Convert.ToString(maximumRows) + startRowIndex)] as DataTable; if (dt == null) { dt = om.GetRecord(maximumRows, startRowIndex, sortExpression); AddCache(Convert.ToString(maximumRows) + startRowIndex, dt); } if (!string.IsNullOrEmpty(sortExpression)) { DataTable tempDt = dt.Clone(); DataRow[] drs = dt.Select("",sortExpression); foreach (DataRow dr in drs) { tempDt.Rows.Add(dr.ItemArray); } dt = tempDt; } return dt; } public int GetRecordCount() { return om.GetRecordCount(); } public bool UpdateRecord(int ID, string Name, string Sex, string Country, string Hobby) { RemoveCache(); return om.UpdateRecord(ID,Name,Sex,Country,Hobby); } public bool DelRecord(int ID) { RemoveCache(); return om.DelRecord(ID); } public DataTable GetCountry() { DataTable countryDt = HttpRuntime.Cache["countryDt"] as DataTable; return countryDt; } public DataTable GetHobby() { DataTable hobbyDt = HttpRuntime.Cache["hobbyDt"] as DataTable; return hobbyDt; } private void AddCache(string key, object data) { System.Web.Caching.Cache dc = HttpRuntime.Cache; if (dc[mainKey[0]] == null) dc.Insert(mainKey[0], DateTime.Now); System.Web.Caching.CacheDependency cd = new System.Web.Caching.CacheDependency(null, mainKey); dc.Insert(GetCacheKey(key), data, cd, System.Web.Caching.Cache.NoAbsoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration); } private void RemoveCache() { System.Web.Caching.Cache dc = HttpRuntime.Cache; if (dc[mainKey[0]] != null) dc[mainKey[0]] = DateTime.Now; } private string GetCacheKey(string key) { return mainKey[0] + "-" + key; }}
实现预加载(proactive loading)
Global.asaxvoid Application_Start(object sender, EventArgs e) { // 在应用程序启动时运行的代码 OdsDataManager om = new OdsDataManager(); HttpRuntime.Cache.Insert("countryDt", om.GetCountry(), null, System.Web.Caching.Cache.NoAbsoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable, null); HttpRuntime.Cache.Insert("hobbyDt", om.GetHobby(), null, System.Web.Caching.Cache.NoAbsoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable, null); om = null; }
转载地址:http://uiuwo.baihongyu.com/