Results 1 to 4 of 4

Thread: how to retain value in custom textbox after postaback

  1. #1

    Thread Starter
    Hyperactive Member bsw2112's Avatar
    Join Date
    Nov 2001
    Location
    ottawa, canada
    Posts
    292

    how to retain value in custom textbox after postaback

    I made the following control that inherits from WebControl

    Code:
    Imports Microsoft.VisualBasic
    Imports System
    Imports System.Web.UI
    Imports System.Web.UI.WebControls
    
    
    
    Public Class tb
        Inherits WebControl
    
        Protected _labelText As String
        Protected _value As String
        Protected _name As String
    
        Public Property LabelText() As String
            Get
                Return _labelText
            End Get
            Set(ByVal value As String)
                _labelText = value
            End Set
        End Property
    
        Public Property Value() As String
            Get
                Return _value
            End Get
            Set(ByVal value As String)
                _value = value
            End Set
        End Property
    
        Public Property Name() As String
            Get
                Return _name
            End Get
            Set(ByVal value As String)
                _name = value
            End Set
        End Property
    
    
        Protected Overrides Sub RenderContents(ByVal writer As HtmlTextWriter)
    
    
            Dim tmp As String = "<label for='" & Name & "'>" & LabelText & "</label><input id='" & Name & "' type='text' value='" & Value & "' />"
    
            writer.Write(tmp)
    
        End Sub
    
    
        Protected Overridable Function GetValue(ByVal Collection As Collections.Specialized.NameValueCollection) As Boolean
    
    
                Me.Value = HttpContext.Current.Request(Name)
    
    
            Return True
    
        End Function
    
    
        Protected Sub Page_Init(ByVal Sender As Object, ByVal E As EventArgs) Handles Me.Init
    
            GetValue(Page.Request.Form)
    
        End Sub
    
    
    
    End Class
    then in my default.aspx.vb file I have

    Code:
    Dim newTB As tb = New tb
    
            newTB.Name = "cat"
            newTB.LabelText = "My cat's name "
    
            placeholder.Controls.Add(newTB)
    I don't understand why the value of the textbox isn't retained after postback
    I tried to inherit directly from TextBox but then the render doesn't stick and the label doesn't appear.

    Does anyone know what I can do to make this work?

    bsw

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: how to retain value in custom textbox after postaback

    It isn't retained because you are explicitly writing it out to the browser. As a result, ASP.NET has no idea of its existence and so you'll have to maintain its value yourself.

    When you use writer.write() and write out some HTML as you're doing, think ASP/PHP: It won't maintain the values for you.

  3. #3

    Thread Starter
    Hyperactive Member bsw2112's Avatar
    Join Date
    Nov 2001
    Location
    ottawa, canada
    Posts
    292

    Re: how to retain value in custom textbox after postaback

    hi mendhak

    Thanks for the reply

    I knew that I was to track it myself but I forgot to add the name attribute. Now it works.

    Code:
    Public Class tb
        Inherits WebControl
    
        Protected _labelText As String
        Protected _value As String
        Protected _name As String
    
        Public Property LabelText() As String
            Get
                Return _labelText
            End Get
            Set(ByVal value As String)
                _labelText = value
            End Set
        End Property
    
        Public Property Value() As String
            Get
                Return _value
            End Get
            Set(ByVal value As String)
                _value = value
            End Set
        End Property
    
        Public Property Name() As String
            Get
                Return _name
            End Get
            Set(ByVal value As String)
                _name = value
            End Set
        End Property
    
    
        Protected Overrides Sub RenderContents(ByVal writer As HtmlTextWriter)
    
            Dim tmp As String = "<label for='cat'>" & _labelText & "</label><input name='" & _name & "' id='" & _name & "' type='text' value='" & _value & "' runat='server' />"
    
            writer.Write(tmp)
    
        End Sub
    
    
    
        Protected Sub Page_Init(ByVal Sender As Object, ByVal E As EventArgs) Handles Me.Init
            If Not GetValue(Page.Request.Form) Then
                GetValue(Page.Request.QueryString)
            End If
        End Sub
    
        Protected Overridable Function GetValue(ByVal Collection As Collections.Specialized.NameValueCollection) As Boolean
    
            For Each Key As String In Collection.AllKeys
                If Key = _name Then
                    Me.Value = Page.Server.HtmlEncode(Collection(Key)).Trim
                    Return True
                End If
            Next
            Return False
        End Function
    End Class

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: how to retain value in custom textbox after postaback

    Wahey.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width