locked
SharePOint 2010 web part with Custom Properties RRS feed

  • Question

  • I would like to build a custom web part with custom properties and change the display content based on the custom property value.  I am able to do this but when I add the web part on two pages/sites I am not able to specify seperate values for the custom property.  How to restrict the scope of custom variable to specific web part only.
    Tuesday, March 20, 2012 7:58 PM

Answers

  • Please, remove "Shared" from here:

    Public Shared SharedAN As String

    Bond... Brooke Bond!

    Tuesday, March 20, 2012 10:57 PM
  • Try another way to get access to web part properties from user control:

    Add property of type of web part to user control and assign web part object to it when you are creating user control.

    MyWebpartUserControl.cs:

    public MyWebPart ParentWebpart { get; set; }

    MyWebpart.cs:

    var control = (MyWebpartUserControl) Page.LoadControl(_ascxPath);

    control.ParentWebpart = this;

    Controls.Add(control);



    Bond... Brooke Bond!


    • Edited by Aviw_ Wednesday, March 21, 2012 7:33 AM
    • Proposed as answer by Anders Runge [Denmark] Wednesday, March 21, 2012 9:38 AM
    • Marked as answer by Srikanth T Wednesday, March 21, 2012 11:36 PM
    Wednesday, March 21, 2012 7:32 AM

All replies

  • how do you define your custom properties?

    if you do like this

    [WebBrowsable(true),
    Category("Miscellaneous"),
    Personalizable(PersonalizationScope.Shared),
    WebDisplayName("Enter some text")]
    public string CustomTextProp { get; set; }

    you should have your "customtextprop" different for each instance of your webpart 

    hope this will help


    Best regards, Christopher. Mon Blog My Blog

    Tuesday, March 20, 2012 8:18 PM
    Answerer
  • Chris,

    Thanks for quick response.  I tried in similar way using VB.NET (given below) but when I change the proporty value of one instance it is changing the value of other instance also.

    <WebBrowsable(True), _
             Personalizable(PersonalizationScope.[Shared]), _
             Category("XXXX"), _
             DisplayName("App Name"), _
             WebDisplayName("App Name"), _
             Description("Specify the App Name")> _
        Public Property ServiceLine() As String
            Get
                Return SharedAN
            End Get
            Set(ByVal value As String)
                SharedAN = value
            End Set
        End Property

    Tuesday, March 20, 2012 10:01 PM
  • Please, give more code.

    Bond... Brooke Bond!

    Tuesday, March 20, 2012 10:39 PM
  • Hi,

    I included the code for the WP class below.  I am not sure where is the issue but I had to declare the variable as Public Shared to use it during Page_Load method.

    <ToolboxItemAttribute(False)> _
    Public Class MyWP
        Inherits WebPart

        Public Shared SharedAN As String
       

        ' Visual Studio might automatically update this path when you change the Visual Web Part project item.
        Private Const _ascxPath As String = "~/_CONTROLTEMPLATES/PortalWPs/MyWP/MyWPUserControl.ascx"

       
        <WebBrowsable(True), _
                 Personalizable(PersonalizationScope.[Shared]), _
                 Category("XXXX"), _
                 DisplayName("App Name"), _
                 WebDisplayName("App Name"), _
                 Description("Specify the App Name")> _
            Public Property AppName() As String
                Get
                    Return SharedAN
                End Get
                Set(ByVal value As String)
                    SharedAN = value
                End Set
        End Property
       
        Protected Overrides Sub CreateChildControls()
           

            MyBase.CreateChildControls()

           
            Dim control As Control = Page.LoadControl(_ascxPath)
            Controls.Add(control)

           

        End Sub

    End Class

    Tuesday, March 20, 2012 10:47 PM
  • Please, remove "Shared" from here:

    Public Shared SharedAN As String

    Bond... Brooke Bond!

    Tuesday, March 20, 2012 10:57 PM
  • Thanks fir the suggestion.  But if I removed Shared, I am not able to access the property values in Page_Load method.

    Tuesday, March 20, 2012 11:10 PM
  • Try another way to get access to web part properties from user control:

    Add property of type of web part to user control and assign web part object to it when you are creating user control.

    MyWebpartUserControl.cs:

    public MyWebPart ParentWebpart { get; set; }

    MyWebpart.cs:

    var control = (MyWebpartUserControl) Page.LoadControl(_ascxPath);

    control.ParentWebpart = this;

    Controls.Add(control);



    Bond... Brooke Bond!


    • Edited by Aviw_ Wednesday, March 21, 2012 7:33 AM
    • Proposed as answer by Anders Runge [Denmark] Wednesday, March 21, 2012 9:38 AM
    • Marked as answer by Srikanth T Wednesday, March 21, 2012 11:36 PM
    Wednesday, March 21, 2012 7:32 AM
  • It is working fine now.  Thanks to All for your help.


    • Edited by Srikanth T Wednesday, March 21, 2012 11:37 PM
    Wednesday, March 21, 2012 11:25 PM