How can I assign a property value based on another property in a composite control?

159 Views Asked by At

Environment: Asp.net 4.5, Webforms

I'm creating a composite control. I've exposed multiple public properties, but running into a slight problem.

Let's say I have two properties:

Public Property Path() As String
   Get
       Return ViewState("Path")
   End Get
   Set(ByVal Value As String)
       If UseAbsolute = True Then
           ' do something
       Else
           ' it always lands heere...
       End If
   End If
   ViewState("Path") = Value
   End Set
End Property
Private _Path As String = String.Empty

Public Property UseAbsolute() As Boolean
    ....
End Property
Private _UseAbsolute As Boolean = False

My controls are being assigned values on PreRender. The problem is, when I call "Path" it's getting the default/private value for UseAbsolute. So even if I set the property to True in the control/html, it grabs the false first.

I can work around this many ways, but I feel I'm missing a proper method or understanding.

UPDATE

I forgot to mention. I am:

EnsureChildControls()

in the PreRender...

I also tried adding this to the properties themselves.

1

There are 1 best solutions below

5
On

Shouldn't Your code be like this instead of how you have it?

Private _Path As String = String.Empty
Private _UseAbsolute As Boolean = False
'
Public Property Path() As String
  Get
    Return _Path
  End Get
  Set(ByVal Value As String)
    If UseAbsolute = True Then
      ' do something
      _Path = some_absolute_path
    Else
      ' do something else
      _Path = some_relative_path
    End If
  End Set
End Property
'
Public Property UseAbsolute () As Boolean
  Get
    Return _UseAbsolute
  End Get
  Set(ByVal Value As Boolean)
    _UseAbsolute = Value
  End Set
End Property