Common Techniques for Passing Data Between ASP.NET Pages

Trasnferring data between pages is a fundamental requirement in ASP.NET web development. Several mechanisms exist for this purpose, including QueryString, Session, Cookies, Application state, and Server.Transfer. Each has distinct characteristics suited for different scenarios.

1. QueryString

The QueryString method appends key-value pairs to the URL, making it visible in the browser's address bar. It is suitable for transmitting small, non-sensitive strings or numeric values.

Pros: Simple to implement for basic data types.
Cons: Values are exposed in the URL (security risk) and cannot transmit complex objects.

Usage:

  • Construct a URL with parameters in the source page.
  • Redirect using Response.Redirect().
  • Retrieve values in the target page via Request.QueryString["key"].
// Source: PageA.aspx
protected void SendButton_Click(object sender, EventArgs e)
{
    string redirectUrl = $"PageB.aspx?username={HttpUtility.UrlEncode(LabelUser.Text)}";
    Response.Redirect(redirectUrl);
}
// Target: PageB.aspx
protected void Page_Load(object sender, EventArgs e)
{
    string username = Request.QueryString["username"];
    if (!string.IsNullOrEmpty(username))
        DisplayLabel.Text = HttpUtility.UrlDecode(username);
}

2. Session State

Session stores user-specific data on the server for the duration of a session. It supports both primitive types and serializable objects.

Pros: Handles complex data; easy to use.
Cons: Consumes server memory; data is lost if the session expires.

Usage:

  • Assign values using Session["key"] = value;
  • Retrieve in another page with Session["key"].
// PageA.aspx
protected void SaveToSession_Click(object sender, EventArgs e)
{
    Session["UserProfile"] = new { Name = TextBoxName.Text, Id = 101 };
}
// PageB.aspx
protected void Page_Load(object sender, EventArgs e)
{
    var profile = Session["UserProfile"];
    if (profile != null)
        InfoLabel.Text = profile.ToString();
}

Clear session data selectively with Session.Remove("key") or entirely with Session.Clear().

3. Cookies

Cookies store small text-based data on the client’s browser. They persist across sessions if configured with an expiration date.

Pros: Useful for maintaining lightweight user preferences.
Cons: Limited to strings; vulnerable to tampering; privacy concerns.

Usage:

  • Create and send a cookie via Response.Cookies.Add().
  • Read it latter using Request.Cookies["name"].Value.
// PageA.aspx
protected void SetCookie_Click(object sender, EventArgs e)
{
    var userPref = new HttpCookie("UserTheme", "DarkMode")
    {
        Expires = DateTime.Now.AddDays(7)
    };
    Response.Cookies.Add(userPref);
}
// PageB.aspx
protected void Page_Load(object sender, EventArgs e)
{
    string theme = Request.Cookies["UserTheme"]?.Value ?? "Default";
    ThemeLabel.Text = theme;
}

4. Application State

Application state provides global storage accessible to all users throughout the application lifetime. It is ideal for shared read-only data like counters or configuration settings.

Pros: Global scope; supports objects; low overhead per access.
Cons: Not user-specific; requires thread synchronization to avoid race conditions.

Usage:

  • Store data with Application["key"] = value;
  • Access it elsewhere after locking to ensure thread safety.
// PageA.aspx
protected void UpdateCounter_Click(object sender, EventArgs e)
{
    Application.Lock();
    Application["VisitCount"] = (int)(Application["VisitCount"] ?? 0) + 1;
    Application.UnLock();
}
// PageB.aspx
protected void Page_Load(object sender, EventArgs e)
{
    Application.Lock();
    int count = (int)(Application["VisitCount"] ?? 0);
    Application.UnLock();
    CounterLabel.Text = count.ToString();
}

5. Server.Transfer

Server.Transfer redirects execution to another page on the same server without informing the client. The URL in the browser remains unchanged, and the target page can access the source page’s properties and controls.

Pros: Efficient (no round-trip); preserves server context; supports complex data transfer.
Cons: URL doesn’t update, which may break relative links; limited to local pages.

Usage:

  • Expose public properties in the source page.
  • Call Server.Transfer("Target.aspx").
  • In the target, cast Context.Handler to the source page type to access its members.
// Source: InputPage.aspx
public string EnteredText => InputBox.Text;

protected void TransferButton_Click(object sender, EventArgs e)
{
    Server.Transfer("DisplayPage.aspx");
}
// Target: DisplayPage.aspx
protected void Page_Load(object sender, EventArgs e)
{
    var sourcePage = (InputPage)Context.Handler;
    OutputLabel.Text = sourcePage.EnteredText;
}

Note: Unlike Response.Redirect, Server.Transfer does not support cross-domain navigation and keeps the original URL intact.

Tags: ASP.NET QueryString Session Cookies Application State

Posted on Sun, 02 Aug 2026 16:09:29 +0000 by Ghulam Yaseen