写一个带属性的自定义列表栏,出现控件获取不到的问题网上寻找无果特来求助!!!
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using Microsoft.SharePoint; using Microsoft.SharePoint.WebControls; namespace Test { public class Test2 : SPFieldText { public Test2(SPFieldCollection fields, string fieldName) : base(fields, fieldName) { } public Test2(SPFieldCollection fields, string typeName, string displayName) : base(fields, typeName, displayName) { } public override string DefaultValue { get { return base.DefaultValue; } set { base.DefaultValue = value; } } //public override BaseFieldControl FieldRenderingControl //关联字段展示控件 //{ // get // { // BaseFieldControl fc = new Test3(); // fc.FieldName = this.InternalName; // return fc; // } //} public override BaseFieldControl FieldRenderingControl { get { return new Test3() { FieldName = base.InternalName }; } } } }
Test3.cs中的代码
C# code?using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using Microsoft.SharePoint; using Microsoft.SharePoint.WebControls; namespace Test { public class Test3 : BaseFieldControl { public Label show; public DropDownList ADD; public Label Display; protected override void OnLoad(EventArgs e) { base.OnLoad(e); if (Page.IsPostBack) { //Sync up the image and the urlHolder for each post back if we are in edit/new mode if (base.ControlMode == SPControlMode.Edit || base.ControlMode == SPControlMode.New) { } } } //重写默认模板 protected override string DefaultTemplateName { get { if (this.ControlMode == SPControlMode.Display) { return this.DisplayTemplateName; } else { return "DefaultCustomFieldControl"; } } } public override string DisplayTemplateName { get { return "DisplayCustomFieldControl"; } set { base.DisplayTemplateName = value; } } //重写控件生成方法 protected override void CreateChildControls() { if (base.Field == null) { return; } base.CreateChildControls(); Index(); if (this.ControlMode == SPControlMode.Display) { Display.Text = this.Display.Text; Display.BackColor = System.Drawing.Color.FromName(Display.Text); } else { string strHeight = base.Field.GetCustomProperty("CustomFieldProperty").ToString(); string[] arr = strHeight.Split(','); for (int i = 0; i < arr.Length; i++) { this.ADD.Items.Add(arr[i]); } show.Text = ADD.SelectedValue; show.BackColor = System.Drawing.Color.FromName(ADD.SelectedValue); } } public void DDLList_OnSelectedIndexChanged(Object sender, EventArgs e) { show.Text = ADD.SelectedValue; show.BackColor = System.Drawing.Color.FromName(ADD.SelectedValue); } public void Index() { this.Display = (Label)TemplateContainer.FindControl("LBDisplay"); if (Display ==null) { throw new ArgumentException("未找到LBDisplay控件"); } this.ADD = (DropDownList)TemplateContainer.FindControl("DDLList"); if (ADD == null) { throw new ArgumentException("未找到LBDisplay控件"); } this.show = (Label)TemplateContainer.FindControl("LBShow"); if (show == null) { throw new ArgumentException("未找到LBDisplay控件"); } } public override object Value { get { EnsureChildControls(); if (show != null) { return show.Text; } else { return null; } } set { EnsureChildControls(); if (show != null) { show.Text = (String)value; } } } } }
Test3.ascx中的代码
C# code?<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %> <%@ Assembly Name="Microsoft.Web.CommandUI, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Register TagPrefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Register TagPrefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Register TagPrefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %> <%@ Import Namespace="Microsoft.SharePoint" %> <%@ Register TagPrefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Register TagPrefix="PublishingWebControls" Assembly="Microsoft.SharePoint.Publishing, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" Namespace="Microsoft.SharePoint.Publishing.WebControls" %> <%@ Control Language="C#"Inherits="Test3.cs" %> <SharePoint:RenderingTemplate ID="DefaultCustomFieldControl" runat="server"> <Template> <asp:Label ID="LBShow" runat="server" Text=""></asp:Label> <asp:DropDownList ID="DDLList" runat="server" OnSelectedIndexChanged="DDLList_OnSelectedIndexChanged"></asp:DropDownList> </Template> </SharePoint:RenderingTemplate> <SharePoint:RenderingTemplate ID="DisplayCustomFieldControl" runat="server"> <Template> </Template> </SharePoint:RenderingTemplate>
fldtypes_Test.xml中的代码
C# code?<?xml version="1.0" encoding="utf-8" ?> <FieldTypes> <FieldType> <Field Name="TypeName">测试</Field> <Field Name="ParentType">Text</Field> <Field Name="TypeDisplayName">测试</Field> <Field Name="TypeShortDescription">测试</Field> <Field Name="UserCreatable">TRUE</Field> <Field Name="FieldTypeClass"> Test.Test2, $SharePoint.Project.AssemblyFullName$ </Field> <PropertySchema> <Fields> <Field Name="CustomFieldProperty" DisplayName="输入颜色支持英文与字符指定若使用字符加#,以‘,’分割" Type="Text" ></Field> </Fields> </PropertySchema> </FieldType> </FieldTypes>
ASCX !!居然传错码了简直无情
<SharePoint:RenderingTemplate ID="DefaultCustomFieldControl" runat="server"> <Template> <asp:Label ID="LBShow" runat="server" Text=""></asp:Label> <asp:DropDownList ID="DDLList" runat="server" OnSelectedIndexChanged="DDLList_OnSelectedIndexChanged()"></asp:DropDownList> </Template> </SharePoint:RenderingTemplate> <SharePoint:RenderingTemplate ID="DisplayEColorFieldControl" runat="server"> <Template> <asp:Label ID="LBDisplay" runat="server" Text=""></asp:Label> </Template> </SharePoint:RenderingTemplate>