locked
The recommended way to add client side script on an .aspx page RRS feed

  • Question

  • User54516845 posted

    I've seen a variety of articles giving different solutions as to how to embed client side javascript in the final html sent back to the browser.   I suppose that is for historic reasons given that the way to do it has evolved from Asp.Net 2.0 through to the Asp.Net 4.5.   I was wondering if anyone can describe how this has evolved through the versions and what is the current recommended way to do it ?  I've inherited some very old asp.net code and a bit confused

    Saturday, November 24, 2018 6:34 PM

Answers

  • User475983607 posted

    It's less about recommended methods and more about picking a strategy the fits your needs.  Just because a server control has been around for a long time does not mean it is not useful.  Secondly, JavaScript runs in the browser so any method that returns the JavaScript so that it produces the expected result is fine.

    I've inherited some very old asp.net code and a bit confused

    It is a lot easier to provide assistance if you provide the code.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, November 24, 2018 7:28 PM

All replies

  • User475983607 posted

    It's less about recommended methods and more about picking a strategy the fits your needs.  Just because a server control has been around for a long time does not mean it is not useful.  Secondly, JavaScript runs in the browser so any method that returns the JavaScript so that it produces the expected result is fine.

    I've inherited some very old asp.net code and a bit confused

    It is a lot easier to provide assistance if you provide the code.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, November 24, 2018 7:28 PM
  • User54516845 posted

    Thanks for your post,

    So, I am basing myself on this post

    If I understand correctly, the most basic and intuitive way to add client script (when no master page is referenced from .aspx) is to add the script tag to the .aspx page itself like shown below

      <script language="javascript">
           // JS code here....
        </script>

    If however in my project I make use of master pages (always a better idea), then I need to add a script place holder in the master page like this and then have each .aspx page inject its own script into that content place holder like this....

    <asp:ContentPlaceHolder ID="scriptContentHolder" runat="server"></asp:ContentPlaceHolder>
    <asp:Content ID="scriptContent" ContentPlaceHolderID="scriptContentHolder" runat="server">
    <script language="javascript" type="text/javascript">
       // JS Code here....  
        </script>
    </asp:Content>

    Now, the evolution of this in later versions of Asp.Net the Ajax Toolkit was the introduction of the class ClientScriptManager which was added to better manage client side scripts, .    In this new approach, user should define any scripts in code behind, and register them using methods such as RegisterClientScriptBlock & RegisterClientScriptInclude methods....  

    The code would look like this

    ClientScript.RegisterStartupScript(
    this.GetType(), "PutANameHere", 
    "<script> // JSCode Here </script>"
    );
    

    Note that in the above , the above code would need to be called in the page_load handler

    I am right in my understanding so far ?

    Sunday, November 25, 2018 8:22 AM
  • User475983607 posted

    Both constructs place HTML in a specific location on the page.

    ClientScript.RegisterStartupScript(
    this.GetType(), "PutANameHere", 
    "<script> // JSCode Here </script>"
    );

    Note that in the above , the above code would need to be called in the page_load handler

    RegisterStartupScript can be invoked in any event handler not just the page_load event.

    I am right in my understanding so far ?

    IMO, it is more important to understand JavaScript fundamentals and know there are ASP.NET constructs for rendering JavaScript in Web Forms.

    Sunday, November 25, 2018 1:55 PM