ASP.NET textbox loses text value in composite control but Textbox subclass doesn't!?

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
 
E

ErwinP

Hi,

Found the solution through another newsgroup. The ASP.NET TextBox Web
Control class has a boolean private property called
'SaveTextViewState'. This property determines if the Text value must be
stored in the view state. The Text value is not stored if all of the
following conditions apply:
1. no event-handler is connected to the TextChanged event
2. the Enabled property of the control is set to True
3. the Visible property of the control is set to True
4. MyBase.GetType is TextBox
5. TextBox is used in Password mode

In my case, all of these conditions were true which explains the loss
of the Text value as stated in my first question.
The fact that my 'own' TextBox remembers it's Text value (second
question) can be explained with the 4th condition: my TextBox class
returns a different type than the ASP.NET TextBox. This results in the
Text value being stored in the view state.

Regards,


Erwin
 

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

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top