1

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.

1 Answer 1

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!

2
  • 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 Commented Feb 23, 2012 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
    Commented Mar 9, 2012 at 9:07

Your Answer

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

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