Sign up ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

im trying to set a DefaultValue for my custom webpart property..

namespace Beraterprofil.VisualWebPart1
    public class VisualWebPart1 : WebPart
        private string _sectionToRead = "CV";

        [WebBrowsable(true), Category("Miscellaneous"), Personalizable(PersonalizationScope.Shared),
            WebDisplayName("Welche Sektion soll gelesen werden?"), DefaultValue("CV")]
         public string sectionToRead
             get { return _sectionToRead; }
             set { _sectionToRead = value; }            

This is how i programmed it so far. What really confuses me is when i'm debugging this my sectionToRead string always displays "SPS-Section-ContactInfo" and i can't figure out from where he gets this string value..

Display of the Debug

share|improve this question

1 Answer 1

up vote 11 down vote accepted

AFAIK, DefaultValue attribute won't work for webpart properties.

You can use .webpart file for setting default values to your properties. You will need to add new <property> element under <properties> tag, like following:

<property name="sectionToRead" type="string">CV</property>
share|improve this answer
i had used the string "SPS-Section-ContactInfo" yesterday for testing purposes and apparently it has been stored inside sharepoint and since then every time it has produced an server error so i wasn't able to change the custom property to a value which won't produce an error.. – Peter Oct 14 '11 at 11:55
Webpart property is an attribute of the webpart instance, which you has added to a webpart page somewhere on the site. So, you can basically remove your webpart instance from the particular page (using ?contents=1 query string), and when you add it again, new webpart instance will be created, and default values will be applied. – Andrey Markeev Oct 14 '11 at 16:12
Right, I was going to mention that if you change the value... it will never use the default again. – Kit Menke Oct 14 '11 at 20:19
@AndreyMarkeev, is there a source describing why DefaultValue doesn't work with webpart properties? I ran into this exact problem today and of course ended coming up with a workaround for my solution. I appreciate your answer that pointed me in the right direction, but I am curious as to why the DefaultValue setting seems to be ignored on Webparts. – Peter Tirrell Aug 3 at 19:20

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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