SharePoint 2010 BaseFieldControl 在显示模式下复制列表中的第一项
我在使用 BaseFieldControl 时遇到了一个让我分心的问题。
本质上,我正在尝试使用 BaseFieldControl 来呈现字段,将列表转换为 HTML 表。
当我的表格呈现时,它会写出正确的行数,但每行中的数据始终与列表中的第一项相同。
当我将 ControlMode 属性从 SPControlMode.Display 更改为 SPControlMode.Edit 时,列表会正确呈现(除了处于编辑模式之外)
当我的代码在 ControlMode 设置为 SPControlMode.Display 的情况下运行时,我实际上可以在 BaseFieldControl.ItemFieldValue 中获取正确的值属性,但可怜的 BaseFieldControl 仍然坚持渲染列表中的第一项!
我还在 SharePoint 基础和 SharePoint 2010 服务器上安装了 Web 部件,并且得到了相同的结果!
最后我用谷歌搜索并找到了其他人的例子。不幸的是,当我尝试其他开发人员的代码(未修改)时,我得到了完全相同的结果!
这就是我正在做的事情。任何建议将不胜感激!
foreach(list.DefaultView.ViewFields 中的字符串 f) {
TableCell c = new TableCell();
var i = item[f];
if (i != null)
{
SPField spf = item.Fields.GetField(f);
BaseFieldControl bfc = spf.FieldRenderingControl;
bfc.ControlMode = SPControlMode.Display;
bfc.Value = bfc.ItemFieldValue;
bfc.ID = Guid.NewGuid().ToString();
bfc.FieldName = spf.Title;
bfc.ListId = list.ID;
bfc.ItemId = item.ID;
SPContext context = SPContext.GetContext(this.Context, item.ID, list.ID, SPContext.Current.Web);
bfc.ItemContext = context;
bfc.RenderContext = context;
bfc.EnableViewState = true;
bfc.Visible = true;
c.Controls.Add(bfc);
}
else
{
c.Text = "NULL";
}
r.Cells.Add(c);
}
I have come across a problem when using the BaseFieldControl that is driving me to distraction.
Essentinally I am trying to convert a list into a HTML table using the BaseFieldControl to render the fields.
When my table renders it writes out the correct number of lines BUT the data in each line is always the same as the first item in the list.
When I change the ControlMode property from SPControlMode.Display to SPControlMode.Edit the list renders correctly ( apart from being in Edit mode )
When my code running with ControlMode set to SPControlMode.Display I can actually get at the correct value in the BaseFieldControl.ItemFieldValue property but the wretched BaseFieldControl still insists on rendering the first item in the list!
I've also installed the web part on a SharePoint foundation and SharePoint 2010 server and I get the same results!
Finally I've googled around and found other peoples examples. Unfortunately when I try other dev's code ( unmodified ) I get exactly the same results!
This is what I'm doing. Any suggestions would be really appreciated!
foreach (string f in list.DefaultView.ViewFields)
{
TableCell c = new TableCell();
var i = item[f];
if (i != null)
{
SPField spf = item.Fields.GetField(f);
BaseFieldControl bfc = spf.FieldRenderingControl;
bfc.ControlMode = SPControlMode.Display;
bfc.Value = bfc.ItemFieldValue;
bfc.ID = Guid.NewGuid().ToString();
bfc.FieldName = spf.Title;
bfc.ListId = list.ID;
bfc.ItemId = item.ID;
SPContext context = SPContext.GetContext(this.Context, item.ID, list.ID, SPContext.Current.Web);
bfc.ItemContext = context;
bfc.RenderContext = context;
bfc.EnableViewState = true;
bfc.Visible = true;
c.Controls.Add(bfc);
}
else
{
c.Text = "NULL";
}
r.Cells.Add(c);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我终于修好了。原来是 SPWeb 对象的问题。我从 SPContext 中获取了它,并将其作为对我的方法的引用传递。
当我停止这样做,而是在方法中创建它(并为列表中的每个项目创建一次)时,一切都工作正常。
很奇怪。
I finally fixed it. Turns out to be a problem with the SPWeb object. I had grabbed it from SPContext and passed it through as a reference to my method.
When I stopped doing that but instead created it within the method ( and created it once per item in the list ) it all worked fine.
Very strange.