0

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
{
    [ToolboxItemAttribute(false)]
    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"),
        Personalizable(PersonalizationScope.Shared),
        WebBrowsable(true),
        WebDisplayName("Enable something"),
        WebDescription("Something")]
        public Boolean Ready
        {
            get { return _isEnabled; }
            set { _isEnabled = value; }
        }

        string _pageTitle;
        [WebBrowsable(true),
        WebDisplayName("Page Title"),
        WebDescription("Title displayed on the page"),
        Category("Test Properties"),
        Personalizable(PersonalizationScope.Shared)]
        public string PageTitle
        {
            get { return _pageTitle; }
            set { _pageTitle = value; }
        }
        #endregion

        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)
        {
            base.OnInit(e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
        }
    }
}
4

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.

1

This is my code for vwp properties:

private string _lista = "";
[WebBrowsable(true),
WebDisplayName("[some name]"),
WebDescription("[some name]"),
Personalizable(PersonalizationScope.Shared),
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, privacy policy and cookie policy

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