博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
GridView实战二:使用ObjectDataSource数据源控件
阅读量:6859 次
发布时间:2019-06-26

本文共 10939 字,大约阅读时间需要 36 分钟。

前言:

  ObjectDataSource数据源控件优点甚多,确实令人爱不惜手,但不支持重绑定这一项确实让人失望。下面的实战二将通过ObjectDataSource配合GridView来实现删、改、分页、排序,并分析使用cache后排序失灵的原因。

 

实战:

1.效果:

图1.显示状态

图2.编辑状态

 

2.代码:

.aspx

1 
5
6
9
10
11
12
13
14
15
16
17
<%#Eval("Name") %>
18
19
20
22
23
25
26
27
28
29
30
31
32
33
34
35
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<%#Eval("Country")%>
51
52
53
54
55
56
57
Hobby
58
<%#Eval("Hobby") %>
59
60
61
62
63
64
65
66
67
68
69
70 当前第<%#this.gv.PageIndex+1 %>/<%#this.gv.PageCount %>页 71
72
73
74
76
77
78
跳转到第
79
80

说明:

1.因用了数据源控件,所以Name在编辑状态时使用<%#Bind("Name")%>来实现双向通讯的绑定

2.因为没有添加的功能,所以用了asp:CommandField来实现编辑、删除等按钮的功能。

3.排序功能上只要在ods上设定SortParameterName,它的值就是SelectMethod中关于排序的参数的名称,然后设定GridView的AllowSorting为true就ok了。排序按钮上依然用到GridView内置的CommandName——Sort,然后CommandArgument设为要排序的字段名,至于排序的方向由ObjectDataSource负责,省心多了。

 

.aspx.cs代码

