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)
{
}
}
}