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 problem using multiple instances of a web part using custom properties.

I made a web part using SilverLight 4, VS2010 and SP2010. The Web Part passes some custom properties to SilverLight using list data chosen from an EditorPart. The web part works fine but when I try to add more than one web part and make those depend on other lists and data, only the latest web part will appear showing the graphics made from the settings of the first web part and the first web part just appears empty.

What I really want is for the site to display each web part added with each different setup. How is this possible?

I've tried to prefix every property getter and setter with

[WebBrowsable(false), WebPartStorageAttribute(Storage.Shared)]
[Personalizable(PersonalizationScope.Shared)]

and played around with the Storage.Shared/Storage.Personal/Storage.None for the WebPartStoarageAttribute argument. That didn't fix the problem.

Also I've made sure that every web part and EditorPart have its own id as done by Victor Wiléns in SharePoint 2010 Web Parts in action:

 EditorPartCollection IWebEditable.CreateEditorParts()
    {
        List<EditorPart> editors = new List<EditorPart>();
        myWebPartEditor editorPart = new myWebPartEditor();
        editorPart.ID = this.ID + "_editorPart";
        editors.Add(editorPart);
        return new EditorPartCollection(editors);
    }

    object IWebEditable.WebBrowsableObject
    {
        get { return this; }
    }

I suspect that the Web Part is only able to use one XAP file at a time?

I hope I've described the problem well enough and that someone is able to help :) Thanks in advance.

share|improve this question

1 Answer 1

I know it may be too late, but i have encountered the same problem and i think i have fixed it.

I had a webpart that contained a usercontrol that generates a form based on a listname contained in the site, listname choosen from the webpart properties, implemented dynamicaly with a editorpart . The problem was that if i inserted two webparts even in different subsites, and configured the properties of both with different input, only the last one would work.

At first i had the properties of the webpart defined like this (this is an example of one):

[WebBrowsable(false), Personalizable(PersonalizationScope.Shared)]
        public  string EmailReceiver
        {
           get { return _emailReceiver; }
            set { _emailReceiver= value; }
        }
  private static string _emailReceiver;

In the public class ContactFormWebPartEditorPart : EditorPart i had a textbox defined :

private TextBox Subject; The apply changes in the editorpart :

public override bool ApplyChanges()
        {
            EnsureChildControls();
            ContactFormWebPart webPart = WebPartToEdit as ContactFormWebPart;
            if (webPart != null)
            {
                webPart.ContactFormList = _dropDownLists.SelectedValue.ToString();
                webPart.EmailReceiver = Email.Text;
                webPart.EmailSubject = Subject.Text;
                webPart.RedirectTo = redirect.Text;
            }
            return true;
        }

All you have to do is to change the EmailReceiver property from the webpart to:

[WebBrowsable(false), Personalizable(PersonalizationScope.Shared)]
        public  string EmailReceiver
        {
            get;
            set;

        }

I have tested it with 3 different webparts, configured each with different properties. It works! Two of them were on the same page!

Maybe it works because the setter and the getter generate random field names on rutime.

Hope that helps!

share|improve this answer
    
Your problem was that you had kept the private string as static :) "private static string _emailReceiver;" This meant that all instances share the same string –  justforkix09 Feb 23 '12 at 5:56
    
Thank you for your answer but it didtn help. We have a none silverlight project where no problems are presented when adding multiple instances of that web part. Its pure html. I suspect the problems occur with the SilverLight aspect of my solution. –  Wolfsberg Mar 9 '12 at 9:07

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.