IsPostBack Migration

This sample demonstrates IsPostBack behavior in Blazor. In Web Forms, Page.IsPostBack was false on the initial GET and true on form POSTs. The BWFC shim adapts this for Blazor: false on first render, true on subsequent renders (interactive mode) or on POST requests (SSR mode).


1. IsPostBack Status

In Web Forms, IsPostBack indicates whether the page is being loaded for the first time or in response to a postback.

Before (Web Forms)

if (!IsPostBack)
{
    // Initial load — bind data
    GridView1.DataBind();
}

After (Blazor with BWFC)

// Same pattern works!
if (!IsPostBack)
{
    // Initial load — bind data
    LoadData();
}
Live Demo - IsPostBack Status

Current IsPostBack value: False

Render count: 0

In interactive mode, IsPostBack is false on first render and true after OnInitialized completes. Click the button to see the current value after re-rendering.


2. Guard Pattern

The classic if (!IsPostBack) guard ensures initialization code runs only once, on the first page load.

Live Demo - Guard Pattern

Data loaded on initial render: Alpha, Bravo, Charlie

Initialization ran: 1 time(s)

The data list is populated only when !IsPostBack. Clicking the button re-renders but does not re-initialize the data.


3. IsHttpContextAvailable Guard

Some Web Forms features (cookies, headers) require HttpContext, which is only available during SSR. Use IsHttpContextAvailable to guard those calls.

Before (Web Forms)

// HttpContext always available
string ip = Request.UserHostAddress;

After (Blazor with BWFC)

// Guard for availability
if (IsHttpContextAvailable)
{
    // Safe to use HTTP features
    var cookies = Request.Cookies;
}
Live Demo - HttpContext Guard

IsHttpContextAvailable: True

HttpContext is available — HTTP-level features (cookies, headers) work normally.