Question

I have a requirement for building a budget report which has the same key fields for 6 different categories. I have written a visual web part which pulls a listitem for each section and displays the data along with an edit button. There is also a custom property in the webpart to store the type of budget:

 public class ProjectDetailsView : WebPart
    {
        // Visual Studio might automatically update this path when you change the Visual Web Part project item.
        private const string _ascxPath = @"~/_CONTROLTEMPLATES/SpecialProjectsProjectList/ProjectDetailsView/ProjectDetailsViewUserControl.ascx";

        [Personalizable(), WebBrowsable]
        public String BudgetType { get; set; }

        protected override void CreateChildControls()
        {
            ProjectDetailsViewUserControl control = Page.LoadControl(_ascxPath) as ProjectDetailsViewUserControl;
            //Control control = Page.LoadControl(_ascxPath);
            if (control != null)
            {
                control.WebPart = this;
            }

            Controls.Add(control);
        }
    }
}

I have added this visual wepart 6 times on the same page. It seems that only the first control is actually saving the custom property. Is this likely an error in the code, or can you not have the same webpart numerous times on the same page?

I don't want to spend hours debugging if someone already knows I need to create a separate control per section. Many thanks!

Was it helpful?

Solution 2

I have now discovered why I couldn't save the custom properties in the GUI. I was closing the Current Web Object from the SPContext. Apparently this should not be done as it's handled by the framework. Once I removed my CurrentSite.Dispose() from my finally in the webpart user control, I was able to properly save the custom properties.

OTHER TIPS

If you are going to put a web part on a page multiple times, each instance of the web part needs to have a unique id. Here's a sample of something you could do:

control.ID = this.ID + "_ProjectDetailsControl";

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top