Sharepoint自定义字段

时间:2011-07-19 15:31:20

标签: c# .net sharepoint-2010

我已经创建了一个自定义字段类型,我认为没有错误,因为它很简单,但字段框没有显示在表单中(参见图片)

pic

.ascx文件:

 <SharePoint:RenderingTemplate ID="MyField" runat="server">
 <Template>
 <asp:TextBox ID="TextField" MaxLength="255" runat="server" BackColor="Pink"
 Font-Bold="true" BorderStyle="Dotted" BorderColor="DarkBlue" TextMode="MultiLine" />
 </Template>
 </SharePoint:RenderingTemplate>

字段类型文件:

namespace MyCustomField.CustomField
{
class SPFieldMyCustomField : SPFieldMultiLineText
{

    public SPFieldMyCustomField(SPFieldCollection fields, string fieldName) : base(fields, fieldName)
    {
    }

    public SPFieldMyCustomField(SPFieldCollection fields, string typeName, string displayName)
        : base(fields, typeName, displayName)
    {
    }

    public override BaseFieldControl FieldRenderingControl
    {
        get
        {
            BaseFieldControl control = new MyCustomFieldControl();
            control.FieldName = base.InternalName;

            control.ControlMode = SPControlMode.Display;

            return control;
        }
    }
}
}

和控制文件:

namespace MyCustomField.CustomField
{
internal class MyCustomFieldControl : RichTextField
{
    protected override void CreateChildControls()
    {
        ControlMode = SPControlMode.Display;

        base.CreateChildControls();
    }

    protected override void RenderFieldForDisplay(HtmlTextWriter output)
    {
        var html = String.IsNullOrEmpty(Item[Field.InternalName] as string) ? "" : Item[Field.InternalName] as string;

        RenderHtmlForDisplay(output, html);
    }

    protected override string DefaultTemplateName
    {
        get
        {
            return "MyField"; 
        }
    } 
}
}

如您所见,未显示TextBox。

1 个答案:

答案 0 :(得分:3)

看起来控件设置为仅呈现SPControlMode.Display。您的屏幕截图显示在EditNew模式

您似乎想要覆盖RenderFieldForInput方法

protected override void RenderFieldForInput(HtmlTextWriter output)
{
    var html = String.IsNullOrEmpty(Item[Field.InternalName] as string) ? "" : Item[Field.InternalName] as string;

    RenderHtmlForDisplay(output, html);
}