سؤال

so I have a custom web part 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();
       }
     }
   }
 }
هل كانت مفيدة؟

المحلول 2

Ok so from debugging the solution it seems that while the FormMode is in Edit mode you cannot call on the Update method for the list. The code works fine in display mode on the DispForm.aspx page and it will hide and show the correct fields in the EditForm.aspx page but when you click Save that is when the error is thrown. If the Update method is removed the error will not be thrown however you will not be able to see the changes to the fields. I actually found this out by trying another route which is actually hiding the rendering control on the page instead of the actual SP Field item. You can follow the steps on this link to see how to manipulate the actual controls that are rendered on the form: link text

نصائح أخرى

I think the problem is you're calling List.Update multiple times.

you might be better having an "IsListUpdated" flag

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();
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى sharepoint.stackexchange
scroll top