Sharepoint web part form validation blocks updating web part settings

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-12 03:02:35

问题


I have written a web part in C# for Sharepoint 2007 which has a single field which is validated as a required field using RequiredFieldValidator(). The web part also has some configuration fields (ones you edit by clicking on Modify Shared Web Part).

When I make changes to these fields and try to apply them, the validation of the user field kicks in and prevents the update, even though I am not submitting the form. I am only trying to submit the settings. The web part may be used in a few places on our farm, so site collection administrators need to be able to change the settings - at the moment it is not friendly enough for these users to do so.

Here is where I validate the user field:

// Validate form field - required field, and max length is 100 characters.
InputFormRequiredFieldValidator messageRequiredValidator = new InputFormRequiredFieldValidator();
messageRequiredValidator.ControlToValidate = txtMessage.ID;
messageRequiredValidator.ErrorMessage = "You must write a message to send!";
messageRequiredValidator.Display = ValidatorDisplay.Dynamic;
messageRequiredValidator.Text = "<img src=\"/_layouts/images/CNSCA16.gif\"/> You must write a message to send.";
tc.Controls.Add(messageRequiredValidator);

Here is where I define one of the configuration fields:

private string recipientEmailAddress = "sender@domain.tld";
    [WebBrowsable(true),
    Personalizable(true),
    WebPartStorage(Storage.Shared),
    WebDescription("Email address the form should be sent to"),
    WebDisplayName("Recipient Email Address"),
    SPWebCategoryName("Email Settings")]
    public string RecipientEmailAddress
    {
        get { return recipientEmailAddress; }
        set { recipientEmailAddress = value; }
    }

This is the first web part I have written, so there may be some subtleties I am missing in how to do admin configuration and validation of user submitted fields.


回答1:


Ok - I've found the key to this. You can add a validationGroup property to each validator, and to the button that causes the validation. So I changed my code to include:

messageRequiredValidator.validationGroup = "UserInput";

and a similar property to my submit button. Now when I click on Ok in the ToolPane, it doesn't validate the UserInput validation group. That only happens when I click on my Submit button.




回答2:


You can dynamically disable validations on OK, Cancel buttons in ApplyChanges method:

ToolPane pane = Zone as ToolPane;
if (pane != null)
    pane.Cancel.CausesValidation = false;

OR you can also check if editor pane is open and disable validation in webpart :

WebPartManager wpm = WebPartManager.GetCurrentWebPartManager(Page);

if (wpm.DisplayMode == WebPartManager.EditDisplayMode)
 {
      //Page is in edit mode
 }



回答3:


I would suggest to use the SharePoint validation control.



来源:https://stackoverflow.com/questions/4333750/sharepoint-web-part-form-validation-blocks-updating-web-part-settings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!