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 ;)