E
ErwinP
Hi,
I ran into some strange behavior regarding the ASP.NET TextBox control
when
used in a custom composite control. I've created a simple custom
control (see
listing 1 at the end of this post) which has two textboxes and a
boolean ShowFirst
property that determines which of the two textboxes should be
displayed.
Question 1: when I flip between textboxes by changing the value of the
ShowFirst
property, the texbox that was last displayed loses it's Text value.
What's the reason
for this?
Question 2: when I replace the the standard ASP.NET textbox in my
custom composite
control with a custom textbox control, I can flip between textboxes
without loosing
the Text values. Which is rather strange because (as you can see in
listing 2) my
CustomTextBox class does not add to or modify the behavior of the
normal TextBox
class. Any explanation of this behavior would be much appreciated!
Kind regards,
Erwin Prejean
Listing 1: custom composite control
Imports System.ComponentModel
Imports System.Web.UI
Public Class CustomCompositeControl
Inherits System.Web.UI.WebControls.WebControl
Implements INamingContainer
Public Overrides ReadOnly Property Controls() As ControlCollection
Get
EnsureChildControls()
Return MyBase.Controls
End Get
End Property
Public Property ShowFirst() As Boolean
Get
Dim storedValue As Object = ViewState.Item("ShowFirst")
If IsNothing(storedValue) Then
Return True
Else
Return CType(storedValue, Boolean)
End If
End Get
Set(ByVal Value As Boolean)
ViewState.Item("ShowFirst") = Value
End Set
End Property
Protected Overrides Sub CreateChildControls()
txt1 = New System.Web.UI.WebControls.TextBox
txt1.ID = "txt1"
Controls.Add(txt1)
txt2 = New System.Web.UI.WebControls.TextBox
txt2.ID = "txt2"
Controls.Add(txt2)
End Sub
Protected Overrides Sub RenderContents(ByVal output As
System.Web.UI.HtmlTextWriter)
If ShowFirst Then
output.Write("First")
txt1.RenderControl(output)
Else
output.Write("Second")
txt2.RenderControl(output)
End If
End Sub
Private txt1, txt2 As System.Web.UI.WebControls.TextBox
End Class
Listing 2: custom textbox control
Public Class CustomTextBox
Inherits System.Web.UI.WebControls.TextBox
End Class
I ran into some strange behavior regarding the ASP.NET TextBox control
when
used in a custom composite control. I've created a simple custom
control (see
listing 1 at the end of this post) which has two textboxes and a
boolean ShowFirst
property that determines which of the two textboxes should be
displayed.
Question 1: when I flip between textboxes by changing the value of the
ShowFirst
property, the texbox that was last displayed loses it's Text value.
What's the reason
for this?
Question 2: when I replace the the standard ASP.NET textbox in my
custom composite
control with a custom textbox control, I can flip between textboxes
without loosing
the Text values. Which is rather strange because (as you can see in
listing 2) my
CustomTextBox class does not add to or modify the behavior of the
normal TextBox
class. Any explanation of this behavior would be much appreciated!
Kind regards,
Erwin Prejean
Listing 1: custom composite control
Imports System.ComponentModel
Imports System.Web.UI
Public Class CustomCompositeControl
Inherits System.Web.UI.WebControls.WebControl
Implements INamingContainer
Public Overrides ReadOnly Property Controls() As ControlCollection
Get
EnsureChildControls()
Return MyBase.Controls
End Get
End Property
Public Property ShowFirst() As Boolean
Get
Dim storedValue As Object = ViewState.Item("ShowFirst")
If IsNothing(storedValue) Then
Return True
Else
Return CType(storedValue, Boolean)
End If
End Get
Set(ByVal Value As Boolean)
ViewState.Item("ShowFirst") = Value
End Set
End Property
Protected Overrides Sub CreateChildControls()
txt1 = New System.Web.UI.WebControls.TextBox
txt1.ID = "txt1"
Controls.Add(txt1)
txt2 = New System.Web.UI.WebControls.TextBox
txt2.ID = "txt2"
Controls.Add(txt2)
End Sub
Protected Overrides Sub RenderContents(ByVal output As
System.Web.UI.HtmlTextWriter)
If ShowFirst Then
output.Write("First")
txt1.RenderControl(output)
Else
output.Write("Second")
txt2.RenderControl(output)
End If
End Sub
Private txt1, txt2 As System.Web.UI.WebControls.TextBox
End Class
Listing 2: custom textbox control
Public Class CustomTextBox
Inherits System.Web.UI.WebControls.TextBox
End Class