|
In this example we are creating a custom field type which concatenates two text fields (first and last name).
<%@ Control Language="C#" %> <%@ Assembly Name="CustomControls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=xxxxxxxxxxxxxxxx" %> <%@ Register TagPrefix="SharePoint" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" Namespace="Microsoft.SharePoint.WebControls" %>
<SharePoint:RenderingTemplate Id="CustomFieldRendering" runat="server"> <Template> <asp:TextBox runat="server" ID="txtFirstName" /> <asp:TextBox runat="server" ID="txtLastName" /> </Template> </SharePoint:RenderingTemplate> using System; using System.Collections.Generic; using System.Text; using System.Web.UI.WebControls;
using Microsoft.SharePoint; using Microsoft.SharePoint.WebControls;
namespace CustomControl { public class customfieldcontrol : BaseFieldControl { protected TextBox txtFirstName; protected TextBox txtLastName;
protected override string DefaultTemplateName { get { return "CustomFieldRendering"; } } public override object Value { get { EnsureChildControls(); return txtFirstName.Text + "%" + txtLastName.Text; } set { try { EnsureChildControls(); txtFirstName.Text = value.ToString().Split('%')[0]; txtLastName.Text = value.ToString().Split('%')[1]; } catch { } } } public override void Focus() { EnsureChildControls(); txtFirstName.Focus(); } protected override void CreateChildControls() { if (Field == null) return; base.CreateChildControls();
//Don't render the textbox if we are just displaying the field if (ControlMode == Microsoft.SharePoint.WebControls.SPControlMode.Display) return;
txtFirstName = (TextBox)TemplateContainer.FindControl("txtFirstName"); txtLastName = (TextBox)TemplateContainer.FindControl("txtLastName"); if (txtFirstName == null) throw new NullReferenceException("txtFirstName is null"); if (txtLastName == null) throw new NullReferenceException("txtLastName is null"); if (ControlMode == Microsoft.SharePoint.WebControls.SPControlMode.New) { txtFirstName.Text = ""; txtLastName.Text = ""; } } } } Actually, what this code does is: Define the ID of the renderingtemplate control in our usercontrol. |