Remembering a visitor between pages
Pages do not remember each other by default. Session state gives one visitor a small store that lasts while they browse.
Store and read
C#
// on one page
Session["userName"] = txtName.Text;
// on a later page
string who = Session["userName"] as string;What to keep in mind
- Session belongs to one visitor, not to everyone.
- It ends when the session times out, so never treat it as permanent storage.
- Anything important belongs in a database, not in session.
Small values only. Session is for a name, an identifier or a setting. A large object in session slows the server and surprises the next developer.
With pages, input and memory in place, you can apply all of it in a worked shopping cart example.