JavaScript

How to run JavaScript from code behind in ASP.NET

This tutorial for asp.net programmers will show you how to run JavaScript from code behind follow the next steps:

1. Set the Body tag in your page/master page:

<body runat="server" id="MyBody">

2. Write the script tag in your page:

<script src="MyFunction.js" type="text/javascript"></script>

3. In the code behind attach the onload attribute to the body tag:

protected void Page_Load(object sender, EventArgs e)
{
MasterPageBodyTag = (HtmlGenericControl)Page.Master.FindControl(“MyBody”);
MasterPageBodyTag.Attributes.Add(“Onload”, “MyFunction()”);
}

Related …

Learn more