Here is the scenario: you have a web part which does something on the page; let’s say it redirects to another page based on some conditions. The problem here is that if you add this web part to the page – it will always redirect users and they will never have a chance to configure it. So the goal of this post is to show you how to determine the state of the page to alter the execution flow of your web part.
The method below will work for web part pages, so if insert this code into an ASCX control or ASPX page – you will get an error complaining that WebPartManager is null.
For ASPX pages use the following declaration:
<PublishingWebControls:EditModePanel runat=server id="EditModePanel1" PageDisplayMode="Display">
<!-- Markup and controls to be rendered when the page is in Display Mode-->
In the scenario of the web part, in your web part body, use the following statement:
if (WebPartManager.DisplayMode == WebPartDisplayMode.BrowseDisplayMode)
{
// Render controls in the normal mode in which end users view a page
}
else if (WebPartManager.DisplayMode == WebPartDisplayMode.EditDisplayMode)
{
// Render controls for content authors for when the page is in the edit mode
}
Now, your web part will provide a configuration experience for your content authors instead of throwing them in the loop of default rendering mode.
More on display templates in my SharePoint 2010 developer book
Enjoy!


