How do I include JavaScript in an ASP.NET page?

You can include a script block in your ASP.NET page like this:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="HTML5Test.aspx.cs" Inherits="HTML5Test" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script type="text/javascript">
   function alertOnClick()
   { 
        alert('Clicked It!');
   }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>HTML5 Test</title>
    <link href="App_Themes/MMTheme/mm.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <form id="form1" runat="server">
    <div>
     <asp:Button ID="Button1" runat="server" Text="Button"></asp:Button></p>
    </div>
    </form>
</body>
</html>

You can then add the JavaScript to an event on one of your ASP.NET controls like this:

protected void Page_Load(object sender, EventArgs e)
{
   Button1.Attributes.Add("onclick", "alertOnClick();");
}

© (c) 2026 Oak Leaf Enterprises, Inc., 1996-2026 • Updated: 09/22/11
Comment or report problem with topic