Question : Add Property to asp.net user control to make reusable

I have downloaded a superb gallery called NotesForGallery.  I have created a user control calles Pictures.ascx which calls this gallery:

<ctl:Gallery runat="server" ID="Gallery1" FolderUrl="images/all' Size="100" />

I would like to reuse this control many times so have tried to add a property to Pictures.ascx called Directory which would let me pass different image directories.

So in pictures.ascx  have added:
 Dim _directory As String
    Public Property Directory() As String
        Get
            Return _directory
        End Get
        Set(ByVal value As String)
            _directory = value
        End Set
    End Property

Function GetDirectory() As String
        Return _directory
    End Function

And changed the call to NotesForGallery to:

<ctl:Gallery runat="server" ID="Gallery1" FolderUrl='<%#GetDirectory()%>' Size="100" />
And changed my aspx page to:
<uc4:Pictures ID="Pictures1" runat="server" Directory="~/documents/pictures/All/"/>

Problem is that when I run this I get the following error:

[NullReferenceException: Object reference not set to an instance of an object.]
   NotesFor.Gallery.get_FolderUrl() +31
   NotesFor.Gallery.get_Files() +58

[TargetInvocationException: Property accessor 'Files' on object 'NotesFor.Gallery' threw the following exception:'Object reference not set to an instance of an object.']
   System.ComponentModel.ReflectPropertyDescriptor.GetValue(Object component) +390
   NotesFor.ScriptDescriptorHelper.GetScriptDescriptors(Control control) +195


And when run in edbug it does not seem to call the GetDirectory function.  Have I made any glaring mistakes?

Answer : Add Property to asp.net user control to make reusable

Mmmm... I think that you don't need the GetDirectory function. Comment it and change this line of your code:

Instead of
<ctl:Gallery runat="server" ID="Gallery1" FolderUrl='<%#GetDirectory()%>' Size="100" />
Put this:
<ctl:Gallery runat="server" ID="Gallery1" FolderUrl='<%=Me.Directory%>' Size="100" />

Hope that helps.
Random Solutions  
 
programming4us programming4us