题
因此,我在compentform.aspx和editform.aspx上都有一个自定义的Web零件.aspx和editform.aspx页面。问题是当我尝试编辑列表项目并保存更改时,我会收到错误。错误和代码示例如下:
错误:
Spexception:此列表的设置最近已更改。在编辑此列表之前,请先刷新您的浏览器。
代码:
protected void Page_Load(object sender, EventArgs e)
{
using (SPSite site = new SPSite("http://labsp001"))
{
if (site != null)
{
using (SPWeb web = site.OpenWeb())
{
var mainList = web.Lists["Main List"];
web.AllowUnsafeUpdates = true;
web.Update();
var spFieldCol = mainList.Fields.Cast<SPField>().ToList();
if (spFieldCol != null)
{
foreach (SPField fieldItem in spFieldCol)
{
if (field != null)
{
field.ShowInDisplayForm = true;
field.ShowInEditForm = true;
field.Update();
field.ParentList.Update();
}
}
}
web.AllowUnsafeUpdates = false;
web.Update();
}
}
}
}
解决方案 2
好的,从调试解决方案来看,似乎在formmode处于编辑模式时,您无法调用列表的更新方法。该代码在compentform.aspx页面上的显示模式中正常工作,它将隐藏和显示正确的字段。如果删除了更新方法,则不会抛出错误,但是您将无法看到字段的更改。实际上,我通过尝试另一条路由,该路由实际上隐藏了页面上的渲染控件,而不是实际的SP字段项目。您可以按照此链接上的步骤来查看如何操纵表单上呈现的实际控件:链接文字
其他提示
我认为问题是您正在调用list.upate多次。
您可能会更好地拥有“ IslistupDed”标志
IE。
bool IsListUpdated = false;
foreach (SPField fieldItem in spFieldCol)
{
if (field != null)
{
field.ShowInDisplayForm = true;
field.ShowInEditForm = true;
field.Update();
isListUpdated=true;
}
}
if (IsListUpdated)
mainList.Update();