473,385 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

keeping a variable value on postback

I'm dimming a string at the top of my page so I can use it in several
different subs on the page.

I'm setting the text in one sub and then reading it in several. I'd like to
also use this varable on postback. The catch is, since I'm dimming it at the
top of the page, it resets itself on postback.

The fix I cam up with is to, on page load, populate a hidden field with the
value I want, then read that back on postback. But is there a more
elegant/straightforward way to achieve this?

-Darrel
Nov 18 '05 #1
5 2005
The viewstate uses a hidden field but is mre "elegant" since its an actual
class you can program against....you can do it as a property:

public property MyValue() as string
get
If (ViewState("myValue") Is Nothing) Then
Return "initialValue"
End If
return cstr(ViewState("myValue"))
end get
set (value as string)
ViewState("myValue") = value
end set
end property

or more straightfowardly:

Sub Page_Load
dim myValue as string
if not page.IsPostBack
myValue = "initialValue"
else
myValue = cstr(ViewState("myValue"))
end if
end sub

Without seeing your code I can't tell you where, in this 2nd example, you
would set the viewstate via ViewState("myValue") = myValue but it should
be the same place you are creating the hidden form field.

Karl
--
MY ASP.Net tutorials
http://www.openmymind.net/
"Darrel" <no*****@nospam.com> wrote in message
news:u0*************@TK2MSFTNGP10.phx.gbl...
I'm dimming a string at the top of my page so I can use it in several
different subs on the page.

I'm setting the text in one sub and then reading it in several. I'd like to also use this varable on postback. The catch is, since I'm dimming it at the top of the page, it resets itself on postback.

The fix I cam up with is to, on page load, populate a hidden field with the value I want, then read that back on postback. But is there a more
elegant/straightforward way to achieve this?

-Darrel

Nov 18 '05 #2
Hi Darrel,

You can use the Cache object:

Cache("MyString") = "Test String"

Now, whenever you use Cache("MyString"), it will return the value. If this
is different for each user then use Session() instead of Cache(). One thing
to be aware of is that a Cache("whatever") variable can expire at any time.
So you should always check it for Nothing and re-create it if that happens.
The best way to do this is to encapsulate the Cache object in your own
class. Then use that classes properties and have those properties reference
the Cache object. In those properties test for Nothing and if it Is Nothing
then put the logic in there to re-create it. Good luck! Ken.

--
Ken Dopierala Jr.
For great ASP.Net web hosting try:
http://www.webhost4life.com/default.asp?refid=Spinlight
If you sign up under me and need help, email me.

"Darrel" <no*****@nospam.com> wrote in message
news:u0*************@TK2MSFTNGP10.phx.gbl...
I'm dimming a string at the top of my page so I can use it in several
different subs on the page.

I'm setting the text in one sub and then reading it in several. I'd like to also use this varable on postback. The catch is, since I'm dimming it at the top of the page, it resets itself on postback.

The fix I cam up with is to, on page load, populate a hidden field with the value I want, then read that back on postback. But is there a more
elegant/straightforward way to achieve this?

-Darrel

Nov 18 '05 #3
Problem with the cache is that it's shared accross multiple requests.
Multiple requests to the same page from multiple users will/could result in
problems.

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Ken Dopierala Jr." <kd*********@wi.rr.com> wrote in message
news:ue**************@TK2MSFTNGP10.phx.gbl...
Hi Darrel,

You can use the Cache object:

Cache("MyString") = "Test String"

Now, whenever you use Cache("MyString"), it will return the value. If this is different for each user then use Session() instead of Cache(). One thing to be aware of is that a Cache("whatever") variable can expire at any time. So you should always check it for Nothing and re-create it if that happens. The best way to do this is to encapsulate the Cache object in your own
class. Then use that classes properties and have those properties reference the Cache object. In those properties test for Nothing and if it Is Nothing then put the logic in there to re-create it. Good luck! Ken.

--
Ken Dopierala Jr.
For great ASP.Net web hosting try:
http://www.webhost4life.com/default.asp?refid=Spinlight
If you sign up under me and need help, email me.

"Darrel" <no*****@nospam.com> wrote in message
news:u0*************@TK2MSFTNGP10.phx.gbl...
I'm dimming a string at the top of my page so I can use it in several
different subs on the page.

I'm setting the text in one sub and then reading it in several. I'd like

to
also use this varable on postback. The catch is, since I'm dimming it at

the
top of the page, it resets itself on postback.

The fix I cam up with is to, on page load, populate a hidden field with

the
value I want, then read that back on postback. But is there a more
elegant/straightforward way to achieve this?

-Darrel


Nov 18 '05 #4
Hi Karl,

Good point. That is why I recommended using Session if this value would be
different for different users. I like your ViewState example though, don't
have to worry about it expiring or anything. Ken.

"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:uH****************@TK2MSFTNGP11.phx.gbl...
Problem with the cache is that it's shared accross multiple requests.
Multiple requests to the same page from multiple users will/could result in problems.

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Ken Dopierala Jr." <kd*********@wi.rr.com> wrote in message
news:ue**************@TK2MSFTNGP10.phx.gbl...
Hi Darrel,

You can use the Cache object:

Cache("MyString") = "Test String"

Now, whenever you use Cache("MyString"), it will return the value. If

this
is different for each user then use Session() instead of Cache(). One

thing
to be aware of is that a Cache("whatever") variable can expire at any

time.
So you should always check it for Nothing and re-create it if that

happens.
The best way to do this is to encapsulate the Cache object in your own
class. Then use that classes properties and have those properties

reference
the Cache object. In those properties test for Nothing and if it Is

Nothing
then put the logic in there to re-create it. Good luck! Ken.

--
Ken Dopierala Jr.
For great ASP.Net web hosting try:
http://www.webhost4life.com/default.asp?refid=Spinlight
If you sign up under me and need help, email me.

"Darrel" <no*****@nospam.com> wrote in message
news:u0*************@TK2MSFTNGP10.phx.gbl...
I'm dimming a string at the top of my page so I can use it in several
different subs on the page.

I'm setting the text in one sub and then reading it in several. I'd
like to
also use this varable on postback. The catch is, since I'm dimming it
at the
top of the page, it resets itself on postback.

The fix I cam up with is to, on page load, populate a hidden field
with the
value I want, then read that back on postback. But is there a more
elegant/straightforward way to achieve this?

-Darrel



Nov 18 '05 #5
Thanks to both of you. Good info!

-Darrel
Nov 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Joseph Luner | last post by:
I am having problem postback, (code shown below) the variable "my_str" is lost after clicking the "submit" button. Isn't it suppose to display "changed" after posting back? Is there anyway I...
1
by: Joseph Luner | last post by:
I am having problem postback, (code shown below) the variable "my_str" is lost after clicking the "submit" button. Isn't it suppose to display "changed" after posting back? Is there anyway I...
2
by: Rodusa | last post by:
I have a hidden field inside one datagrid which I can't get to make it keep its state after a postback event. Look field: <input type="hidden" id="TxtHiddenItem_id" name="TxtHiddenItem_id"...
12
by: Cindy Lee | last post by:
When I do a sorta on 1 table, then the other table goes back to the original order. What can I set so, it keeps the order of the other current gridview's order. I set all the gridview values...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.