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

Discussion in 'ASP .Net Web Controls' started by ErwinP, Aug 5, 2005.

  1. ErwinP

    ErwinP Guest

    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
    ErwinP, Aug 5, 2005
    #1
    1. Advertising

  2. ErwinP

    ErwinP Guest

    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
    ErwinP, Aug 19, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jstorta
    Replies:
    3
    Views:
    428
    jstorta
    Feb 20, 2006
  2. Chad
    Replies:
    0
    Views:
    209
  3. S.Volkov
    Replies:
    2
    Views:
    205
    S.Volkov
    Mar 12, 2006
  4. Trans
    Replies:
    8
    Views:
    307
    Robert Klemme
    Oct 23, 2008
  5. Chris Kennedy
    Replies:
    1
    Views:
    140
    Michael Winter
    Dec 13, 2003
Loading...

Share This Page