Control Property.

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
 
M

Milosz Skalecki [MCAD]

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.
 
R

Riki

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top