单选:
{
}
多选:
SPFieldMultiChoice ss = list.Fields["分类"] as SPFieldMultiChoice;
foreach (string cc in ss.Choices)
foreach (string cc in ss.Choices)
{
ckType.Items.Add(cc);//用于多选的checkboxList
}
多选字段值的获得
SPFieldMultiChoice chInjury = (SPFieldMultiChoice)m_listItem.Fields["Injury"];
string valueInjury = chInjury.GetFieldValueAsText(m_listItem["Injury"]);
lbInjury.Text = valueInjury.Replace(", ", "<br/>");
List中创建MulitChoice类型的Field
MulitChoice类型的Field在List中是常用的,它可以用来标记一个Item的Tag,使用户在创建Item时适当的Tag标签,是很好的管理和检索标记;
在创建Field时,我们需要对MulitChoice类型的Field做相应的处理;下面我们就来创建一个MulitChoice类型的Field;
- privatestring[] mTagValue = newstring[] {"Asp.Net", "Microsoft", "C#"};
- privatestring mListName = "Article";
- publicvoid MakeList(string listName)
- {
- using (SPSite site = new SPSite(listName))
- {
- using (SPWeb web = site.OpenWeb())
- {
- SPList list = web.GetList(listName);
- if (list.Fields.ContainsField("Tag"))
- {
- SPField tagField = list.Fields["Tag"];
- if (!tagField.Type.Equals(SPFieldType.MultiChoice))
- {
- tagField.Type = SPFieldType.MultiChoice;
- tagField.Update();
- }
- SPFieldMultiChoice tagChoice = (SPFieldMultiChoice)tagField;
- if (tagChoice.Choices.Count == 0)
- {
- MakeTagValue(list);
- }
- }
- else
- {
- list.Fields.Add("Tag", SPFieldType.MultiChoice, false);
- MakeTagValue(list);
- }
- }
- }
- }
这里我们需要一个MakeTagValue这个方法对我们创建的Field添加我们需要的Choice;需要对Field的SchemaXml进行相应的操作;
- privatevoid MakeTagValue(SPList list)
- {
- try
- {
- SPField field = list.Fields["Tag"];
- XmlDocument xmlDoc = new XmlDocument();
- xmlDoc.LoadXml(field.SchemaXml);
- if(xmlDoc.GetElementsByTagName("CHOICES").Count == 0)
- {
- XmlElement xmlRoot = xmlDoc.CreateElement("CHOICES");
- xmlDoc.FirstChild.AppendChild(xmlRoot);
- }
- foreach (string tagValue in mTagValue)
- {
- XmlElement xmle = xmlDoc.CreateElement("CHOICE");
- xmle.InnerText = tagValue;
- xmlDoc.GetElementsByTagName("CHOICES")[0].AppendChild(xmle);
- }
- field.SchemaXml = xmlDoc.OuterXml;
- field.Update();
- }
- catch (Exception ex)
- { }
- }
在这里我就不截取Field的SchemaXml的代码了,有兴趣的读者可以使用你们的编辑器, 写一个Demo看看里面的结构;