One of these days I came accross a simple requirement that led me to a couple of scenarios: "How do I detect if I am in display mode or in edit mode?". This has some significant importance in WCM (Web Content Management) world, where you may need some tricks to hide fields, controls or even alter the look of a component. Thhis can be achieved (at least) in three ways:
-
in the Layouts (aspx) code only:
In this case you have specific zones where to put controls that appear in edit mode and in display mode (http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.publishing.webcontrols.editmodepanel.aspx):
For any part you need to hide from Edit mode surround it by
<PublishingWebControls:EditModePanel runat=server id="EditModePanel1" PageDisplayMode="Display">
<!-- your code here -->
</PublishingWebControls:EditModePanel>
For any part you need to hide from Display mode surround it by
<PublishingWebControls:EditModePanel runat=server id="EditModePanel1" PageDisplayMode="Edit">
<!-- your code here -->
</PublishingWebControls:EditModePanel>
-
code behind: webparts
In this case, the object responsible for the information of the current mode is the WebPartManager.DisplayMode (http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.webparts.webpartmanager.displaymode.aspx). The allowued values for this enumerate are (from msdn):
Display mode | Description |
---|---|
BrowseDisplayMode | Displays Web Parts controls and UI elements in the normal mode in which end users view a page. |
DesignDisplayMode | Displays zone UI elements and enables users to drag Web Parts controls to change the layout of a page. |
EditDisplayMode | Displays special editing UI elements and enables end users to edit the controls on a page. |
CatalogDisplayMode | Displays special catalog UI elements and enables end users to add and remove page controls. |
ConnectDisplayMode | Displays special connections UI elements and enables end users to connect Web Parts controls. |
Note that this only works correctly if there is only 1 webpart on the page as the test is done individually.
-
code behind: other situations
This is probably the least used mode, nevertheless, I had to used a couple of times. You can detect the current mode through:
Microsoft.SharePoint.SPContext.Current.FormContext.FormMode
This is an enumerate with the values:
- SPControlMode.Display
- SPControlMode.Edit
Thus you can build a very simple test:
if (Microsoft.SharePoint.SPContext.Current.FormContext.FormMode == SPControlMode.Display)
{
// your code to support display mode
}
else // Microsoft.SharePoint.SPContext.Current.FormContext.FormMode = SPControlMode.Edit
{
// your code to support edit mode
}
No comments:
Post a Comment