如何根据复合控件中的另一个属性分配属性值?

时间:2015-06-23 19:51:38

标签: asp.net vb.net composite-controls

环境:Asp.net 4.5,Webforms

我正在创建一个复合控件。我暴露了多个公共属性,但遇到了一个小问题。

假设我有两个属性:

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

我的控件正在为PreRender分配值。问题是,当我调用“Path”时,它获取UseAbsolute的默认/私有值。因此,即使我在control / html中将属性设置为True,它也会先获取false。

我可以通过这么多方式解决问题,但我觉得我错过了正确的方法或理解。

更新

我忘了提。我是:

EnsureChildControls()
<\ n>在PreRender中......

我也尝试将其添加到属性本身。

1 个答案:

答案 0 :(得分:0)

你的代码不应该是这样而不是你的代码吗?

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