Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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!

share|improve this question
    
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 Answers 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";

share|improve this answer
up vote 2 down vote accepted

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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