Passing data between asp.net pages
I'm wondering the opinion of what is the best way to pass a lot of values between MULTIPLE pages. I was thinking of eith开发者_如何学Goer saving the values to a database, using context.Items[], or Session[]. I'm not sure about what is the best method. I'm passing probably around 40 variables.This will be during a checkout process so they should be persisted through the users lifecycle. The user does not change the values after they are entered the first time. The values would be like Billing Address, First and Last name etc
Session is probably the most efficient. However, if the session times out or is lost for any other reason, any entries are lost. So if persistence is more important, you may want to store it in the db.
Maybe Cross-Page Posting option?
Another option could be Server.Transfer
method together with Context.Items
or Form
property.
In this case any posted form variables and query string parameters are available to the second/third/etc. page as well.
But the URL in browser won't be changed until you really transferred to another page.
One thing to be aware of is that if the first page wrote something to the Response buffer and you don’t clear it, then any output from the second page will be appended to the output of the first. This is often the cause of a weird behavior where it seems that a page is returning two different pages. Also, since the transfer happens on the server, you cannot transfer a request to an external site.
More information could be found here.
Passing the Values via Session variables doesn't work too well. You might also have chances where the user hits the back button or clicks refresh and the session variables have been garbage collected. This is the way for any cached items. One way around this is when the user goes to the second page, you take all the variables and put them into a hidden field somewhere separated by a Key:Value pair.
If the values can put put into the open, why not just put them into the URL?
If you can't do either, then putting them into a database and giving them a Guid ID per the username, that would prolly be the last alternative for me since you have to both read and write to the DB on each request.
The answer is entirely dependent on what the user expects the next time he returns to your site. Does he expect you to remember the contact information he already entered? If so, then you're going to have to use long term persistence in a database, cookie, etc. Is your user entering someone else's information for this visit only (i.e. a customer service app), and will enter different information the next time they visit? If so, then you may want to use the Session object.
精彩评论