Using Properties on a Web Part

Last post 11-02-2009 3:03 PM by djs25uk. 2 replies.

Sort Posts:

  • Using Properties on a Web Part

    10-27-2009, 6:45 AM
    • Member
      22 point Member
    • djs25uk
    • Member since 02-19-2008, 1:56 PM
    • Posts 60

    Dear All

    I'm just getting started with writing web parts for a SharePoint environment. I would like to be able to have a couple custom properties in my web part that can be customised, not per user (i.e. personalisation) but for the shared page.

    Here is the code that I'm working with. The first property that I've used appears in the web part properties pane. The second property doesn't work -- I think the only difference is I've tried to put it into a category and make it Storage.Shared. The web part compiles fine without errors or warnings and deploys to my SharePoint site. However, only the first property appears -- the second is no-where to be seen.

    Option Explicit On
    Option Strict On
    
    Imports System
    Imports System.Runtime.InteropServices
    Imports System.Web.UI
    Imports System.Web.UI.WebControls
    Imports System.Web.UI.WebControls.WebParts
    Imports System.Xml.Serialization
    
    Imports Microsoft.VisualBasic
    Imports Microsoft.SharePoint
    Imports Microsoft.SharePoint.WebControls
    Imports Microsoft.SharePoint.WebPartPages
    Imports System.ComponentModel
    
    Namespace ExamResults
    
        <Guid("a2dca7a1-e9b4-42a6-b888-86b143523ee3")> _
        Public Class WebPart1
            Inherits System.Web.UI.WebControls.WebParts.WebPart
    
            Private _start As String = [String].Empty
    
            ' Private variables 
            Private _myString As String
    
            Public Sub New()
                Me.Title = "Virtual Earth Driving Directions Web Part"
                Me.ExportMode = WebPartExportMode.All
    
                ' Initialize private variables. 
                _myString = "Hello"
            End Sub
    
            ' Property to personalize the start location 
            <Personalizable()> _
            <WebBrowsable()> _
            Public Property Start() As String
                Get
                    Return _start
                End Get
                Set(ByVal value As String)
                    _start = value
                End Set
            End Property
    
    
            ' This one doesn't work
            <Category("Custom Properties")> _
            <DefaultValue("Hello")> _
            <WebPartStorage(Storage.Shared)> _
            <FriendlyNameAttribute("Percentage")> _
            <Description("Enter a percentage.")> _
            <Browsable(True)> _
            <XmlElement(ElementName:="MyString")> _
            Public Property MyString() As String
                Get
                    Return _myString
                End Get
                Set(ByVal value As String)
                    _myString = value
                End Set
            End Property
    
            ' Render a Virtual Earth Map as a Web part 
            Protected Overloads Overrides Sub RenderContents(ByVal writer As HtmlTextWriter)
    
                If [String].IsNullOrEmpty(_start) Then
                    writer.Write("Select a start point by personalizing this WebPart.")
                Else
                    MyBase.RenderContents(writer)
    
                    writer.Write([String].Format(HtmlFormat, _start))
                End If
            End Sub
    
        End Class
    
    End Namespace
    

    Please could you help me to get started with this?

    Many thanks

    Daniel 

  • Re: Using Properties on a Web Part

    11-01-2009, 11:02 PM
    Answer

    Hi Daniel,

    It seems this is a SharePoint question and we have a forum to discuss this.  So I suggest you post this question to that forum, maybe you can get the solution soon.

    Here is link,

    http://social.msdn.microsoft.com/Forums/en-US/sharepointcustomization/threads

     

    Hope it helps.
    I'm always ready for your reply.

    Thank you for thinking of ASP.NET Forums,
    Jerry Weng - MSFT

    If the post helps you, please click the Mark As Answer or the Mark As Not Answer if not.
  • Re: Using Properties on a Web Part

    11-02-2009, 3:03 PM
    • Member
      22 point Member
    • djs25uk
    • Member since 02-19-2008, 1:56 PM
    • Posts 60

    Thank you but I did actually work it out in the end...I marked it as resolved but it didn't seem to stick. 

Page 1 of 1 (3 items)