… 并且避免 ASP.NET 验证控件错误:
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.
当创建一个WebPart来显示包含验证控件的表单时, ASP.NET 验证控件会阻止你签入和发布当前的Publishing页面.
解决方案:
- 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 ;)




