簡體   English   中英

自定義FieldRenderingControl Sharepoint 2010

[英]Custom FieldRenderingControl Sharepoint 2010

我在空白共享點項目中創建了一個自定義字段,並且重寫了FieldRenderingControl,以便在顯示列表中的項目時可以創建自己的表布局。

我遇到的問題是呈現控件類中的ItemFieldValue始終為null。

如何獲取要顯示的字段的字段值?

這是我的自定義字段類

namespace CustomFieldDefinitions.Fields
{
    public class AttributeField : SPField
    {
        #region Constructors

        /// <summary>
        /// This is a constuctor with two parameters.
        /// </summary>
        /// <param name="fields"></param>
        /// <param name="fieldName"></param>
        public AttributeField(SPFieldCollection fields, string fieldName)
            : base(fields, fieldName)
        {
        }

        /// <summary>
        /// This is a contructor with three parameters.
        /// </summary>
        /// <param name="fields"></param>
        /// <param name="typeName"></param>
        /// <param name="displayName"></param>
        public AttributeField(SPFieldCollection fields, string typeName, string displayName)
            : base(fields, typeName, displayName)
        {
        }

        #endregion

        #region Overridden Properties

        /// <summary>
        /// This ties the control used to support this field with the current implementation of it.
        /// </summary>
        public override BaseFieldControl FieldRenderingControl
        {
            [SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
            get
            {
                BaseFieldControl fieldControl = new AttributeFieldControl();
                fieldControl.FieldName = this.InternalName;
                return fieldControl;
            }
        }


        public override object GetFieldValue(string value)
        {
            return base.GetFieldValue(value);
        }

        #endregion

    }
}

還有我的FieldRenderingControl類

namespace CustomFieldDefinitions.FieldControls
{
    public class AttributeFieldControl : BaseFieldControl
    {
        protected Label AttributeValueForDisplay;
        protected TextBox AttributeValueTextbox;

        public override string DisplayTemplateName
        {
            get
            {
                 return "AttributeFieldDisplayControl";
            }
            set
            {
                base.DisplayTemplateName = value;
            }
        }

        protected override string DefaultTemplateName
        {
            get
            {
                 if (this.ControlMode == SPControlMode.Display)
                {
                    return this.DisplayTemplateName;
                }
                else
                {
                    return "AttributeFieldControl";
                }
            }
        }

        protected override void CreateChildControls()
        {
            if (this.Field != null)
            {
                 base.CreateChildControls();

                 this.AttributeValueForDisplay = (Label)TemplateContainer.FindControl("lblAttValue");
                 this.AttributeValueTextbox = (TextBox)TemplateContainer.FindControl("txtAttValue");

                 if (ControlMode == SPControlMode.New || ControlMode == SPControlMode.Edit)
                 {
                     AttributeValueTextbox.Text = Convert.ToString(this.ListItemFieldValue);
                 }
                 else
                 {
                     AttributeValueForDisplay.Text = Convert.ToString(this.ListItemFieldValue);
                 }
            }
        }
    }
}

最后是標記

<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %> 
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" %>
<SharePoint:RenderingTemplate ID="AttributeFieldDisplayControl" runat="server">
    <Template>
        <asp:Label ID="lblAttValue" runat="server" BorderColor="Red"></asp:Label>
    </Template>
</SharePoint:RenderingTemplate>
<SharePoint:RenderingTemplate ID="AttributeFieldControl" runat="server">
    <Template>
        <asp:TextBox ID="txtAttValue" runat="server" BorderColor="Red"></asp:TextBox>
    </Template>
</SharePoint:RenderingTemplate>

請隨函附上該問題的答案。 我是個笨蛋。 當調用BdcModel的ReadItem方法時,我沒有設置要傳遞到顯示頁面的對象的'AttributeValue'屬性。

因此,一旦我添加完一切,一切都很好。 謝謝所有看過這篇文章的人。 我很抱歉浪費您的時間。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM