I have problem with custom properties. I want to set custom boolean value or write custom text. It do not work, if I don't set values to static. However, if I set value to static all webparts share the same values and I don't want that. Example, write text "Demo" in first webpart settings and I open another webpart in another subsite, it contains also text "Demo". But if I remove static keyword, my webpart settings are null. How I can fix this?

Here is my example code:

using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace SharePointProject1.WebPart1
    public partial class WebPart1 : WebPart
        // Uncomment the following SecurityPermission attribute only when doing Performance Profiling on a farm solution
        // using the Instrumentation method, and then remove the SecurityPermission attribute when the code is ready
        // for production. Because the SecurityPermission attribute bypasses the security check for callers of
        // your constructor, it's not recommended for production purposes.
        // [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Assert, UnmanagedCode = true)]

        #region Web-Part settings
        public  Boolean _isEnabled; // if this is static, I can read value but all webparts in SharePoint share this value
        [Category("Custom Properties"),
        WebDisplayName("Enable something"),
        public Boolean Ready
            get { return _isEnabled; }
            set { _isEnabled = value; }

        string _pageTitle;
        WebDisplayName("Page Title"),
        WebDescription("Title displayed on the page"),
        Category("Test Properties"),
        public string PageTitle
            get { return _pageTitle; }
            set { _pageTitle = value; }

        public WebPart1()
            // Problem read custom properties

            string test1 = _pageTitle; // null
            string test2 = PageTitle; // null
            bool test3 = _isEnabled; // false
            bool test4 = Ready; // false

        protected override void OnInit(EventArgs e)

        protected void Page_Load(object sender, EventArgs e)

2 Answers 2


You are trying to read the property values too early in the web part life cycle. The property values are populated from the content database after the constructor has run. If you check the property values in OnLoad or OnPreRender you will see the values you have set in the editor.


This is my code for vwp properties:

private string _lista = "";
WebDisplayName("[some name]"),
WebDescription("[some name]"),
Category("some category")]
public string _Lista
    get { return _lista.ToString(); }

    set { _lista = value; }

As you can see the only diference with your code is on the first line, you declare your propertie as public, I declare it as private.

The other that i can see is that you set some variables into de VisualWebPart1 method, did you try to move those lines to onload or Init methods?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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