In the past, developers typically mixed Javascript into active server pages and assorted Microsoft web development code.

Today, ASP.NET offers new models for this aspect of development giving developers a number of options.


In page-based development, adding Javascript to a server-side control allows for richer manipulation; for example, after dragging and dropping an HTML control for a button and modifying the appropriate settings, Javascript allows the time on the button to be displayed as the user's time rather than a time retrieved from the server. Javascript allows for more control over certain settings, which may include problematic default settings or performance. Use a server control's attribute property to add Javascript to control-specific controls.


The common functionality of button rollover consists of a mouse hover over a button causing the button to change color or shade. This simple function proves important in rich web applications or systems with substantial fields and buttons. The HTML DOM event onmouseover mixed into standard control code provides the functionality, and virtually any other event can be used in a similar way.


In Web Form-based development, no property exists for setting server control focus. Javascript solves this problem through adding appropriate code to the body. Convert the body tag to a server control to change focus dynamically. Also, change focus by modifying code in server-side events.


Beyond useful snippets in controls, larger functions can be employed. There are 2 ways to achieve this: the RegisterStartupScript method and the RegisterClientScriptBlock method. The RegisterStartupScript method renders script after all elements load, which prevents the script from attempting to manipulate missing elements (i.e., those which have not loaded yet).

Adding functionality like a rollover to a control triggers a separate request to retrieve information from the server. A cleaner way to achieve this functionality requires no additional requests because the information downloads on loading of the page. In the RegisterClientScriptBlock method, Javascript code for this purpose employs only a Page_Load event and a button-click event. This method renders the script after the Viewstate tag, and before any elements load; furthermore, the browser executes this script immediately. This method usually works better as a function definition, which the RegisterStartupScriptMethod calls. A best practice is separating Javascript (in a .js file), and using the two methods described above to incorporate Javascript.