0

I have a master page which loads a user control to a placeholder

Code example

placeHolderNav.Controls.Clear()
Dim ucATG As UserControl = DirectCast(LoadControl("/main/navigation.ascx"), UserControl)
ucATG.ID() = "lookJS"
placeHolderNav.Controls.Add(ucATG)

Inside that navigation.ascx control there is a literal with ID litShowWork. The child page is getwork.aspx, which has a method called that insert into that literal from a child page

code sample

Dim litOmniture As Literal = DirectCast(Me.Master.FindControl("lookJS").FindControl("litShowWork"), Literal)

If Not litOmniture Is Nothing Then
    litShowWork.Text &= "hello"
End If
Nikita Silverstruk
  • 1,097
  • 1
  • 20
  • 42
Neo
  • 481
  • 2
  • 9
  • 24
  • Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Oct 23 '13 at 15:12
  • would be nice however i'm working with legacy code – Neo Oct 23 '13 at 15:13

1 Answers1

0

I would expose a public property in your masterpage e.g. ShowWorkText as String. This property sets (or reads) the Literal's Text. It searches the control in the Placeholder that is accessible directly. Then your code is more readable and more maintainable. It's also safer if you decide to replace the Literal with a TextBox for example. You have to cast the page's Master property to the actual type of your master to access that property.

Since the literal is in a UserControl you should use the same approach to expose the property there. Then the master accesses it instead of the page.

In the master (of type Site):

Public Property ShowWorkText As String
    Get
        Dim navigationControl As Navigation = Me.placeHolderNav.Controls.OfType(Of Navigation)().FirstOrDefault()
        If navigationControl IsNot Nothing Then
            Return navigationControl.ShowWorkText
        End If
        Return Nothing
    End Get
    Set(value As String)
        Dim navigationControl As Navigation = Me.placeHolderNav.Controls.OfType(Of Navigation)().FirstOrDefault()
        If navigationControl IsNot Nothing Then
            navigationControl.ShowWorkText = value
        End If
    End Set
End Property

in the UserControl (of type Navigation, LiteralShowWork is the litaral):

Public Class Navigation
    Inherits System.Web.UI.UserControl

    Public Property ShowWorkText As String
        Get
            Return LiteralShowWork.Text
        End Get
        Set(value As String)
            LiteralShowWork.Text = value
        End Set
    End Property

End Class

in the page that want to set the text (as mentioned Site is the type of the master):

Dim site As Site = TryCast(Me.Master, Site)
If site IsNot Nothing Then
    site.ShowWorkText = "hello"
End If
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • that would work but my hands are tied at this moment since every other page use the same thing. I have to work with what i got :(. – Neo Oct 23 '13 at 16:16