UserControl - Textbox tag property looses value on postback

Discussion in 'ASP .Net' started by Mike Dole, Nov 15, 2004.

  1. Mike Dole

    Mike Dole Guest

    I experimented with a usercontrol (TagedTextbox), I added a tag
    property.
    The tag value however is not persisted on postback (text value's
    fine)?

    I worked around it with:

    Me.ViewState("tag") = txtToelichting.Tag

    but what's wrong with my code?



    [Code Snippet:]

    Imports System.ComponentModel
    Imports System.Web.UI


    Namespace Greenbay

    <DefaultProperty("Text"), ValidationProperty("Text"),
    ToolboxData("<{0}:TagedTextBox runat=server></{0}:TagedTextBox>")>
    Public Class TagedTextBox
    Inherits System.Web.UI.WebControls.TextBox
    Implements INamingContainer

    Private _tag As String

    Public Property Tag() As String
    Get
    Return _tag
    End Get
    Set(ByVal Value As String)
    _tag = Value
    End Set
    End Property
    End Class
    End Namespace

    Thanks in advance,

    Mike
     
    Mike Dole, Nov 15, 2004
    #1
    1. Advertising

  2. Mike Dole

    Karl Seguin Guest

    Your subject says UserControl (ASCX files), but this is a server control
    question (no problem, just wanted to make sure the terminology was
    straight).

    There's nothing wrong with your code, if you want the value to be persisted,
    you need to store it in the viewstate as you did...there are no alternative,
    unless you reset the value on postback in your page...your proprety should
    probably look something like


    public property Tag() as string
    get
    if ViewState("tag") is nothing then
    'What to do? return default maybe?
    else
    Return cstr(ViewState("tag"))
    end if
    end get
    set (byval value as string)
    ViewState("tag") = value
    end set
    end property

    Karl

    --
    MY ASP.Net tutorials
    http://www.openmymind.net/


    "Mike Dole" <> wrote in message
    news:...
    > I experimented with a usercontrol (TagedTextbox), I added a tag
    > property.
    > The tag value however is not persisted on postback (text value's
    > fine)?
    >
    > I worked around it with:
    >
    > Me.ViewState("tag") = txtToelichting.Tag
    >
    > but what's wrong with my code?
    >
    >
    >
    > [Code Snippet:]
    >
    > Imports System.ComponentModel
    > Imports System.Web.UI
    >
    >
    > Namespace Greenbay
    >
    > <DefaultProperty("Text"), ValidationProperty("Text"),
    > ToolboxData("<{0}:TagedTextBox runat=server></{0}:TagedTextBox>")>
    > Public Class TagedTextBox
    > Inherits System.Web.UI.WebControls.TextBox
    > Implements INamingContainer
    >
    > Private _tag As String
    >
    > Public Property Tag() As String
    > Get
    > Return _tag
    > End Get
    > Set(ByVal Value As String)
    > _tag = Value
    > End Set
    > End Property
    > End Class
    > End Namespace
    >
    > Thanks in advance,
    >
    > Mike
     
    Karl Seguin, Nov 15, 2004
    #2
    1. Advertising

  3. Mike Dole

    Mike Dole Guest

    Thanks Karl,

    It did the trick, but it still confuses me (the client / server /
    session thing).
    I know that's what it's all about but I just don't get it..

    In vb.net I just say
    car.speed = 50
    start car
    'car.started = true
    'car.speed = 50

    In asp.net I have to start the car (By pressing the start button =
    postback my speed resets), I have to store the speed in a ViewState.

    If I catch the speed viewstate in In the page_load event it's empty
    because the 50's assigned in the btn_start click event and the
    Page_load event's triggered before speed gets it's value.

    If I want to do something with the ViewState I have to retrieve the
    values in the Pre_Render event..

    Private Sub Page_PreRender(ByVal sender As Object, ByVal e As
    System.EventArgs) Handles MyBase.PreRender
    Panel1.BackColor = ViewState("BackColor")
    End Sub


    It seems like ASP.net is much more complicated...

    But anyway thanks for your help it helped me a lot.

    Regards,

    Mike



    "Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net> wrote in message news:<>...
    > Your subject says UserControl (ASCX files), but this is a server control
    > question (no problem, just wanted to make sure the terminology was
    > straight).
    >
    > There's nothing wrong with your code, if you want the value to be persisted,
    > you need to store it in the viewstate as you did...there are no alternative,
    > unless you reset the value on postback in your page...your proprety should
    > probably look something like
    >
    >
    > public property Tag() as string
    > get
    > if ViewState("tag") is nothing then
    > 'What to do? return default maybe?
    > else
    > Return cstr(ViewState("tag"))
    > end if
    > end get
    > set (byval value as string)
    > ViewState("tag") = value
    > end set
    > end property
    >
    > Karl
    >
    > --
    > MY ASP.Net tutorials
    > http://www.openmymind.net/
     
    Mike Dole, Nov 16, 2004
    #3
  4. Mike Dole

    Karl Seguin Guest

    It is..but in winforms the client is always connected, there is a constant
    state "ON"...in ASP.Net it's "ON/OFF/ON/OFF/ON/OFF" with nothing connecting
    the pieces together....without a doubt it's more complicated, but it's not
    like they said "let's screw web developers!"...it's just how working with a
    stateless environment works...

    Karl

    --
    MY ASP.Net tutorials
    http://www.openmymind.net/


    "Mike Dole" <> wrote in message
    news:...
    > Thanks Karl,
    >
    > It did the trick, but it still confuses me (the client / server /
    > session thing).
    > I know that's what it's all about but I just don't get it..
    >
    > In vb.net I just say
    > car.speed = 50
    > start car
    > 'car.started = true
    > 'car.speed = 50
    >
    > In asp.net I have to start the car (By pressing the start button =
    > postback my speed resets), I have to store the speed in a ViewState.
    >
    > If I catch the speed viewstate in In the page_load event it's empty
    > because the 50's assigned in the btn_start click event and the
    > Page_load event's triggered before speed gets it's value.
    >
    > If I want to do something with the ViewState I have to retrieve the
    > values in the Pre_Render event..
    >
    > Private Sub Page_PreRender(ByVal sender As Object, ByVal e As
    > System.EventArgs) Handles MyBase.PreRender
    > Panel1.BackColor = ViewState("BackColor")
    > End Sub
    >
    >
    > It seems like ASP.net is much more complicated...
    >
    > But anyway thanks for your help it helped me a lot.
    >
    > Regards,
    >
    > Mike
    >
    >
    >
    > "Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>

    wrote in message news:<>...
    > > Your subject says UserControl (ASCX files), but this is a server control
    > > question (no problem, just wanted to make sure the terminology was
    > > straight).
    > >
    > > There's nothing wrong with your code, if you want the value to be

    persisted,
    > > you need to store it in the viewstate as you did...there are no

    alternative,
    > > unless you reset the value on postback in your page...your proprety

    should
    > > probably look something like
    > >
    > >
    > > public property Tag() as string
    > > get
    > > if ViewState("tag") is nothing then
    > > 'What to do? return default maybe?
    > > else
    > > Return cstr(ViewState("tag"))
    > > end if
    > > end get
    > > set (byval value as string)
    > > ViewState("tag") = value
    > > end set
    > > end property
    > >
    > > Karl
    > >
    > > --
    > > MY ASP.Net tutorials
    > > http://www.openmymind.net/
     
    Karl Seguin, Nov 17, 2004
    #4
    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. Replies:
    0
    Views:
    399
  2. Fabiano
    Replies:
    2
    Views:
    652
    =?Utf-8?B?c3Jpbmk=?=
    Jun 3, 2005
  3. shruds
    Replies:
    1
    Views:
    961
    John C. Bollinger
    Jan 27, 2006
  4. Mark van Bree

    Postback problem with usercontrol in usercontrol

    Mark van Bree, May 30, 2006, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    181
  5. MarsVoyager
    Replies:
    3
    Views:
    127
Loading...

Share This Page