开发者

Sharepoint Web Part validation set off by Publishing Controls

I have a web part which uses validation (InputFormRequiredFieldValidator) on a textbox field to prevent submission of an empty field. When clicking on Check In to Share Draft or Publish while in Edit mode, this validation is done, and since I am not actually trying to submit the form, but rather check it in, I'd rather this didn't happen.

How can I achieve this?

See also: Sharepoint web part form validation blocks updating web part settings - this has the validation code, and how I solved开发者_开发问答 the problem of the EditorPart setting off the validation.

Update: I've tried detecting EditDisplayMode and disabling the validator as follows:

if (WebPartManager.DisplayMode.Equals(WebPartManager.EditDisplayMode))
{
     messageRequiredValidator.Enabled = false;
}

This doesn't work - I still get the validation failure message when Checking in the page. Maybe I am not detecting the DisplayMode correctly.


As Madhur pointed out I needed to check for BrowseDisplayMode. I started to go down the route of switching off the validator when in Edit mode and relying on the default of true, but there are other display modes such as Design, where I might also get the problem. So I checked for BrowseDisplayMode as follows:

WebPartManager mgr = WebPartManager.GetCurrentWebPartManager(Page);
if (mgr.DisplayMode.Equals(mgr.SupportedDisplayModes["Browse"]))
{
     messageRequiredValidator.Enabled = true;
}
else
{
     messageRequiredValidator.Enabled = false;
}

This seems to do the trick. Would appreciate any feedback anyone has on this method.


I went through a world of pain getting this to work so thought I'd share.

Even though I was correctly getting the display mode, my control continued to validate regardless of the mode.

The final solution involved three things:

  1. Disabling the RegularExpressionValidator by default - i.e. Enabled-"false"
  2. Moving my code into the CreateChildControls method.
  3. Adding an ELSE clause! Without it, validation was ALWAYS set to true. Don't ask me why.

And here's the code. Basic variation on previously supplied examples.

// Get the current display mode of the WPM
WebPartManager wp = WebPartManager.GetCurrentWebPartManager(Page);
String mode = wp.DisplayMode.Name;
// Enable validation in BrowseDisplayMode only
if (wp.DisplayMode == WebPartManager.BrowseDisplayMode)
{
    reqJournal.Enabled = true;
}
else
{
    reqJournal.Enabled = false;
    lblMsg.Text = "<strong>" + mode + " mode</strong>: Validation is disabled.";
}


Use the WebPartManager.DisplayMode enumeration to detect the current page mode and enable the validation only in BrowseDisplayMode

That's how the SharePoint OOTB webparts does validation.

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.webparts.webpartmanager.displaymode.aspx


//disable javascript on sharepoint edit mode
$(function () {
    var inDesignMode = document.forms[MSOWebPartPageFormName].MSOLayout_InDesignMode.value;

    if (inDesignMode == "1") {
        // page is in edit mode
        if ((typeof (Page_Validators) != "undefined") && (Page_Validators != null)) {
            var i;
            for (i = 0; i < Page_Validators.length; i++) {
                ValidatorEnable(Page_Validators[i], false);
            }
        }
    }
    else {
        // page is in browse mode
    } 
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