sharepoint 自定义字段实现省市联动
最后实现效果如下:
设置栏如下:
解决方案结构如下:
fldtypes_RoyCustomField.xml 内容如下:
<?xml version="1.0" encoding="utf-8" ?>
<FieldTypes>
<FieldType>
<Field Name="TypeName">RoyCustomField</Field>
<Field Name="ParentType">Text</Field>
<Field Name="TypeDisplayName">RoyCustomField</Field>
<Field Name="TypeShortDescription">RoyCustomField</Field>
<Field Name="UserCreatable">TRUE</Field>
<Field Name="FieldTypeClass">RoyCustomField.RoyCustomField,$SharePoint.Project.AssemblyFullName$</Field>
</FieldType>
</FieldTypes>
<FieldTypes>
<FieldType>
<Field Name="TypeName">RoyCustomField</Field>
<Field Name="ParentType">Text</Field>
<Field Name="TypeDisplayName">RoyCustomField</Field>
<Field Name="TypeShortDescription">RoyCustomField</Field>
<Field Name="UserCreatable">TRUE</Field>
<Field Name="FieldTypeClass">RoyCustomField.RoyCustomField,$SharePoint.Project.AssemblyFullName$</Field>
</FieldType>
</FieldTypes>
RoyCustomField.cs 内容如下:
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Web.UI.WebControls;
using System.Web.UI;
using System;
namespace RoyCustomField
{
public class RoyCustomField : SPFieldText
{
public RoyCustomField(SPFieldCollection field, string strFieldName)
: base(field, strFieldName)
{
}
public RoyCustomField(SPFieldCollection field, string strFieldName, string strDispName)
: base(field, strFieldName, strDispName)
{
}
public override BaseFieldControl FieldRenderingControl
{
get
{
BaseFieldControl fc = new RoyText();
fc.FieldName = this.InternalName;
return fc;
}
}
}
public class RoyText : BaseFieldControl
{
private DropDownList _ddlProvince;
private DropDownList _ddlCity;
#region Province of autoPostBack
void Province_SelectedIndexChanged(object sender, EventArgs e)
{
_ddlCity.Items.Clear();
if (_ddlProvince.SelectedValue.ToString() == "河北省")
{
_ddlCity.Items.Add(new ListItem("石家庄市", "石家庄市"));
}
else if (_ddlProvince.SelectedValue.ToString() == "山东省")
{
_ddlCity.Items.Add(new ListItem("济南市", "济南市"));
}
else
{
_ddlCity.Items.Add(new ListItem("北京市", "北京市"));
}
}
#endregion
protected override void CreateChildControls()
{
base.CreateChildControls();
this.TemplateContainer.FindControl("_ddlProvince");
_ddlProvince = new DropDownList();
_ddlProvince.Items.Add(new ListItem("河北省", "河北省"));
_ddlProvince.Items.Add(new ListItem("山东省", "山东省"));
_ddlProvince.AutoPostBack = true;
_ddlProvince.SelectedIndexChanged += Province_SelectedIndexChanged;
this.Controls.Add(_ddlProvince);
this.Controls.Add(new LiteralControl(" - "));
this.TemplateContainer.FindControl("_ddlCity");
_ddlCity = new DropDownList();
_ddlCity.Items.Add(new ListItem("石家庄市", "石家庄市"));
_ddlCity.Items.Add(new ListItem("济南市", "济南市"));
this.Controls.Add(_ddlCity);
}
public override object Value
{
get
{
this.EnsureChildControls();
return _ddlProvince.Text+"-"+_ddlCity.Text;
}
set
{
string fv = (string)this.ItemFieldValue;
if (fv.Contains("-"))
{
string[] all = fv.Split('-');
_ddlProvince.Text = all[0];
_ddlCity.Text = all[1];
}
}
}
}
}
using Microsoft.SharePoint.WebControls;
using System.Web.UI.WebControls;
using System.Web.UI;
using System;
namespace RoyCustomField
{
public class RoyCustomField : SPFieldText
{
public RoyCustomField(SPFieldCollection field, string strFieldName)
: base(field, strFieldName)
{
}
public RoyCustomField(SPFieldCollection field, string strFieldName, string strDispName)
: base(field, strFieldName, strDispName)
{
}
public override BaseFieldControl FieldRenderingControl
{
get
{
BaseFieldControl fc = new RoyText();
fc.FieldName = this.InternalName;
return fc;
}
}
}
public class RoyText : BaseFieldControl
{
private DropDownList _ddlProvince;
private DropDownList _ddlCity;
#region Province of autoPostBack
void Province_SelectedIndexChanged(object sender, EventArgs e)
{
_ddlCity.Items.Clear();
if (_ddlProvince.SelectedValue.ToString() == "河北省")
{
_ddlCity.Items.Add(new ListItem("石家庄市", "石家庄市"));
}
else if (_ddlProvince.SelectedValue.ToString() == "山东省")
{
_ddlCity.Items.Add(new ListItem("济南市", "济南市"));
}
else
{
_ddlCity.Items.Add(new ListItem("北京市", "北京市"));
}
}
#endregion
protected override void CreateChildControls()
{
base.CreateChildControls();
this.TemplateContainer.FindControl("_ddlProvince");
_ddlProvince = new DropDownList();
_ddlProvince.Items.Add(new ListItem("河北省", "河北省"));
_ddlProvince.Items.Add(new ListItem("山东省", "山东省"));
_ddlProvince.AutoPostBack = true;
_ddlProvince.SelectedIndexChanged += Province_SelectedIndexChanged;
this.Controls.Add(_ddlProvince);
this.Controls.Add(new LiteralControl(" - "));
this.TemplateContainer.FindControl("_ddlCity");
_ddlCity = new DropDownList();
_ddlCity.Items.Add(new ListItem("石家庄市", "石家庄市"));
_ddlCity.Items.Add(new ListItem("济南市", "济南市"));
this.Controls.Add(_ddlCity);
}
public override object Value
{
get
{
this.EnsureChildControls();
return _ddlProvince.Text+"-"+_ddlCity.Text;
}
set
{
string fv = (string)this.ItemFieldValue;
if (fv.Contains("-"))
{
string[] all = fv.Split('-');
_ddlProvince.Text = all[0];
_ddlCity.Text = all[1];
}
}
}
}
}
注意事项:
1.xml的文件名必须以fldtypes_开头,否则不能识别。
2.如果要设置默认值请Override DefaultValue,如果要验证格式是否正确请Override GetValidatedString。
本文参考:http://www.cnblogs.com/Roy_Cao/archive/2012/08/14/2637542.html