How to tell if page is in edit mode on a non-publi

2019-04-06 16:32发布

问题:

For our publishing sites we use the SPContext.Current.FormContext.FormMode enum to work out if the current page is in edit mode. I've seen that this does not work for a team site I'm currently working on. The FormMode is always set to 'Invalid'.

However, when I click edit page on a sample page, the page does switch to edit mode so there must be some other way of knowing that a page is in edit mode. So how can I tell if I'm in edit mode for a page living in a team site?

Cheers. Jas.

回答1:

For my scenario, I've found that I can make use of the WebPartManager object to find out if the current page is in edit mode.

Dim wpm As WebPartManager = WebPartManager.GetCurrentWebPartManager(Page)

result = wpm.DisplayMode.Name.Equals("design", StringComparison.InvariantCultureIgnoreCase)

The above code informs me whether the current page is in edit mode, since the webpart zone is in design mode. When not in design mode, the DisplayMode will usually be 'Browse'.



回答2:

The SPContext.Current.FormContext.FormMode cannot be used in OnInit; it is always Invalid there. Try it later; I use it in OnPreRender, for example.

The WebPartManager.DisplayMode can be used to check whether an editor part is active in the editor zone. It is an additional thing - you can have the page in edit mode without that. It dependes on what you want to check in your scenario.

By the way, use the read only members for the comparison, like: wpm.DisplayMode == WebPartManager.EditDisplayMode.

--- Ferda Prantl