Question

hi I'm trying to inject script to the page i'm redirecting to.

how can i do it?

few things:

i know i can put some flag in the session or cookie or query string and then check it in the other page. but i want to avoid doing this since i'm redirecting to many pages and don't want to duplicate the code - unless i'm gonna have to ...

The relevant code is:

 ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), Guid.NewGuid().ToString(), "doSomeThings();", true);
 Response.Redirect(newURL);
Was it helpful?

Solution

You can't control what will happen on the redirect target - the target only knows about the request it is getting, not about who it came from or what caused it.

You will have to use some sort of mechanism on each redirected page you want to put the script on that will detect that it is needed (whether through parameters on the URL or otherwise).

Instead of repeating code like you want to avoid, create a base class and inherit your pages from it. This injection behaviour can be put in the base class to be shared by all inheriting pages.

OTHER TIPS

Or you can do another thing, add the JS or required function to your Pages(the one you gonna redirect to) and pass some sort of flag from the current page.

And then in next page, read the flag and if its satisfies you condition then call the function.

As @Oded said, you can't control what gonna happen in next page.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top