Pergunta

A custom web part has custom web part properties. I would like to validate with custom server side code when user changes property value (or when properties are bing saved). Is that possible? Can I show an error message in the tool pane describing the problem that occurred?

Example: I have a web part where user needs to enter a list name (in a property). I would like to check if list with such a name exists.

Foi útil?

Solução

you could create a custom EditorPart which creates a custom interface in which you verify the input. Check this out: Web Part Properties - part 2 - Editor Parts

Outras dicas

I hope this can help you, althought I have not tested it. You can find an example here: http://www.sharepoint-tips.com/2010/06/validating-web-part-properties.html

private string _webPartContentLink = null;
 [Personalizable(PersonalizationScope.Shared)]
        [WebBrowsable(true)]
        [System.ComponentModel.Category("Services")]
        [WebDisplayName("Enter WebPart Content Link")]
        [WebDescription("Enter WebPart Content Link")]
        public string WebPartContentLink 
        {
            get
            {
                if (_webPartContentLink == null)
                {
                    _webPartContentLink = string.Empty;
                }
                return _webPartContentLink;
            }
            set 
            {
                if (!value.StartsWith("http://"))
                      throw new WebPartPageUserException("The WebPart Content Link is not valid");
                }

                _webPartContentLink = value;
             }
          }

In addition to what Wictor said, remember to define your own ValidationGroup when you are using custom or out of the box ASP.NET validators, that way you dont get interference from other validation controls that might be on the same page when you do a post back (maybe a subject for your book Wictor?:-)

You could also create a custom editor part with a textbox and ASP.NET validation control, e.g a RegularExpressionValidator. This gives you client-side validation on the property. The only disadvantage I can see with that solution is that the custom editor parts are always displayed in the top (which quite anoying...).

See this MSDN article for a walk-through: http://msdn.microsoft.com/en-us/library/hh228018.aspx#Y4419

Licenciado em: CC-BY-SA com atribuição
Não afiliado a sharepoint.stackexchange
scroll top