473,385 Members | 1,838 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.

How do I save an object with value, after postback?

I tried to simplify my problem.

What I want to do is to save a string in a variables, and everytime I click
a button, the variable string gets a bit longer by adding the time to it: on
the form there is a button. Somehow the submit seems to initialize the
variable again, rathen then keeping its previous state/value.
The variable has to be public to all methods in the class.

codebehind:

Dim TimeStr As String

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'nothing here
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
TimeStr &= "<br>" & Now().ToString
Response.Write(TimeStr)
End Sub

I guess I am overseeing something basic here. Can anyone help me out?

Thanks,

Leo
--

-----------------------------------------------
Leo Muller
Webmaster Keshet Interactive
Le********@Keshet-TV.com
03 - 7676383 / 067 - 972985
--------------------------------------
http://www.keshet-i.com
http://www.mooma.com
http://www.hakasefet.co.il
http://www.bip.co.il
http://www.keshet-tv.com


Nov 18 '05 #1
2 3193
Every time the page is loaded, the variable is re-created. The page itself
is inherently stateless, but you can save variables into the viewstate of
the page in an attempt to retain their state. Before you fetch anything from
the viewstate, just check to make sure it isn't null for some reason.

if(ViewState["TimeStr"] != null)
{
string temp = ViewState["TimeStr"].ToString();
ViewState["TimeStr"] = (temp + ("<br>" +
System.DateTime.Now.ToString()));
}
else
{
ViewState["TimeStr"] = ("<br>" + System.DateTime.Now.ToString());
}

You'll forgive me if the above code is in C#, but it's been too long since I
worked with VB.
Hope this helps,
Mark Fitzpatrick
Microsoft MVP - FrontPage
"Leo Muller" <le***@keshet-i.com> wrote in message
news:c8**********@news2.netvision.net.il...
I tried to simplify my problem.

What I want to do is to save a string in a variables, and everytime I click a button, the variable string gets a bit longer by adding the time to it: on the form there is a button. Somehow the submit seems to initialize the
variable again, rathen then keeping its previous state/value.
The variable has to be public to all methods in the class.

codebehind:

Dim TimeStr As String

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'nothing here
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
TimeStr &= "<br>" & Now().ToString
Response.Write(TimeStr)
End Sub

I guess I am overseeing something basic here. Can anyone help me out?

Thanks,

Leo
--

-----------------------------------------------
Leo Muller
Webmaster Keshet Interactive
Le********@Keshet-TV.com
03 - 7676383 / 067 - 972985
--------------------------------------
http://www.keshet-i.com
http://www.mooma.com
http://www.hakasefet.co.il
http://www.bip.co.il
http://www.keshet-tv.com

Nov 18 '05 #2
Hi Leo,

What You are forgetting is that your page object is created and destroyed
with request.
This means that your TimeStr variable is initialized on each postback.

You need to store the vairiable in either ViewState, Session or Application
in order to persist its value.
In this case i would use the viewstate and create persistable property like
this:

Public Property TimeStr()
Get
return ViewState.Item("TimeStr")
End Get

Set(ByVal Value)
ViewState.Item("TimeStr") = Value
End Set
End Property

/ricky
Nov 18 '05 #3

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

Similar topics

4
by: John | last post by:
Hi, I generate a report in a comma delimited file and give it a name like MyReport.csv . I then set a Hyperlink control to point tp the file HyperLink1.text = "Download"...
7
by: theyas | last post by:
How can I get my code to NOT display two "Open/Save/Cancel/More Info" dialog boxes when using the "Response.WriteFile" method to download a file to IE I've asked about this before and didn't get a...
12
by: Daniel Walzenbach | last post by:
Hi, I have a Website which allows users to input data. After they finished entering data they can click a button to save their input. Problem now is, that I have no possibility to visualize that...
12
by: Jim Hammond | last post by:
I am passing the whole object instead or parameters in my select and update methods. I can get the updated object if I set UpdateMethod, let ASP.NET autogenerate an update button, and then press...
5
by: C Watson | last post by:
Hi, I'm wondering if anyone can help me with AJAX in ASP.NET 1.1. I have a very specific feature that I would like to use it for. I have a rather long form that the users use to enter data...
1
by: Irene | last post by:
Hello all! I'm creating a web site in ASP.NET (VB.NET). One of the requirements was to allow users to create orders going through several steps. A must have is to have an option to save the work...
1
by: satish.vell | last post by:
Hi, I have a ASP.NET page inside which I toggle the display of a field using javascript. How can I save this display value between postbacks. Here is my code: <pre>
0
by: 1388-2/HB | last post by:
I have a page with a significant number of dynamically created radio buttons, checkboxes and textboxes. When a user is presented with this form and subsequently fills a bunch of stuff out, when...
1
by: =?Utf-8?B?U2hhdw==?= | last post by:
I have a small GridView about max 10 rows and few columns. The columns are simple just a checkbox and a textbox and a bound name. The users don’t want to click “edit” button to edit and...
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:
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.