473,769 Members | 7,320 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.EventArg s) Handles MyBase.Load
'nothing here
End Sub

Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) 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********@Kesh et-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 3255
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.netvis ion.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.EventArg s) Handles MyBase.Load
'nothing here
End Sub

Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) 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********@Kesh et-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
5110
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" Hyperlink1.NavigateUrl = "MyReport.csv" When the user clicks the HyperLink I would always like to
7
3706
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 satisfactory answer (check your browser) so now that I've had the time to set up a reasonable little test that I can post somewhere, I'll try again. The app I've written has three ASPX pages. One is a combined page which writes a little text...
12
1862
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 the page is being saved and when it is (hopefully) done saving. I thought about a modal window being opened using JavaScript when they click the save button telling them that the data is being saved. After saving is completed I could use...
12
8705
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 update after making changes, but I don't want that update button. How can I get the updated object when the user presses one of my other action buttons?
5
2664
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 during a phone call with a client. Since it's long, I put six Save buttons down the length of the form and asked the users to save often. When they click the Save buttons, the data is saved to a central database and a text label is changed to say...
1
3045
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 in any phase (any step) and to be able to continue later. My idea was to create several pages as steps (no, wizard control is not suitable for several reasons) and to allow users to save any step to the database, that is, a ViewState of the...
1
3215
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
1260
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 they click submit I want to save their postback data (i.e., which checkboxes are checked, which radio buttons are pushed and what's typed into the textboxes). Then later I want another user to be able to view one of these forms and elect to...
1
2503
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 save each row one by one. They want to edit multiple rows and save once. I try to put checkbox and textbox in ItemTemplate, and the UI looks OK on client site. I can select checkbox and type chars in textbox. But when I try to save them through...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10216
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10049
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9865
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5309
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.