DCSIMG
Checking if a Publishing page is in edit mode - justguy's

Checking if a Publishing page is in edit mode

… and avoiding ASP.NET validator errors such as:

This page contains content or formatting that is not valid. You can find more information in the affected sections.

or

Input string was not in a correct format.

 

Hi,

After being asked by a customer to create a web part diaplying a form with validation on input fields, I have come acoross a problem.
It turns out that ASP.NET validators can prevent you from checking in or editing Publishing pages.

Searching for solution I have come across a few:

Check if the web part is in edit or design mode and only add the validator if not:

if (this.WebPartManager.DisplayMode != WebPartManager.EditDisplayMode &&
    this.WebPartManager.DisplayMode != WebPartManager.DesignDisplayMode)
{
    // in display mode
}
This solution applies only if you have a single web part on the page, because you can only check if the current web part is in display/edit/design mode.
Another solution is using the EditModePanel. It allows you to add contols to the page in display or edit mode.
For example:

// add the EditBox to the page in edit mode
EditModePanelpanel = newEditModePanel();
TextBoxtextBox = newTextBox();

panel.Controls.Add(textBox);
panel.PageDisplayMode = PageDisplayMode.Edit;
this.Controls.Add(panel);


This solution didn’t work. Some replection shows me that the panel uses this function (which is obfuscated): public staticSPControlModeGetContextualFormModeFromPostedForm()
This function comes from the internal class ConsoleUtilities.
The solution that worked for me was quite simple:

// check if the form is in display mode
bool inDisplayMode = SPContext.Current.FormContext.FormMode == SPControlMode.Display;

 

Well, good luck ;)

Published Wednesday, August 13, 2008 5:14 AM by justguy

Comments

# re: Checking if a Publishing page is in edit mode

I know this is probably a silly question, but how EXACTLY would I add this code to a sharepoint page?  Does it going in the master page, the content pages?  I cannot access a .cs file for the page in sharepoint (without a great  deal of work) so if it goes inline, is there anything else I need to add to the code, like <% %> or something.  Where do I put it in the page?

I referencing this line: bool inDisplayMode = SPContext.Current.FormContext.FormMode == SPControlMode.Display;

Wednesday, September 17, 2008 11:15 PM by self-made

# re: Checking if a Publishing page is in edit mode

Hi,

The best option for you to add the code to a page is using a web part.

You can build a custom web part that will display a form (several fields the user will be able to edit).

The validator goes into the web part too.

Monday, November 10, 2008 6:06 AM by justguy

# re: Checking if a Publishing page is in edit mode

dvdsfvds

Tuesday, December 16, 2008 2:15 AM by fdf

# re: Checking if a Publishing page is in edit mode

If you want to add the code to the master page, follow the solution from the link below. Works for me :)

ari.provoke.co.nz/.../asp-net-validation-controls-prevent-publishing-pages-from-saving.aspx

Monday, April 06, 2009 11:56 AM by blowtouch

# re: Checking if a Publishing page is in edit mode

What I want to know is:

Why the hell do we have to worry about this in the first place? SharePoint is so annoying. Every day I have to find solutions to stupid problems like this!

Friday, April 24, 2009 9:29 AM by Adrian

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above: