前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >GridView实战二:使用ObjectDataSource数据源控件(自定义缓存机制实现Sort)

GridView实战二:使用ObjectDataSource数据源控件(自定义缓存机制实现Sort)

作者头像
^_^肥仔John
发布2018-01-18 11:06:04
5890
发布2018-01-18 11:06:04
举报

因为使用ObjectDataSource自带的缓存机制无法实现排序功能,苦苦寻觅终于找到了解决方案。参考后觉得还是自己实操一下比较安心,下面是对《GridView实战二:使用ObjectDataSource数据源控件》的改进!!

  CL代码:

代码语言:javascript
复制
 1 public class CL
 2 {
 3     private OdsDataManager om = new OdsDataManager();
 4     private static string[] mainKey = {"ods"};
 5 
 6     public CL()
 7     {
 8     }
 9 
10     public DataTable GetRecord(int maximumRows, int startRowIndex, string sortExpression)
11     {
12         DataTable dt = HttpRuntime.Cache[GetCacheKey(Convert.ToString(maximumRows) + startRowIndex)] as DataTable;
13         if (dt == null)
14         {
15             dt = om.GetRecord(maximumRows, startRowIndex, sortExpression);
16             AddCache(Convert.ToString(maximumRows) + startRowIndex, dt);
17         }
18         if (!string.IsNullOrEmpty(sortExpression))
19         {
20             DataTable tempDt = dt.Clone();
21             DataRow[] drs = dt.Select("",sortExpression);
22             foreach (DataRow dr in drs)
23             {
24                 tempDt.Rows.Add(dr.ItemArray);
25             }
26             dt = tempDt;
27         }
28 
29         return dt;
30     }
31 
32     public int GetRecordCount()
33     {
34         return om.GetRecordCount();
35     }
36 
37     public bool UpdateRecord(int ID, string Name, string Sex, string Country, string Hobby)
38     {
39         RemoveCache();
40         return om.UpdateRecord(ID,Name,Sex,Country,Hobby);
41     }
42 
43     public bool DelRecord(int ID)
44     {
45         RemoveCache();
46         return om.DelRecord(ID);
47     }
48 
49     public DataTable GetCountry()
50     {
51         DataTable countryDt = HttpRuntime.Cache["countryDt"] as DataTable;
52         return countryDt;
53     }
54 
55     public DataTable GetHobby()
56     {
57         DataTable hobbyDt = HttpRuntime.Cache["hobbyDt"] as DataTable;
58         return hobbyDt;
59     }
60 
61     private void AddCache(string key, object data)
62     {
63         System.Web.Caching.Cache dc = HttpRuntime.Cache;
64         if (dc[mainKey[0]] == null)
65             dc.Insert(mainKey[0], DateTime.Now);
66 
67         System.Web.Caching.CacheDependency cd = new System.Web.Caching.CacheDependency(null, mainKey);
68         dc.Insert(GetCacheKey(key), data, cd, System.Web.Caching.Cache.NoAbsoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration);
69     }
70 
71     private void RemoveCache()
72     {
73         System.Web.Caching.Cache dc = HttpRuntime.Cache;
74         if (dc[mainKey[0]] != null)
75             dc[mainKey[0]] = DateTime.Now;
76     }
77 
78     private string GetCacheKey(string key)
79     {
80         return mainKey[0] + "-" + key;
81     }
82 }

实现预加载(proactive loading) Global.asax

代码语言:js
复制
 1     void Application_Start(object sender, EventArgs e) 
 2     {
 3         // 在应用程序启动时运行的代码
 4         OdsDataManager om = new OdsDataManager();
 5         HttpRuntime.Cache.Insert("countryDt", om.GetCountry(), null,
 6             System.Web.Caching.Cache.NoAbsoluteExpiration,
 7             System.Web.Caching.Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable, null);
 8         HttpRuntime.Cache.Insert("hobbyDt", om.GetHobby(), null,
 9             System.Web.Caching.Cache.NoAbsoluteExpiration,
10             System.Web.Caching.Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable, null);
11         om = null;
12     }

  参考资料:http://www.cnblogs.com/fsjohnhuang/archive/2011/12/17/2291200.html

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2011-12-19 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档