Using Page.ClientScript.RegisterStartupScript doesn't appear to work after Server.Transfer

1.1k Views Asked by At

This should be a fairly simple question

At some point in my code I am doing a Server.Transfer to a different page

In the new page, there is a call (the details are irrelevant)

Page.ClientScript.RegisterStartupScript(GetType(), name, js, true); 

However, this javascript never appears on the page.

1

There are 1 best solutions below

0
On

Although this question is very old, I faced the same problem and as this question appeared on google top for me, I will share my solution.

For me the problem was in this line (vb.net)

page = DirectCast(HttpContext.Current.Handler, Page) 

it should be

page = DirectCast(HttpContext.Current.CurrentHandler, Page)

and then you use

page.ClientScript...whatever

The trick is that HttpContext.Current.Handler refers to the page before server transfer, and HttpContext.Current.CurrentHandler to the currently executing page: http://msdn.microsoft.com/en-us/library/system.web.httpcontext.currenthandler(v=vs.110).aspx