UserControl - Textbox tag property looses value on postback

M

Mike Dole

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
 
K

Karl Seguin

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
 
M

Mike Dole

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
 
K

Karl Seguin

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 said:
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>
 

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,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top