In your web part or user control, you may wish to know if the page on which your asset is placed is in edit or design mode, in order to alter the behaviour in some way.
How you do this depends on whether you’re using a publishing or non-publishing site.
var isPublishing = SPContext.Current.FormContext.FormMode != SPControlMode.Invalid; var wpDMode = WebPartManager.GetCurrentWebPartManager(Page).DisplayMode.Name; var isEditing = isPublishing ? SPContext.Current.FormContext.FormMode != SPControlMode.Display : (wpDMode.Equals("Edit") || wpDMode.Equals("Design"));
In a non-publishing environment (e.g. Team Sites) the code is a little different depending on whether you set the page in edit mode, or whether you’ve opened a web part property editor.
If the page is in edit mode because you clicked on Edit Page from the ribbon, the DisplayMode property will be set to “Design”.
If the page is in edit mode because you displayed a web part property editor, then the DisplayMode property will be set to “Edit”.