so I have a custom webpart on both the DispForm.aspx and EditForm.aspx which, on page load gets a list of all the fields in a SharePoint list and loops through each field and sets its display property to be true for both the DispForm.aspx and EditForm.aspx pages. The problem is when I try to edit a list item and save the changes I get an error. The error and code sample is show below:
Error: [SPException: The settings for this list have been recently changed. Refresh your browser before editing this list.]
Code:
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();
}
}
}
}