I have a session variable that I set to zero at every Page_Load beginning:

Session["DoRefresh"] = "0";

On a dropdown value changed, in the event handler I do:

Session["DoRefresh"] = "1";

On the end of aspx page I have a javascript added:

function OnRequestEnd(sender, args) {
    if ('<%= Session["DoRefresh"] != null ? Session["DoRefresh"].ToString() + "test" : "0" %>' == '1') { // call some refresh javascript functions } 

"test" is added here only for debugging to differentiate it from the zero after ":"

And I am not touching the session variable anywhere else but on the three places. I am using a session var since I plan to extend its usage in PageLoad, but the problem I am blocked with already is that I see from Chrome debugger the "if line" is resulting with

if ('0test' == '1')

It looks the session variable is still 0 in the moment its value is added to the aspx page to render. But how could it be, it is specified embedded code is executed in Render phase, which we know is afterward postback events handling. How could this be?

Dani AI

Generated

Two likely reasons explain why the page still sends "0" to the browser after the dropdown postback.

First, the server-side event might not have executed (this happens when controls are created dynamically but not recreated at the right lifecycle stage). Second, when using asynchronous postbacks (UpdatePanel), only the updated regions are sent back to the browser — a server-side literal that was rendered earlier will not change on the client unless its containing markup is part of the partial update. Note that Page_Load runs before control events and rendering happens after them, so if the handler did run and the script block was returned, the client would see the new value.

Quick checklist to narrow it down: verify that the dropdown event actually fired (breakpoint, Trace.Write, or a log entry); inspect the partial-postback response in the browser Network tab to see whether the script with the server-side value is present and updated; avoid reinitializing the refresh flag unconditionally at the top of Page_Load (initialize only on first load unless a reset is intended); if controls are created dynamically, recreate them in Page_Init with the same IDs so events can wire up.

A robust fix is to have the server push the client action on the same postback instead of relying on a previously rendered literal. For UpdatePanel partial postbacks use ScriptManager to register a startup script that runs after the response:

ScriptManager.RegisterStartupScript(this, GetType(), "refreshNow", "callClientRefresh();", true);

Alternatives: place a hidden field inside the UpdatePanel and let the client-side endRequest handler read it, or use ScriptManager.RegisterDataItem (ASP.NET 4.5+) to send a small payload to the browser.

For this usually comes down to either the handler not firing (see 's note about dynamic-control event issues) or the script block not being included in the partial update. Emitting the client call from the server (RegisterStartupScript) or updating an element inside the panel will solve the timing mismatch.

Member Avatar for Member #949455

And I am not touching the session variable anywhere else but on the three places. I am using a session var since I plan to extend its usage in PageLoad, but the problem I am blocked with already is that I see from Chrome debugger the "if line" is resulting with

You mean 'test' in here not '0test'

from this:

if ('0test' == '1')

to this:

if ('test' == '1')

Read this regarding about using Session with Postback:

http://stackoverflow.com/questions/6948533/why-dont-events-of-dynamically-added-user-control-fire-on-postback

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.