2

I developed a custom webpart with a webpart property with some textboxes and it works partially.

If I deploy the project, the written text in the property is away. This is ok.

But my MAIN Problem is, that about after one day the text in the property disappear is away. I use SharePoint 2010.

Here is my code:

[WebBrowsable(true), Category("category"), Personalizable(PersonalizationScope.Shared), WebDisplayName("Hello"), WebDescription("Description1")]
public string hello
{
    get { return _hello; }
    set { _hello = value; }
}
public static string _hello;
3

Your code should look like below, its a base class that the webpart calls.... in the base class im setting the webpart settings, Iv made a custom one for you where you will see it in the properties of the webpart when you edit the webpart.

    private const string c_YourCustomVairable = "";

    public BaseWebPart()
    {
        _YourCustomVairable= c_YourCustomVairable;
    }

    internal string _YourCustomVairable;

    [SPWebCategoryName("This will be shown in webpart settings")]
    [DefaultValue(c_YourCustomVairable)]
    [Personalizable(PersonalizationScope.Shared)]
    [WebDisplayName("name to display")]
    [WebDescription("this is the description")]
    [WebBrowsable(true)]

    public string YourCustomVairable
    {
        get { return _YourCustomVairable; }
        set { _YourCustomVairable = value; }
    }

Base class explained:

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.webpartpages.webpart.aspx

how to make webparts:

http://msdn.microsoft.com/en-us/library/ee231579.aspx

Hope this helps :)

-1

I am not sure a day but your code doesn't look right too me, perhaps there is an update process running that causes the webpart to reinit itself.

Try changing:

[WebBrowsable(true), Category("category"), Personalizable(PersonalizationScope.Shared),     WebDisplayName("Hello"), WebDescription("Description1")]
public string hello
{
    get { return _hello; }
    set { _hello = value; }
}
public static string _hello;

too:

public static string _hello;
[WebBrowsable(true), Category("category"), Personalizable(PersonalizationScope.Shared), WebDisplayName("Hello"), WebDescription("Description1")]
public string hello
{
    get { return _hello; }
    set { _hello = value; }
}

See James solution below for a spot which I missed, cheers James!

  • 2
    I wouldn't set the private member to be static. It's not an instance member if set to static, and probably won't be persisted properly when serialized. – James Love Aug 21 '12 at 13:02
  • Good spot james +1 for you. – Hugh Wood Aug 21 '12 at 13:03
  • 1
    You're suggesting just moving the backing variable deceleration around a bit? Really? Maybe he just didn't have his lucky red socks on when compiling ;) – Ryan Aug 21 '12 at 13:06
  • See, I saw that and hoped that there was another bit of code sneakily put in somewhere that I didn't see. But I prefer the lucky red socks idea :) – James Love Aug 21 '12 at 13:36
  • Thx for replying. If I remove the static, I received an error. The thing is, that I have the property in the VisualWebPart1.cs File and want to use the values in the VisualWebPart1.ascx.cs File. THE Error: An object reference is required for the non-static field, method, or property 'VisualWebPart1.VisualWebPart1.VisualWebPart1._hello' – grekko Aug 22 '12 at 7:10

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.