Question

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;
}
} 
Était-ce utile?

La solution

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

Autres conseils

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à sharepoint.stackexchange
scroll top