TextBox and Property

Discussion in 'ASP .Net' started by shapper, Dec 6, 2007.

  1. shapper

    shapper Guest

    Hello,

    I am working on Library which will include various controls.

    One of these controls has a TextBox.

    I am using a property named Value to define the TextBox text:

    ' Value
    Private _Value As String
    Public Property Value() As String
    Get
    Return _Value
    End Get
    Set(ByVal value As String)
    _Value = value
    End Set
    End Property ' Value

    And the TextBox Init event is the following:

    Private Sub tbText_Init(ByVal sender As Object, ByVal e As
    EventArgs) Handles tbText.Init
    tbText.ID = "tbText"
    tbText.Text = _Value
    End Sub

    I added this control to a page and defined its value to "Hello".

    I also added a button to the page that when clicked basically does
    Response.Write(MyControl.Value)

    What happens is that when I change the TextBox text to "Goodbye" and
    click the button the text that Response.Write outputs is "Hello".

    Why isn't the property returning the new value?

    What is the right way to do this?

    Thanks,

    Miguel
     
    shapper, Dec 6, 2007
    #1
    1. Advertising

  2. Howdy,

    This is because you change the text of the text box in the Init event.
    Button raises its Click event between Load and PreRender page's events
    meaning after you take the value of the Value property. In addition, if the
    value is set through the code or data binding its value will be lost because
    it's not stored in the viewstate. You need to simplify your code to:

    Public Property Value() As String
    Get
    Return tbText.Text
    End Get
    Set(ByVal value As String)
    tbText.Text = value
    End Set
    End Property ' Value

    Done

    hope it helps
    --
    Milosz


    "shapper" wrote:

    > Hello,
    >
    > I am working on Library which will include various controls.
    >
    > One of these controls has a TextBox.
    >
    > I am using a property named Value to define the TextBox text:
    >
    > ' Value
    > Private _Value As String
    > Public Property Value() As String
    > Get
    > Return _Value
    > End Get
    > Set(ByVal value As String)
    > _Value = value
    > End Set
    > End Property ' Value
    >
    > And the TextBox Init event is the following:
    >
    > Private Sub tbText_Init(ByVal sender As Object, ByVal e As
    > EventArgs) Handles tbText.Init
    > tbText.ID = "tbText"
    > tbText.Text = _Value
    > End Sub
    >
    > I added this control to a page and defined its value to "Hello".
    >
    > I also added a button to the page that when clicked basically does
    > Response.Write(MyControl.Value)
    >
    > What happens is that when I change the TextBox text to "Goodbye" and
    > click the button the text that Response.Write outputs is "Hello".
    >
    > Why isn't the property returning the new value?
    >
    > What is the right way to do this?
    >
    > Thanks,
    >
    > Miguel
    >
     
    Milosz Skalecki [MCAD], Dec 6, 2007
    #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. S. Justin Gengo
    Replies:
    0
    Views:
    554
    S. Justin Gengo
    Jul 16, 2003
  2. tanmaypatankar
    Replies:
    0
    Views:
    1,093
    tanmaypatankar
    Dec 18, 2007
  3. Zarko Gajic

    Custom Attributes (AttributeTargets.Property) and Property Type

    Zarko Gajic, Apr 7, 2005, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    441
    Zarko Gajic
    Apr 7, 2005
  4. Replies:
    0
    Views:
    270
  5. Jurjen de Groot
    Replies:
    0
    Views:
    319
    Jurjen de Groot
    May 19, 2008
Loading...

Share This Page