之前有人给我推荐了Data Cogs Information Technology发布的Cascading Drop Down Lists,一个支持二级联动的分开的Field Type,经过大家测试,美中不足就是对中文名称字段的支持不好,会报错,呵呵,今天腾下时间对此进行修复。
在ChildDropDownListFieldControl.cs文件中的SetDataSource方法,这个方法是设置子字段DropDownList数据源及其内容的方法,其中有这么几句话:
1
SPSite site = new SPSite(childSiteUrl);
2
SPList list = site.OpenWeb().Lists[childListName];
3
4
string caml = @"<Where>
5
<Eq>
6
<FieldRef Name='{0}'/><Value Type='Text'>{1}</Value>
7
</Eq></Where>";
8
9
SPQuery query = new SPQuery();
10
query.Query = string.Format(caml, childJoinField, parentSelectedValue);
11
问题就在这里,这个childJoinField直接使用前面的属性读取值,如下
2

3

4

5

6

7

8

9

10

11

1
childSiteUrl = Field.GetCustomProperty("ChildSiteUrl").ToString();
2
childListName = Field.GetCustomProperty("ChildListName").ToString();
3
childListTextField = Field.GetCustomProperty("ChildListTextField").ToString();
4
childListValueField = Field.GetCustomProperty("ChildListValueField").ToString();
5
childJoinField = Field.GetCustomProperty("ChildJoinField").ToString();
6
我们知道,在构成CAML查询语句的时候如果要查询字段,是需要使用childJoinField内部名称InternalName的,但是这里直接使用的显示名称,所以CAML语句拼写出现了错误,造成查询不到相应的问题,导致后面的
2

3

4

5

6

this.ChildDropDownList.DataSource = results.GetDataTable();
报错。
所以我们只要修复这个问题就OK了。
1
SPSite site = new SPSite(childSiteUrl);
2
SPList list = site.OpenWeb().Lists[childListName];
3
4
string caml = @"<Where>
5
<Eq>
6
<FieldRef Name='{0}'/><Value Type='Text'>{1}</Value>
7
</Eq></Where>";
8
9
SPQuery query = new SPQuery();
10
// 修改的这里:)注释了原来的那句话
11
// 将构建CAML的字段由显示名称改为内部名称就OK了:)
12
//query.Query = string.Format(caml, childJoinField, parentSelectedValue);
13
childJoinField = list.Fields[childJoinField].InternalName;
14
query.Query = string.Format(caml, childJoinField, parentSelectedValue);
15

2

3

4

5

6

7

8

9

10

11

12

13

14

15

最后,给出按此修复后的组件和源代码下载,希望对大家有所帮助:)
组件
修复后的源代码
注:Project里面的wsp包我没有重新打包,如果大家有需要我会抽时间重新打包,时间紧迫,望大家理解,呵呵:)
另:
我在调试的时候发现了一个问题:
中文名称字段创建的时候,有时会调用两次Update,导致第二次取不到值,把第一次存储的值全给刷空了,如果要确定这个就需要在创建字段后进入到找个字段的编辑界面看看刚才的设定值是否都在,如果不在的话就是被刷了,重新建立这个字段吧……具体原因尚未查明,但是建立好后就没问题了。