生活随笔
收集整理的這篇文章主要介紹了
GridView实战二:使用ObjectDataSource数据源控件(自定义缓存机制实现Sort)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
? 參考資料:http://www.cnblogs.com/fsjohnhuang/archive/2011/12/17/2291200.html
因?yàn)槭褂肙bjectDataSource自帶的緩存機(jī)制無法實(shí)現(xiàn)排序功能,苦苦尋覓終于找到了解決方案。參考后覺得還是自己實(shí)操一下比較安心,下面是對(duì)《GridView實(shí)戰(zhàn)二:使用ObjectDataSource數(shù)據(jù)源控件》的改進(jìn)!!
? 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;}
}
?
實(shí)現(xiàn)預(yù)加載(proactive loading)
Global.asax
void Application_Start(object sender, EventArgs e) {// 在應(yīng)用程序啟動(dòng)時(shí)運(yùn)行的代碼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;}
總結(jié)
以上是生活随笔為你收集整理的GridView实战二:使用ObjectDataSource数据源控件(自定义缓存机制实现Sort)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。