Enabling design mode in ASP.NET WebParts

07Jul10

WebPart pages support lots of advanced functionality right out of the box, but there are some complexities in getting everything set up correctly. In a previous post, I pointed out an issue that I had with WebParts personalization support. Another thing that I got tripped up on recently was getting design mode working.

Design mode is a page viewing mode in which the WebParts on the page can be moved around via drag-and-drop using the mouse. This support is baked in, you just need to know how to enable it. Intuitively it seems like there should be an enum somewhere with the different display modes that you can use to change the mode. It doesn’t quite work this way, and the documentation is a little bit confusing on exactly how things are supposed to work.

The documentation gives an example in which the supported DisplayModes are queried from the WebPartManager and their names are added to a drop-down field on the page. Simply stated, all of the possible display modes are stored as static fields of the WebPartManager class rather than as an enum. The display modes supported at runtime for a given WebPartManager instance are found using the SupportedDisplayModes property.

foreach( WebPartDisplayMode mode in WebPartManager1.SupportedDisplayModes ) {
     // .....
}

Setting the display mode for the page is done like this:

WebPartManager1.DisplayMode = WebPartManager.DesignDisplayMode;

Note that if we try to set a display mode that is not supported by the WebPartManager, a runtime error will result. Also, drag-and-drop doesn’t work in all browsers. It didn’t work in Google Chrome for me, but it worked fine (go figure) in IE8.

Here is what it looks like in action:

Advertisements


No Responses Yet to “Enabling design mode in ASP.NET WebParts”

  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: