Search code examples
javascriptasp.netextractcode-behind

How do you extract all the javascript code from a user control in code behind


On the PreRender event of my page, I decide to do a response.redirect(). That means none of the javascript enclosed in <script language="JavaScript" type="text/javascript"> tags on that page are executed.

I wish to do the below :

String allJavaScript = MagicFunctionThatReturnsAllJSInTheControl(someUserControl)

ClientScript.RegisterClientScriptBlock("".GetType(), "s", allJavaScript );

Response.Redirect("~/newpage.aspx",false);

Do you know how would one code up, in C#/vb.net the MagicFunctionThatReturnsAllJSInTheControl(someUserControl)

Thanks


Solution

  • In light of your last comment, I think I have a possible solution, even if it does not answer your question as originally stated.

    So, you want scripts in currentpage.aspx to execute on the client even though you're in the process of redirecting it to newpage.aspx. In this situation, you can avoid Response.Redirect() and perform the redirection on the client:

    string redirectScript = String.Format(CultureInfo.InvariantCulture,
        "window.location.href = '{0}';", ResolveUrl("~/newpage.aspx"));
    
    ClientScript.RegisterClientScriptBlock(GetType(), "redirectScript",
        redirectScript, true);
    

    This way, the client should load the page and execute the scripts within, then load newpage.aspx. If you can stand the original page to be briefly visible while the redirected page loads, this might solve your problem.