1 public partial class Default2 : System.Web.UI.Page   2 {
3 private OdsDataManager dm = new OdsDataManager(); 4 5 protected void Page_Load(object sender, EventArgs e) 6 {
7 8 } 9 10 protected void gv_OnRowDataBound(object sender, GridViewRowEventArgs e) 11 {
12 DataRowView drv = e.Row.DataItem as DataRowView; 13 14 if (e.Row.RowType == DataControlRowType.DataRow) 15 {
16 //显示时 17 if (this.gv.EditIndex == -1) 18 {
19 //设置性别 20 RadioButtonList rbl = e.Row.FindControl("rblSexShow") as RadioButtonList; 21 rbl.Items.Add(new ListItem("Male", "M")); 22 rbl.Items.Add(new ListItem("Female", "F")); 23 if ((drv["Sex"] as string).ToLower().Equals("m")) 24 rbl.Items[0].Selected = true; 25 else 26 rbl.Items[1].Selected = true; 27 } 28 //修改时: 29 else if (e.Row.RowIndex == this.gv.EditIndex) 30 {
31 //性别 32 RadioButtonList rbl = e.Row.FindControl("rblSexEdit") as RadioButtonList; 33 rbl.Items.Add(new ListItem("Male", "M")); 34 rbl.Items.Add(new ListItem("Female", "F")); 35 if ((drv["Sex"] as string).ToLower().Equals("m")) 36 rbl.Items[0].Selected = true; 37 else 38 rbl.Items[1].Selected = true; 39 //国籍 40 DropDownList ddlCountry = e.Row.FindControl("ddlCountry") as DropDownList; 41 DataTable countryDt = dm.GetCountry(); 42 ListItem li = null; 43 for (int i = 0; i < countryDt.Rows.Count; ++i) 44 {
45 string cn = countryDt.Rows[i]["cn"] as string; 46 li = new ListItem(cn, cn); 47 if (cn.Equals(drv["Country"] as string)) 48 li.Selected = true; 49 ddlCountry.Items.Add(li); 50 } 51 //兴趣 52 CheckBoxList cbl = e.Row.FindControl("cbxlHobby") as CheckBoxList; 53 DataTable hobbyDt = dm.GetHobby(); 54 string hobbys = drv["Hobby"] as string; 55 ListItem hobbyLi = null; 56 string hstr = string.Empty; 57 for (int i = 0; i < hobbyDt.Rows.Count; i++) 58 {
59 hstr = hobbyDt.Rows[i]["hobby"] as string; 60 hobbyLi = new ListItem(hstr, hstr); 61 if (hobbys.IndexOf(hstr) >= 0) 62 hobbyLi.Selected = true; 63 cbl.Items.Add(hobbyLi); 64 } 65 } 66 } 67 else if (e.Row.RowType == DataControlRowType.Pager) 68 {
69 //绑定分页控件 70 DropDownList ddlPaging = e.Row.FindControl("ddlPaging") as DropDownList; 71 for (int i = 0; i < this.gv.PageCount; i++) 72 {
73 ddlPaging.Items.Add(new ListItem(Convert.ToString(i + 1), Convert.ToString(i))); 74 } 75 ddlPaging.SelectedIndex = this.gv.PageIndex; 76 } 77 } 78 79 /// 80 /// 分页控件的OnSelectedIndexChanged 81 /// 82 /// 83 /// 84 protected void ddlPaging_OnSelectedIndexChanged(object sender, EventArgs e) 85 {
86 this.gv.PageIndex = (sender as DropDownList).SelectedIndex; 87 } 88 89 protected void ods_OnUpdating(object sender, ObjectDataSourceMethodEventArgs e) 90 {
91 string Sex = (this.gv.Rows[this.gv.EditIndex].FindControl("rblSexEdit") as RadioButtonList).SelectedValue; 92 string Country = (this.gv.Rows[this.gv.EditIndex].FindControl("ddlCountry") as DropDownList).SelectedValue; 93 System.Text.StringBuilder hobbys = new System.Text.StringBuilder(); 94 foreach (ListItem li in (this.gv.Rows[this.gv.EditIndex].FindControl("cbxlHobby") as CheckBoxList).Items) 95 {
96 if (li.Selected) 97 hobbys.Append(li.Value+","); 98 } 99 if (hobbys.Length >= 2) 100 hobbys.Remove(hobbys.Length - 1, 1); 101 102 e.InputParameters.Add("Sex", Sex); 103 e.InputParameters.Add("Country", Country); 104 e.InputParameters.Add("Hobby",hobbys.ToString()); 105 } 106 }

说明:

1.看到behind code是不是发现代码量少了很多呢?这就是用ods的好处了。

2.在更新操作时,因为Country、Sex和Hobby都没有和ods作双向绑定,所以要自己获取并写入到ods的InputParameters中,然后ods就会调用已经设置好的UpdateMethod了。

 

数据操作类

