Jose Saiz

Jose Saiz

  • 1.8k
  • 161
  • 35.7k

HOW To Add script to the end of the ASP.NET page

Mar 31 2020 12:54 PM
Help on adding script to the end of the ASP.NET page after the HTML </body> tag from code behind
 
Default.aspx 
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default_Page" %>  
  2.   
  3.   
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  5.   
  6. <html xmlns="http://www.w3.org/1999/xhtml">  
  7. <head runat="server">  
  8.     <title>Testing</title>  
  9. </head>  
  10. <body>  
  11.     <form id="form1" runat="server">  
  12.     <div>  
  13.         <p><input type="button" id="myjs" value="Click" /></p>  
  14.     </div>  
  15.     </form>  
  16. </body>  
  17.   
  18.  <!-- Insertted Script should go here after inserted from Code behind --> 
  19.   
  20. </html>  
  1. I would like to add this script or any other script after the </body> tag and before </html> tag from the behind code
  2. <script>  
  3.       document.getElementById("myjs").addEventListener('click', function () {  
  4.           alert("Hello World");  
  5.       });  
 Is the above possible?
  1. I tried this from code behind

  2. //- Quick and dirty script string to make my question clear  
  3. string MY_SCRIPT_STRING = "<script>alert('Hello Word')</script>";  
  4. this.Controls.Add(new LiteralControl(MY_SCRIPT_STRING));  
  5.   
  6. This works but it adds the code inside the form not at the end of the page where I want the script code to go after the HTML Tag </body>  and before </html>
 TIA
 

Brought to you by:

Answers (3)