Introduction

In ASP.NET, managing state across web pages and requests is a common challenge. Since HTTP is stateless, developers use tools like Session, ViewState, QueryString, and others to maintain user data across pages or actions.

In this article, we'll explore the differences, use cases, advantages, and limitations of each major state management technique in ASP.NET (non-Core), with examples.

1. Session

What is it?

How to use

Session["Username"] = "john.doe";
var name = Session["Username"].ToString();

Pros

Cons

Ideal for

2. QueryString

What is it?

How to use

// URL: example.com/page.aspx?userId=123
string userId = Request.QueryString["userId"];

Pros

Cons

Ideal for

3. ViewState

What is it?

How to use

ViewState["Counter"] = 5;
int counter = Convert.ToInt32(ViewState["Counter"]);

Pros

Cons

Ideal for

4. TempData (Mostly in ASP.NET MVC)

What is it?

How to use

TempData["Message"] = "User created!";
return RedirectToAction("Index");

// In next request:
string msg = TempData["Message"]?.ToString();

Pros

Cons

Ideal for

5. Hidden Fields

What is it?

How to use

<input type="hidden" id="HiddenField1" value="123" />

Pros

Cons

6. Cookies

What is it?

How to use

Response.Cookies["User"]["Name"] = "John";
string name = Request.Cookies["User"]["Name"];

Pros

Cons

Comparison Table

FeatureScopeStorageSecureExpiresSize LimitPage-to-Page
SessionPer-userServerYesLarge
ViewStateSingle pageClientNoMedium❌ (only postbacks)
QueryStringPer-requestClientNo~2KB
TempDataAcross 1 requestServerYesLarge✅ (1-time only)
Hidden FieldSingle pageClientNoSmall
CookiesBrowserClientYes4KB

Conclusion

Choosing the right state management method in ASP.NET depends on:

If you're storing sensitive or large data, use Session.
For simple filters or IDs between pages — use QueryString.
For maintaining form data during postbacks — use ViewState.