SharePoint:自定义 Web 部件中的 FormField?
在我的主页上,我想要一个简单的 Web 部件,允许用户快速填充列表中的条目。
比方说,该列表包含三个字段:标题(文本)、正文(富文本)、类别(查找)。
我不想使用标准的 DataFormWebPart,因为我有一些隐藏代码,它们也填充了我的列表的一些技术隐藏字段(实际上,我不排除 DataFormWebPart,但我没有找到如何使用它后面有代码)。
所以我开始实现一个自定义的webpart。因为我不想手动处理每个字段输入,所以我开始使用 FormField
控件,它会自动选择呈现控件,并提供一个 Value
属性正确的格式:
<SharePoint:FormField runat="server" id="fldTitle" FieldName="Title" />
此代码是不够的,我必须指定 listid:
<SharePoint:FormField runat="server" id="fldTitle" FieldName="Title" ListId="{title list guid}" />
这工作得非常正确。我可以在代码中访问 fldTitle.Value 来检索用户输入。
但是我必须将 Web 部件包含在正确打包和部署的功能中,并且可以激活该功能。 Web 部件将始终针对相同的列表,但由于该列表也在功能(ListInstance 元素)中实例化,因此我无法提前知道 Guid。
我尝试过使用多种技术来动态设置列表 ID,但没有成功。
我还“反映”了 SP dll,以注意到 FormComponent 类正在使用由 ListFormWebPart 设置的“上下文”。
最后,我的问题是:
- 在主页上创建自定义输入 Web 部件(而不是列表自定义表单)是否是正确的方法?
- 如何保持 FormField 的行为(选择正确的控件并处理输入及其到存储格式的转换)?
- 我必须创建自定义 ListFormWebPart 吗?
- 我可以使用 ControlTemplates 吗?
预先感谢您的帮助...我已经在这个简单的案例中挣扎了好几天...
On my home page, I want a simple webpart that allow users to quickly fill an entry in a list.
The list have, let's say, three fields : title (text), body (rich text), category (lookup).
I don't want to use the standard DataFormWebPart because I have a bit of code-behind that also fill some technical hidden fields of my list (actually, I don't exclude the DataFormWebPart, but I didn't find how to use it with code behind).
So I started to implement a custom webpart. Because I don't want to have to handle manually each field input, I started to use the FormField
control, which automatically choose the rendering control, and provide a Value
property with the correct format :
<SharePoint:FormField runat="server" id="fldTitle" FieldName="Title" />
This code is not sufficient, I have to specified the listid :
<SharePoint:FormField runat="server" id="fldTitle" FieldName="Title" ListId="{title list guid}" />
This is working quite correctly. I can in code access the fldTitle.Value
to retrieve the user input.
BUT I have to include the webpart in a properly packaged and deployed feature, that can be activated. The webpart will always target the same list, but as the list is also instanciated in the feature (ListInstance element), I can't know the Guid in advance.
I've tried using several technics to set the list ID on the fly, but without success.
I've also "reflectored" the SP dlls to notice FormComponent class are using a "Context" that is set by ListFormWebPart.
Finally, my questions are :
- is it the correct way to create a custom input webpart on the home page (not a list custom form) ?
- how can I keep the behavior of the FormField (choose the right control and handle the input and its conversion to the storage format) ?
- Will I have to create a custom ListFormWebPart ?
- May I play with ControlTemplates ?
thanks in advance for the help... I'm struggling with this simple case for days now...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为自定义表单模板是自定义列表表单最简单的方法。由于自定义表单模板是作为用户控件实现的,因此您可以添加所需的任何代码。
请参阅以下文章:
http://www.codeproject.com/KB/sharepoint/SharePointListForms.aspx
I think that customizing form templates is the easiest way to customize list forms. Since custom form templates are implemented as user controls you can add whatever code you want.
See the following article:
http://www.codeproject.com/KB/sharepoint/SharePointListForms.aspx