Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Question from asp newbie 2

Status
Not open for further replies.

rdgerken

Technical User
Jul 8, 2002
108
Hypothetically,

Let's say I create a string array

On the first page, there is a form to enter first names of people. Then they hit a button to go to another page to enter the last names. Now, I want to display both the first and last names on a 3rd page. How is this possible without using a database? It seems that all the objects I create are only good for duration of that page, and I'm not sure (because I'm new to this), how to keep objects alive across different pages. Please help! Is there some place to declare objects that can be referenced from multiple pages - or is there a way that these objects can be stored in the page?

Thanks
 
Here are some ways to do this...i know there are others as well but I do not know the sytax off he top of my head.

Method 1: Session variables

in asp script write something like
Session("FirstName") = textboxID.text

Do the same thing on the second page except replace the first name with the last name.

On the third page, you can access both names by using the session object

eg. lblName.text = Session("FirstName") & " " & Session("LastName")


Method 2 - pass the name in the url.
A couple ways to do this. One way is append variables to the url. Eg, going from page 1 to page 2, use the url

server)/page2.aspx?FirstName=(insert the first name value from the text box on page1)

and going from page 2 to page 3

server)/page3.aspx?FirstName=(insert the first name value from the text box on page1)&LastName=(insert the last name value from the text box on page2)

Now you can access the values on page3.aspx in you asp script using

Request.Item("FirstName")
and
Request.Item("LastName")

eg. lblName.text = Request.Item("FirstName") & " " & Request.Item("LastName")

Another way using the request object which may be easier is to set the submit button to post and redirect to the next page. Then, if your textbox is named FirstName, or LastName, etc you can just access its last data using Request.Item("FirstName") and/or Request.Item("LastName")
 
also, note that session variables can hold any object (eg,, you can put a whole array in a single session variable)
 
rdgerken - as you'll find there are many ways to skin a dot NET cat so to speak, and in your case you can store objects in Session which can be retrieved from page to page, as one mechnism. The general rule of thumb when it comes to Session state is to avoid it if you can since it consumes quite a bit of server resources (so they say - the general argument is to avoid it if you can).

You can also store your string arrays within a server side Cache but here again there is the possibility that it could be lost by inadvertent garbage collection (same as Session) so it would be good if you had a way to reload the objects in case this happens - which would be an impossibility once they have moved from the first to the second to the third page.

The best way to transfer data from one page to another is via the QueryString; at least this is what I use whenever possible.

One way to prevent inadvertent loss of Session and Cache is to load these objects immediately into ViewState once you have made the redirect to the next page (in the On Load event). In this way the objects remain intact and the chance of losing them during the redirect is minimal.

For example, say you're moving from the first page to the second page you can initially load the values in either Session or Cache and then on Page Load of the next page place them in ViewState so they become part of the rendered page and then just before redirect say to the third page you can reload Session or Cache (If Nothing) with the ViewState value.

Before committing on this I would wait and get another answer or two -- most of the variables I deal with are not in fact objects but parameters easily passed in the QueryString. If this were my project and the number of Names were say less than 20 I'd probably pass them in the QueryString (privacy is another matter and there may be security concerns - I am only at a technical level with dot NET).

In the meantime you can search a few threads here at Tek-Tips regarding Cache and Session and see what you think.
 
joelwenzel - we posted around the same time; didn't see your statement - was just generalizing on this as many here seem to have peculiar approaches to passing object such as arrays.
 
Thanks for your prompt replies fellas... This will at least get me moving in the right direction. What I have actually determined with my problem is that I'm going to stay on the same page, so I think looking into the ViewState is where I need to be.

thanks again
 
joelwenzel said:
also, note that session variables can hold any object
I think that's probably the key to this problem. I'd create my own Person class that had an Add method containing FirstName and LastName. You could then call this method for each person that you need to add and store the actual class in a session variable. You can then use it wherever needed and destroy it once you've finished with it.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top