制作一个输入IP地址的field
using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Security;
using System.Text.RegularExpressions;
namespace TomFieldControl
{
[CLSCompliant(false)]
[Guid("9f867628-532b-478e-804a-72c0ccc8b033")]
public class TomFieldControlField : SPFieldText
{
public TomFieldControlField(SPFieldCollection fields, string fieldName)
: base(fields, fieldName)
{
}
public TomFieldControlField(SPFieldCollection fields, string typeName, string displayName)
: base(fields, typeName, displayName)
{
}
public override BaseFieldControl FieldRenderingControl
{
[SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
get
{
BaseFieldControl fieldControl = new TomFieldControlFieldControl();
fieldControl.FieldName = this.InternalName;
return fieldControl;
}
}
public override string GetValidatedString(object value)
{
if (Required && (value == null || value.ToString() == ""))
throw new SPFieldValidationException(SPResource.GetString(Strings.MissingRequiredField));
string ipAdressStr = value.ToString();
//0-255
string ipAdressRE = @"^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$";
if (!Regex.IsMatch(ipAdressStr, ipAdressRE))
throw new SPFieldValidationException("Invalid IP Address.");
if (ipAdressStr == null)
return string.Empty;
else
return ipAdressStr;
}
}
}
field的定义布局:
using System;
using System.Runtime.InteropServices;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace TomFieldControl
{
[CLSCompliant(false)]
[Guid("b0c8454a-9d84-48de-9b2a-22f5e34d0fca")]
public class TomFieldControlFieldControl : BaseFieldControl
{
protected TextBox txtIP01;
protected TextBox txtIP02;
protected TextBox txtIP03;
protected TextBox txtIP04;
//The IPAdress Show on the page of SPControlMode.Display
protected Label labIP;
public TomFieldControlFieldControl()
{
this.txtIP01 = new TextBox();
this.txtIP02 = new TextBox();
this.txtIP03 = new TextBox();
this.txtIP04 = new TextBox();
this.labIP = new Label();
}
public override object Value
{
get
{
EnsureChildControls();
if (ControlMode == SPControlMode.Display)
{
return this.labIP.Text.ToString();
}
else
{
//if return the object of the class .How to the string
string txtStr = this.txtIP01.Text.ToString() + "." + this.txtIP02.Text.ToString() + "." + this.txtIP03.Text.ToString() + "." + this.txtIP04.Text.ToString();
return txtStr;
}
}
set
{
EnsureChildControls();
if (ControlMode == SPControlMode.Display)
{
this.labIP.Text = Value.ToString();
}
else
{
//opreater the IP Adress String
IPAdressClass TxtipclassValue = new IPAdressClass();
TxtipclassValue.getStr(Value);
this.txtIP01.Text = TxtipclassValue.ipAd01.ToString();
this.txtIP02.Text = TxtipclassValue.ipAd02.ToString();
this.txtIP03.Text = TxtipclassValue.ipAd03.ToString();
this.txtIP04.Text = TxtipclassValue.ipAd04.ToString();
}
}
}
public override void Focus()
{
EnsureChildControls();
if (ControlMode != SPControlMode.Display)
{
txtIP01.Focus();
txtIP02.Focus();
txtIP03.Focus();
txtIP04.Focus();
}
}
protected override void CreateChildControls()
{
try
{
//create new the Ip address
if (Field == null) return;
base.CreateChildControls();
if (ControlMode == SPControlMode.New)
{
base.Controls.Add(this.txtIP01);
base.Controls.Add(this.txtIP02);
base.Controls.Add(this.txtIP03);
base.Controls.Add(this.txtIP04);
}
if (ControlMode == SPControlMode.Display)
{
this.labIP.Text =this.ItemFieldValue.ToString();
base.Controls.Add(this.labIP);
}
if (ControlMode == SPControlMode.Edit)
{
//Carries on the edition to the Ip address
IPAdressClass TxtipclassValue =new IPAdressClass();
TxtipclassValue.getStr(this.ItemFieldValue);
this.txtIP01.Text = TxtipclassValue.ipAd01.ToString();
this.txtIP02.Text = TxtipclassValue.ipAd02.ToString();
this.txtIP03.Text = TxtipclassValue.ipAd03.ToString();
this.txtIP04.Text = TxtipclassValue.ipAd04.ToString();
base.Controls.Add(this.txtIP01);
base.Controls.Add(this.txtIP02);
base.Controls.Add(this.txtIP03);
base.Controls.Add(this.txtIP04);
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
protected override void Render(HtmlTextWriter output)
{
try
{
if (ControlMode == SPControlMode.Display)
{
this.labIP.RenderControl(output);
}
else
{
output.Write("<table><tr><td>");
this.txtIP01.RenderControl(output);
output.Write("</td><td>");
this.txtIP02.RenderControl(output);
output.Write("</td><td>");
this.txtIP03.RenderControl(output);
output.Write("</td><td>");
this.txtIP04.RenderControl(output);
output.Write("</td></tr>");
output.Write("<tr><td class='ms-formvalidation'>");
output.Write(ErrorMessage);
output.Write("</td></tr></table>");
}
}
catch (Exception ex)
{
output.Write(ex.Message);
}
}
}
//this class of the opreater the IP Adress String
[Serializable]
public class IPAdressClass
{
private string ipAdress01;
private string ipAdress02;
private string ipAdress03;
private string ipAdress04;
#region
public string ipAd01
{
get
{
return ipAdress01;
}
set
{
this.ipAdress01 = value;
}
}
public string ipAd02
{
get
{
return ipAdress02;
}
set
{
this.ipAdress02 = value;
}
}
public string ipAd03
{
get
{
return ipAdress03;
}
set
{
this.ipAdress03 = value;
}
}
public string ipAd04
{
get
{
return ipAdress04;
}
set
{
this.ipAdress04 = value;
}
}
#endregion
//The address carries on the division to Ip
public void getStr(object txtStr)
{
this.ipAdress01 = ((string)txtStr).Split('.')[0];
this.ipAdress02 = ((string)txtStr).Split('.')[1];
this.ipAdress03 = ((string)txtStr).Split('.')[2];
this.ipAdress04 = ((string)txtStr).Split('.')[3];
}
}
}
部署
然后右击项目属性--》debug--》url(个人站点)
签名--》new一个新的署名
重启IIS
这样在新建list时便可以使用。