SharePoint 2010 BaseFieldControl 在显示模式下复制列表中的第一项

发布于 2024-12-16 22:12:10 字数 1617 浏览 0 评论 0原文

我在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

李不 2024-12-23 22:12:10

我终于修好了。原来是 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文