I have a custom webpart with custom webpart property. I have created a complex validation in the property.

My question is, does this validation execute on every page load or happens only when administrator clicks on apply or OK button in the webpart edit panel?

Please see my sample code..

public string Text
get { return text; }
set {
// **My complex validation goes here**
   text = value;

It happens on every load.
The WebPartManager creates your web part using the default constructor and then applies all the stored values of properties. There really isn't any other way it can do it. The only information it has is the Property and the value, only your code knows how/where if the value is assigned to members internally.
As Paul writes if you only wan't the validation to happen when edited through the browser then you should write an EditorPart and put the validation in there. But is it ok that the uses upload a .webpart file without validation?
Another option is not to do the validation if the DisplayMode of the current WebPartManager is BrowseDisplayMode

  • Thanks Per. Could you explain where the data of custom webpart property is stored? is it in content DB or XML file? – Hojo Apr 2 '12 at 5:10
  • The properties are stored in the content DB – Per Jakobsen Apr 2 '12 at 6:38

On load.

But that may not be a bad thing. You didn't provide many details about your requirements, so hard to say for sure. If the property depends on something that may change external to the web part, then you probably should leave it there so that only valid values are restored when the page is loaded.

However, if the validation only applies when the value is set in the browser, it might make more sense to do the validation in the EditorPart.

  • Thanks for your quick answer. My issue is I have more than 10 webpart properties. These webpart properties are accepting sharePoint list urls, title field name, data field name etc.. Right now I am checking these lists are existing, columns are existing and some other calculations also in the webpart property set method. I sow that this webpart is loading very slow in normal page load. So my doubt is that whether the webpart property validation executes in every page load – Hojo Mar 30 '12 at 13:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.