2.自定义列表表单
1.在上一篇博客中的SharepointTest项目里添加一个名为TestWebParts的文件夹,如图1所示
图1
在TestWebParts中添加可视化WebPart,在页面里添加如下代码
<%@ Register TagPrefix="wssuc" TagName="ToolBar" Src="~/_controltemplates/ToolBar.ascx" %> <%@ Register TagPrefix="wssuc" TagName="ToolBarButton" Src="~/_controltemplates/ToolBarButton.ascx" %> <SharePoint:FormComponent ID="FormComponent1" ControlMode="New" TemplateName="AttachmentRows" runat="server" /> <table cellpadding="0" cellspacing="0" width="100%" style="padding-top: 7px"> <tr> <td width="100%"> <SharePoint:ItemHiddenVersion ID="ItemHiddenVersion1" ControlMode="New" runat="server" /> <SharePoint:ParentInformationField ID="ParentInformationField1" ControlMode="New" runat="server" /> <SharePoint:InitContentType ID="InitContentType1" runat="server" ControlMode="New" /> <wssuc:ToolBar CssClass="ms-formtoolbar" ID="toolBarTbl" RightButtonSeparator="&#160;" runat="server"> <Template_Buttons> <SharePoint:CreatedModifiedInfo ControlMode="New" ID="CreatedModifiedInfo1" runat="server" /> </Template_Buttons> </wssuc:ToolBar> </td> </tr> </table> <table style="width: 100%"> <tr> <td align="right"> <SharePoint:FieldLabel ID="FieldLabelTitle" ControlMode="New" runat="server" FieldName="Title" /> </td> <td> <SharePoint:FormField ID="FormFieldTitle" ControlMode="New" CssClass="ms-long" runat="server" FieldName="Title"> </SharePoint:FormField> </td> </tr> <tr> <td align="right"> <SharePoint:FieldLabel ID="FieldLabelTest" ControlMode="New" runat="server" FieldName="Test" /> </td> <td> <SharePoint:FormField ID="FormFieldTest" ControlMode="New" CssClass="ms-long" runat="server" FieldName="Test"> </SharePoint:FormField> </td> </tr> </table> <div style="text-align: center"> <asp:Button ID="ButtonSave" runat="server" Text="保À¡ê存ä?" OnClick="ButtonSave_Click" /> <asp:Button ID="ButtonCancel" runat="server" Text="取¨?消?" OnClick="ButtonCancel_Click" /> </div>
2.在后台添加如下代码
protected void ButtonSave_Click(object sender, EventArgs e) { SPListItem item = FormFieldTitle.ListItem; SaveButton.SaveItem(SPContext.Current, false, ""); RedirectToListDefaultView(); } protected void ButtonCancel_Click(object sender, EventArgs e) { RedirectToListDefaultView(); } /// <summary> /// 取¨?消?或¨°者?返¤¦Ì回? /// </summary> private void RedirectToListDefaultView() { if (Request["IsDlg"] == "1") { Context.Response.Write("<script type='text/javascript'>window.frameElement.commitPopup();</script>"); Context.Response.Flush(); Context.Response.End(); } else { SPUtility.Redirect(SPContext.Current.List.DefaultViewUrl, SPRedirectFlags.UseSource, this.Context); } }
3.部署以上Project
4.在上一篇博客的中的SharepointList.Test项目中添加一个Module,取名为ModuleTest,将<Module></Module>节点改为如下情形
<Module Name="ModuleTest" Url ="Lists/SharepointTest" SetupPath="pages" xmlns="http://schemas.microsoft.com/sharepoint/"> <File Path="form.aspx" Type="Ghostable" Url="TNewForm.aspx" > <AllUsersWebPart WebPartOrder="0" WebPartZoneID="Main" ID="TNewForm"> <![CDATA[ <webParts> <webPart xmlns="http://schemas.microsoft.com/WebPart/v3"> <metaData> <type name="SharePointTest.TestWebParts.TestAddForm.TestAddForm,SharePointTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=559467c88db54a8e" /> <importErrorMessage>Error</importErrorMessage> </metaData> <data> <properties> <property name="Title" type="string">TNewForm</property> <property name="Description" type="string">测a试º?用®?户¡ì新?建¡§页°3面?</property> </properties> </data> </webPart> </webParts> ]]> </AllUsersWebPart> </File> </Module>
上文中的Token,是webpart部署之后,在assembly中查看
5.打开SharepointTest.Lists的Schema.xml,在<ContentType></ContentType>节点下添加
<XmlDocuments> <XmlDocument NamespaceURI="http://schemas.microsoft.com/sharepoint/v3/contenttype/forms/url"> <FormUrls xmlns="http://schemas.microsoft.com/sharepoint/v3/contenttype/forms/url"> <New>Lists/SharepointTest/ TNewForm.aspx</New> </FormUrls> </XmlDocument> </XmlDocuments>
6.部署SharepointTest.Lists