Sharepoint Web 部件表单验证阻止更新 Web 部件设置

发布于 2024-10-05 09:35:10 字数 1532 浏览 0 评论 0原文

我已经用 C# 为 Sharepoint 2007 编写了一个 Web 部件,其中有一个字段,该字段使用 RequiredFieldValidator() 验证为必填字段。 Web 部件还具有一些配置字段(通过单击“修改共享 Web 部件”进行编辑的字段)。

当我对这些字段进行更改并尝试应用它们时,即使我没有提交表单,用户字段的验证也会启动并阻止更新。我只是想提交设置。 Web 部件可能会在我们服务器场的几个地方使用,因此网站集管理员需要能够更改设置 - 目前对于这些用户来说这样做还不够友好。

这是我验证用户字段的地方:

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

这是我定义一个配置字段的地方:

private string recipientEmailAddress = "[email protected]";
    [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; }
    }

这是我编写的第一个 Web 部件,因此在如何进行管理配置和验证提交的用户方面可能缺少一些微妙之处字段。

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 = "[email protected]";
    [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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

巨坚强 2024-10-12 09:35:10

好的 - 我找到了关键。您可以将 validationGroup 属性添加到每个验证器以及用于验证的按钮导致验证。因此,我更改了代码以包含:

messageRequiredValidator.validationGroup = "UserInput";

以及与提交按钮类似的属性。现在,当我在工具窗格中单击“确定”时,它不会验证 UserInput 验证组。仅当我单击“提交”按钮时才会发生这种情况。

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.

你げ笑在眉眼 2024-10-12 09:35:10

您可以在 ApplyChanges 方法中动态禁用“确定”、“取消”按钮上的验证:

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

或者您也可以检查编辑器窗格是否打开并在 WebPart 中禁用验证:

WebPartManager wpm = WebPartManager.GetCurrentWebPartManager(Page);

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

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
 }
岁月蹉跎了容颜 2024-10-12 09:35:10

我建议使用 SharePoint 验证控件

I would suggest to use the SharePoint validation control.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文