0

I created a custom visual user control webpart property for my SharePoint webpart. The property has a textbox which is getting empty after an IIS reset. Can anyone tell me how to solve this issue please. Sometimes it resets itself without IIS reset as well not 100% sure though.

EDIT

My webpart class

public class MyVisualBoard : System.Web.UI.WebControls.WebParts.WebPart
{
    public const string webPartName = "MyVisualBoard";
    private string _ascxPath;

    public MyVisualBoard()
    {
        _ascxPath = @"~/_ControlTemplates/MyVisualBoard/MyVisualBoardUserControl.ascx";
    }

    protected override void CreateChildControls()
    {
        base.CreateChildControls(); //Always child controls under base childcontrols

        try
        {
            Control userControl = Page.LoadControl(_ascxPath);
            Controls.Add(userControl);
        }
        catch (Exception ex)
        {
            LogErrorMessage(ex, webPartName, Context.Request.Url.AbsoluteUri);
        }
    }

    protected override void OnPreRender(EventArgs e)
    {
        try
        {
            base.OnPreRender(e);
        }
        catch (Exception ex)
        {
            LogErrorMessage(ex, webPartName, Context.Request.Url.AbsoluteUri);
            ShowErrorMessage(String.Empty);
            return;
        }
    }

    protected override void OnInit(EventArgs e)
    {
        // Script manager need to be added for the update panel to work
        if (ScriptManager.GetCurrent(Page) == null)
        {
            ScriptManager sMgr = new ScriptManager();
            Page.Form.Controls.AddAt(0, sMgr);
        }
        base.OnInit(e);
    }

    public static decimal MyCustomProperty;
    [SPWebCategoryName("My Custom Property")]
    [Personalizable(PersonalizationScope.Shared)]
    [WebDisplayName("My Custom Property")]
    [WebBrowsable(true)]
    public decimal _MyCustomProperty
    {
        get { return MyCustomProperty; }
        set { MyCustomProperty = value; }
    }

//more custom properties are just like it (like 7 of them)

User control class looks like this,

using MyVisualBoard.WebParts.MyVisualBoard;

namespace MyVisualBoard.Webparts.MyVisualBoard
{
    public partial class MyVisualBoardUserControl : UserControl
    {
        private const string webPartName = "MyVisualBoard";

        protected void Page_Load(object sender, EventArgs e)
        {
            this.MyCustomProperty.Text = MyVisualBoard.MyCustomProperty;
        }

        //Some  other methods here as well
2
  • how about the code that you use to Persist the properties?. Can you paste the code here? Nov 16, 2012 at 15:49
  • @FalakMahmood I added some code, and whats your skype bro, never able to get you on twitter you know :) Nov 16, 2012 at 16:17

3 Answers 3

1

TimeToThine,

A similar question was asked,

@Ryan has answered the question, credits to him!
Sharepoint Webpart custom properties get default values on server reboot

Similar question on StackExchange:
Sharepoint Webpart custom properties get default values on server reboot

So instead of using static variable, use non-static!

And you can access the property as described in below article [It is for SP2010 but it should work]:
Custom Properties in SharePoint 2010 Visual Web Parts

Hope this helps

0

Normally if you programmed your webpart correctly the properties for the webpart are stored in the content database, so it shouldn't be an issue resetting the IIS or whatever.

Basically there is a lot of information on the internet. For example MSDN

If you want more info just search for custom webpart properties or persisting webpart properties.

Happy coding!

1
  • I think you didn't read my question properly, I am talking about User Control (Visual Webparts in 2007) not Microsoft.SharePoint.WebPartPages.WebPart ones , but thanks anyway :) Nov 16, 2012 at 15:48
0

This should point you into the right direction :)

http://www.codeproject.com/Articles/29285/Simple-WebPart-To-Load-UserControl

hope it help :)

Your Answer

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

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