创建自定义表单模板
上次修改时间: 2010年9月22日
适用范围: SharePoint Foundation 2010
位于 %ProgramFiles%\Common Files\Microsoft Shared\web server extensions\14\TEMPLATE\CONTROLTEMPLATES\ 的 DefaultTemplates.ascx 文件中定义的 Microsoft ASP.NET 控件模板决定 Microsoft SharePoint Foundation 2010 列表项表单的布局。这些模板可以嵌套为表单布局定义轮流使用多个控件模板的控件。控件可以包含 HTML 标记和 Web 控件,但不能包含涉及 <%#...%> 语法的数据绑定语句。若要在前端 Web 服务器上全局替代默认控件模板,可以使用与 DefaultTemplates.ascx 文件中的现有控件模板的标识符 (ID) 相匹配的控件模板 ID 向 \CONTROLTEMPLATES 目录添加自定义 .ascx 文件。有关如何在 Microsoft Visual Studio 中创建用户控件的信息,请参阅如何:为 SharePoint 应用程序页面或 Web 部件创建用户控件。
创建自定义模板
除了替代默认模板,还可以创建自己的自定义表单模板,并按列表类型或内容类型对其加以引用。若要按列表类型注册自定义表单模板,请通过列表定义的 Schema.xml 文件中 <Form> 标记的 Template 属性来指定表单模板的名称,如下面的示例所示。
<Forms>
<Form Type="DisplayForm" Url="DispForm.aspx" SetupPath="pages\form.aspx" WebPartZoneID="Main" />
<Form Type="EditForm" Url="EditForm.aspx" SetupPath="pages\form.aspx" Template="MyCustomForm" WebPartZoneID="Main" />
<Form Type="NewForm" Url="NewForm.aspx" SetupPath="pages\form.aspx" Template="MyCustomForm" WebPartZoneID="Main" />
</Forms>
下面的示例定义另外两个自定义表单。
<Forms>
<Form
Type="DisplayForm"
Url="DispForm.aspx"
SetupPath="pages\form.aspx"
WebPartZoneID="Main" Default="FALSE"/>
<Form
Type="DisplayForm"
Url="MyDispForm.aspx"
SetupPath="pages\form.aspx"
WebPartZoneID="Main"
UseDefaultListFormWebPart="False"
Default="TRUE">
<WebParts>
<AllUsersWebPart WebPartZoneID="Main" WebPartOrder="3"><![CDATA[
<WebPart xmlns="https://schemas.microsoft.com/WebPart/v2">
<Assembly>Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly
<TypeName>Microsoft.SharePoint.WebPartPages.ListFormWebPart</TypeName>
<PageType>PAGE_DISPLAYFORM</PageType>
</WebPart>]]>
</AllUsersWebPart>
<AllUsersWebPart WebPartZoneID="Main" WebPartOrder="1"><![CDATA[
<WebPart xmlns="https://schemas.microsoft.com/WebPart/v2">
<Assembly>Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>
<TypeName>Microsoft.SharePoint.WebPartPages.ListViewWebPart</TypeName>
<PageType>PAGE_VIEW</PageType>
<ViewFlags>Hidden ReadOnly Html</ViewFlags>
</WebPart>]]>
</AllUsersWebPart>
</WebParts>
</Form>
<Form
Type="EditForm"
Url="EditForm.aspx"
SetupPath="pages\form.aspx"
WebPartZoneID="Main" />
<Form
Type="EditForm"
Url="MyEditForm.aspx"
SetupPath="pages\form.aspx"
WebPartZoneID="Main"
Default="TRUE"
UseDefaultListFormWebPart="False"
WebPartOrder="1">
<WebParts>
<AllUsersWebPart WebPartZoneID="Main" WebPartOrder="1"><![CDATA[
<WebPart xmlns="https://schemas.microsoft.com/WebPart/v2">
<Assembly>Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>
<TypeName>Microsoft.SharePoint.WebPartPages.ListViewWebPart</TypeName>
<PageType>PAGE_VIEW</PageType>
<ViewFlags>Hidden ReadOnly Html</ViewFlags>
</WebPart>]]>
</AllUsersWebPart>
<AllUsersWebPart WebPartZoneID="Main" WebPartOrder="2"><![CDATA[
<WebPart xmlns="https://schemas.microsoft.com/WebPart/v2" xmlns:iwp="https://schemas.microsoft.com/WebPart/v2/Image">
<Assembly>Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>
<TypeName>Microsoft.SharePoint.WebPartPages.ImageWebPart</TypeName>
<FrameType>None</FrameType>
<Title>Site Image</Title>
<iwp:ImageLink>/_layouts/images/homepage.gif</iwp:ImageLink>
<iwp:AlternativeText>Logo</iwp:AlternativeText>
</WebPart>]]>
</AllUsersWebPart>
<AllUsersWebPart WebPartZoneID="Main" WebPartOrder="3"><![CDATA[
<WebPart xmlns="https://schemas.microsoft.com/WebPart/v2">
<Assembly>Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>
<TypeName>Microsoft.SharePoint.WebPartPages.ListFormWebPart</TypeName>
<PageType>PAGE_EDITFORM</PageType>
</WebPart>]]>
</AllUsersWebPart>
<View BaseViewID="1" WebPartZoneID="Main" WebPartOrder="4" />
</WebParts>
</Form>
<Form
Type="NewForm"
Url="NewForm.aspx"
SetupPath="pages\form.aspx"
WebPartZoneID="Main" />
</Forms>
若要按内容类型注册一个自定义表单模板,请将类似如下示例的一个定义作为 <ContentType> 标记的子元素添加到此功能的 Elements.xml 文件中。
<XmlDocuments>
<XmlDocument NamespaceURI="https://schemas.microsoft.com/sharepoint/v3/contenttype/forms">
<FormTemplates xmlns="https://schemas.microsoft.com/sharepoint/v3/contenttype/forms">
<Display>MyCustomForm</Display>
<Edit>MyCustomForm</Edit>
<New>MyCustomForm</New>
</FormTemplates>
</XmlDocument>
</XmlDocuments>
在上面的示例中,"MyCustomForm"是要添加到 \CONTROLTEMPLATES 目录中的自定义控件模板的 ID。
模板结构
基于 ASCX 的控件模板将多个控件集中到一个嵌套结构中,其中控件本身也包含模板,可用于充分扩展列表项表单。
%ProgramFiles%\Common Files\Microsoft Shared\web server extensions\14\TEMPLATE\CONTROLTEMPLATES 中定义的文档库表单模板提供了一个很好的控件模板结构示例。下面的示例演示为文档库表单定义的默认控件模板。
<SharePoint:RenderingTemplate ID="DocumentLibraryForm" runat="server">
<Template>
<SharePoint:InformationBar runat="server"/>
<wssuc:ToolBar CssClass="ms-formtoolbar" id="toolBarTbltop" RightButtonSeparator=" " runat="server">
<Template_RightButtons>
<SharePoint:SaveButton runat="server"/>
<SharePoint:GoBackButton runat="server"/>
</Template_RightButtons>
</wssuc:ToolBar>
<SharePoint:FormToolBar runat="server"/>
<SharePoint:FormComponent TemplateName="DocumentLibraryFormCore" runat="server"/>
</Template>
</SharePoint:RenderingTemplate>
在此示例中,文档库表单模板将几个组件结合起来以组成完整的表单模板布局。其中的许多组件都派生自 Microsoft.SharePoint.WebControls.FormComponent 基类,而该基类自身则派生自 Microsoft.SharePoint.WebControls.TemplateBasedControl,这表明组件自身可用于模板。
但是,大多数表单实际上通过 DefaultTemplates.ascx 文件中定义的单独模板呈现,该模板的 ID 为 DocumentLibraryFormCore。此模板还包含各种组件,这些组件中包括一些基于表的 HTML 结构,有助于组织页面布局,如下面的示例所示。
<SharePoint:RenderingTemplate ID="DocumentLibraryFormCore" runat="server">
<Template>
<TABLE class="ms-formtable" style="margin-top: 8px;" border=0 cellpadding=0 id="formTbl" cellspacing=0 width=100%>
<SharePoint:ChangeContentType runat="server"/>
<SharePoint:DocumentLibraryFields runat="server"/>
<SharePoint:ApprovalStatus runat="server"/>
</TABLE>
<SharePoint:WebPartPageMaintenanceMessage runat="server"/>
<SharePoint:DocumentTransformersInfo runat="server"/>
<table cellpadding=0 cellspacing=0 width=100%><tr><td class="ms-formline"><IMG SRC="/_layouts/images/blank.gif" width=1 height=1 alt=""></td></tr></table>
<TABLE cellpadding=0 cellspacing=0 width=100% style="padding-top: 7px"><tr><td width=100%>
<SharePoint:ItemHiddenVersion runat="server"/>
<SharePoint:InitContentType runat="server"/>
<wssuc:ToolBar CssClass="ms-formtoolbar" id="toolBarTbl" RightButtonSeparator=" " runat="server">
<Template_Buttons>
<SharePoint:CreatedModifiedInfo runat="server"/>
</Template_Buttons>
<Template_RightButtons>
<SharePoint:SaveButton runat="server"/>
<SharePoint:GoBackButton runat="server"/>
</Template_RightButtons>
</wssuc:ToolBar>
</td></tr></TABLE>
</Template>
</SharePoint:RenderingTemplate>
此示例的 DocumentLibraryFormCore 模板中的一个关键组件是 DocumentLibraryFields 组件,该组件引用从 Microsoft.SharePoint.WebControls.ListFieldIterator 类派生的 Microsoft.SharePoint.WebControls.DocumentLibraryFields 控件。列表字段迭代器包含纵栏式顺序的字段布局逻辑。但是,ListFieldIterator 类仅呈现模板中未被替代的控件。因此,如果为特定字段添加字段控件(例如,将"标题"字段控件置于右上角并将"作者"字段控件置于其下的控件),迭代器不会对该控件进行布局。您还可以选择指定自己的列表字段迭代器,该迭代器派生自 ListFieldIterator 类,并提供自定义字段迭代逻辑。
您可以替代 DocumentLibraryForm 模板并指定不同的 DocumentLibraryFormCore 模板。然后,可以从该模板进一步自定义表单模板:指定现有控件,或添加对在程序集或 .ascx 文件中定义的其他自定义控件的引用。
重要信息 |
---|
不支持对 SharePoint Foundation 在前端 Web 服务器上安装的 Default.ascx 文件进行编辑。在安装 SharePoint Foundation 的更新或 Service Pack 时,或者将安装升级到下一产品版本时,对最初随产品安装的文件所做的更改可能会被覆盖。 |