Through this blog, it is my effort to share experiences with the community and make the journey enjoyable.

Monday, September 29, 2008

Switching between WebPart Pages Modes

Frequently, need for switching between different modes in WebPart page programmatically is felt. For this we have used using various SharePoint JavaScript functions like MSOTlPn_ShowToolPaneWrapper, MSOLayout_ToggleLayoutMode etc. have been used in the past.New approach for the switching between WebPart modes would be to use WebPartManager DisplayModes collection and setting DisplayMode.Below attached sample webpart code, achieves the same. When you load the page in a browser, Display Mode drop-down list control is populated with display modes and is used to switch to different display modes. To edit controls, select Edit in the drop-down list control. When you are finished, select Browse in the Display Mode drop-down list control to return the page to a normal view. To add controls to the page, switch to catalog mode.

using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;

namespace SPUtil

public class EditModeSwitchWebPart : System.Web.UI.WebControls.WebParts.WebPart
DropDownList dropdownDisplayMode;
protected override void CreateChildControls()
dropdownDisplayMode = new DropDownList();
dropdownDisplayMode.AutoPostBack = true;
dropdownDisplayMode.SelectedIndexChanged += new EventHandler(dropdownDisplayMode_SelectedIndexChanged);

foreach (WebPartDisplayMode wpDisplayMode in WebPartManager.DisplayModes)
WebPartDisplayMode enabledWpDisplayMode = WebPartManager.SupportedDisplayModes[wpDisplayMode.Name];
if (enabledWpDisplayMode != null)

void dropdownDisplayMode_SelectedIndexChanged(object sender, EventArgs e)
if(this.dropdownDisplayMode.SelectedItem != null)
this.WebPartManager.DisplayMode = WebPartManager.DisplayModes[dropdownDisplayMode.SelectedValue];


Post a Comment

Search This Blog


The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway. All posts are provided "AS IS" with no warranties, and confers no rights. In addition, my thoughts and opinions often change, and as a weblog is intended to provide a semi-permanent point in time snapshot you should not consider out of date posts to reflect my current thoughts and opinions.

Popular Posts

Total Pageviews