2

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!

  • For some reason the property was not saving when changing in the sharepoint GUI. I opened Sharpoint Designer and added the property by hand and it seems to be peristing. So to answer my question it appears that you can have one visual webpart multiple times on the same page. – JR_ATLDev Jun 1 '11 at 17:04
  • 1
    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. – JR_ATLDev Jun 1 '11 at 19:06
2

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";

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.

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.