4

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

namespace Beraterprofil.VisualWebPart1
{
    [ToolboxItemAttribute(false)]
    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

12

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>
  • 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
  • 2
    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 '15 at 19:20

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.