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