How to call JavaScript from ASP.NET code behind

To call JavaScript from C# code behind you should:

1. Set the Body tag in your page/master page to runat=”server” and an id=”MyBody”.
2. Code 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()”);
}