Control Property.

  • Thread starter Thread starter shapper
  • Start date Start date
S

shapper

Hello,

I am creating a Composite Control, named Form, which contains a
TextBox.

Form control has a property named Value which define the TextBox text
property.

When I define it as follows it does not work:

Public Property Value() As String
Get
Return ViewState("Value")
End Get
Set(ByVal value As String)
ViewState("Value") = value
End Set
End Property ' Value

Private Sub tbInput_Init(ByVal sender As Object, ByVal e As
EventArgs) Handles tbInput.Init
tbInput.Text = Me.Value
End Sub ' tbInput_Init

Then I changed this simply to:

Public Property Value() As String
Get
Return tbInput.Text
End Get
Set(ByVal value As String)
tbInput.Text = value
End Set
End Property ' Value

Aren't both approaches the same?

Thanks,
Miguel
 
Howdy,

No they aren't. The first sets the text in the init therefore changing the
text after this event would not be reflected. Secondly, the text would be
stored in viewstate twice (in your control and child textbox). In addition,
there's a small bug in the second approach as you have to make sure child
controls have been created before accessing any of their properties:

Public Property Value() As String
Get
EnsureChildControls()
Return tbInput.Text
End Get
Set(ByVal value As String)
EnsureChildControls()
tbInput.Text = value
End Set
End Property ' Value

I'd use the second definition.
 
Another difference, not mentioned by Peter and Milosz, is that
Return ViewState("Value")
sometimes can return Nothing, whereas the Text value of a TextBox can not.

Jos
 
Back
Top