By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,512 Members | 3,689 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,512 IT Pros & Developers. It's quick & easy.

UserControl - Textbox tag property looses value on postback

P: n/a
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
Nov 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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" <m_******@hotmail.com> wrote in message
news:fd**************************@posting.google.c om...
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

Nov 18 '05 #2

P: n/a
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:<e6**************@tk2msftngp13.phx.gbl>...
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/

Nov 18 '05 #3

P: n/a
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" <m_******@hotmail.com> wrote in message
news:fd**************************@posting.google.c om...
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:<e6**************@tk2msftngp13.phx.gbl>...
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/

Nov 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.