1 public class OdsDataManager   2 {
3 private static DataTable dt = null;//用户记录 4 private static DataTable countryDt = null;//国籍 5 private static DataTable hobbyDt = null;//兴趣 6 7 public OdsDataManager() 8 {
9 if (dt == null) 10 {
11 dt = new DataTable(); 12 dt.Columns.Add("ID"); 13 dt.Columns.Add("Name"); 14 dt.Columns.Add("Sex"); 15 dt.Columns.Add("Country"); 16 dt.Columns.Add("Hobby"); 17 18 //Default Data 19 dt.Rows.Add(new object[] { 1, "Mary", "F", "China", "Cooking,Music" }); 20 dt.Rows.Add(new object[] { 2, "John", "M", "China", "Tennis" }); 21 } 22 23 if (countryDt == null) 24 {
25 countryDt = new DataTable(); 26 countryDt.Columns.Add("cn"); 27 28 //Default Data 29 countryDt.Rows.Add(new object[] { "China" }); 30 countryDt.Rows.Add(new object[] { "French" }); 31 countryDt.Rows.Add(new object[] { "America" }); 32 countryDt.Rows.Add(new object[] { "Afria" }); 33 countryDt.Rows.Add(new object[] { "Japan" }); 34 } 35 36 if (hobbyDt == null) 37 {
38 hobbyDt = new DataTable(); 39 hobbyDt.Columns.Add("hobby"); 40 41 //Default Data 42 hobbyDt.Rows.Add(new object[] { "Cooking" }); 43 hobbyDt.Rows.Add(new object[] { "Music" }); 44 hobbyDt.Rows.Add(new object[] { "Reading" }); 45 hobbyDt.Rows.Add(new object[] { "Movies" }); 46 hobbyDt.Rows.Add(new object[] { "Tennis" }); 47 } 48 } 49 50 public DataTable GetRecord(int maximumRows, int startRowIndex, string sortExpression) 51 {
52 //排序 53 if(!string.IsNullOrEmpty(sortExpression)) 54 {
55 dt.DefaultView.Sort = sortExpression; 56 } 57 58 DataRow[] drs = dt.Select(); 59 DataTable dt1 = dt.Clone(); 60 for (int i = startRowIndex; i < startRowIndex+maximumRows && i

说明:

1.GetRecord方法绑定到ods的SelectMethod上,因为启用分页和排序功能,所以参数数组中必须有maximumRows(每页记录数), startRowIndex(当前页首条记录在整个数据集中的索引), sortExpression(排序表达式,首次加载页面时为空字符串,postback时含排序字段和排序方向)。

 

3.数据缓存

  ods可以启用cache,该cache为应用程序级的,就是多个画面的ods只要SelectMethod和SelectCountMethod、Select参数一样就可以共享缓存中的数据,在Cache有效时进行Select操作将会先根据前面说的三个要素从Cache中获取数据,如果没有才执行SelectMethod方法。注意不同的要素组合会各自对应一份缓存的数据,当第二次请求时就直接读缓存。

  就是因为这样问题就来了,如果启用了cache那么上面的排序功能就会失效,而其他功能依然正常。原因在于排序操作是在SelectMethod中实现,而在Cache生效时程序根本就不执行SelectMethod方法,除非说内存不足或其他原因令cache不够大来保存数据而被迫执行SelectMethod方法。对于该问题目前还没找到解决的方法,望大哥们来告诉我啦^_^

好消息:对于上面的问题终于找到了解决方法,就是自定义一个缓存层而不使用ods附带的缓存功能。具体请看:

 

如果您觉得本文的内容有趣就扫一下吧!捐赠互勉!

本文转自^_^肥仔John博客园博客,原文链接:http://www.cnblogs.com/fsjohnhuang/archive/2011/12/17/2291118.html,如需转载请自行联系原作者

你可能感兴趣的文章
Java八种基本数据类型的比较及其相互转化
查看>>
【Java编程规范】 代码书写规范...待续中
查看>>
Qml数据类型
查看>>
float浮点数的二进制存储方式及转换
查看>>
二手X61续
查看>>
如何在 CentOS 7 上禁用 SELinux
查看>>
Android有用代码片断(五)
查看>>
Git 版本库理解
查看>>
spring-boot项目在线生成工程使用(start.spring.io)
查看>>
tomcat修改jsessionid在cookie中的名称
查看>>
机器学习教程
查看>>
在 MinGW 中使用 OpenSSL 创建证书时的 BUG
查看>>
第二十三讲:tapestry条件与循环组件详解之if
查看>>
用js实现的刷新页面
查看>>
我有一个梦想
查看>>
iOS SDK 开发遇到问题集锦
查看>>
Python- You are using pip version 18.1, however version 19.1.1 is available.
查看>>
用java反射将map映射成java对象的简易实现,模拟BeanUtils
查看>>
C Primer Plus 第11章 字符串和字符串函数 11.4 自定义字符串I/O函数
查看>>
JBoss 系列五十五:JBoss 7/WildFly 集群之 HornetQ Messaging - III(示例补充说明)
查看>>