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

时间:2022-04-22
本文章向大家介绍GridView实战二:使用ObjectDataSource数据源控件(自定义缓存机制实现Sort),主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

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

  CL代码:

 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

 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