Wednesday, July 7, 2010

How to add properties to visual web part

MSDN seems does not mention the details about how to add properties to visual web part. So I think it may help with sample code.

I found the link Referencing web part properties in SharePoint 2010 visual web part (I could not find the author of this post, and could not comment it), which really surprised me. Static variables suppose to be shared by different instances, which means we would not be able to specify different web part settings based on the same web part template in that way. To confirm that, I did a quick test. Below are the test code:

Below is the test result. We can see that all the web part instances share the same setting, which is wrong in most of the cases.

3 comments:

Thanks for the tip! It works, except that the old value continues to be displayed in the web part until you refresh the page. I can't figure out why? Edit web part-->change custom field-->click ok-->see that old value is still displayed in the web part-->refresh page by clicking on breadcrumb-->now correct value appears.