Using RegisterStartupScript in ASP.NET

The RegisterStartupScript method is similar to the RegisterClientScriptBlock method. The major difference is that the RegisterStartupScript puts the script at the foot of the ASP.NET page as opposed to the top. The RegisterStartupScript method uses the same constructors as the RegisterClientScriptBlock method:

  • RegisterStartupScript (type, key, script)
  • RegisterStartupScript (type, key, script, script tag specification)

So what difference does it make to register the script at the foot of the page as opposed to the top? A lot, is the answer!
If you have JavaScript working with one of the controls on your page, you will not want your Javascript function to fire before the control is placed on the screen if it requires that control, therefore you should use the RegisterStartupScript method instead of RegisterClientScriptBlock.


For example, the below code listing creates a page that includes a simple control which contains a default value of    ASP.NET 101.

 <asp:TextBox ID="txtBox1" Runat="server">Hello ASP.NET</asp:TextBox>

Therefore use RegisterStartupScript to place a script on the page which uses the value in the txtBox1 control, as shown below:

protected void Page_Load(object sender, EventArgs e)
{
string myScript = @"alert(document.forms[0][’txtBox1’].value);";
Page.ClientScript.RegisterStartupScript(this.GetType(),
"MyScript", myScript, true);
}

This puts the JavaScript function at the foot of the ASP.NET page, so when the JavaScript actually starts, it can find the txtBox1 control and works as planned.

Using RegisterClientScriptInclude in ASP.NET

The final method for registering Javascript in an ASP.NET page is the RegisterClientScriptInclude method. Most developers place JavaScript inside a .js file, which is considered best practice because it makes it simple to make global JavaScript changes to the app and .js pages are cached. You can register the script files on your ASP.NET pages using the ClientScriptInclude method as shown below:

string myJavaScript = "myJsCode.js";
Page.ClientScript.RegisterClientScriptInclude("myKey", myJavaScript);

This creates the following tag in your ASP.NET page:

Using RegisterClientScriptBlock in ASP.NET

The RegisterClientScriptBlock method enables you to put a JavaScript function at the top of a page. Thus, the script is in place for the startup of the page in the browser. Its use is illustrated
in the below listing.

<%@ Page Language="C#" %> <script runat="server"> protected void Page_Load(object sender, EventArgs e) { string customScript = @"function myalert() { alert(’ASP.NET 101’); }"; Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "CustomScript", customScript, true); } </script>
<script type="text/javascript">
<!--
function myalert() { alert(’ASP.NET 101’); }// -->
</script>
<div>
<input type="submit" name="Button1" value="Button" onclick="myalert();"
id="Button1" />
</div>

From the above listing you can see that you create the JavaScript function myalert() as a string named customScript. Using Page.ClientScript.RegisterClientScriptBlock you then program the script to be placed on the page. The two possible constructions of the RegisterClientScriptBlock method are the following:
- RegisterClientScriptBlock (type, key, script)
- RegisterClientScriptBlock (type, key, script, script tag specification)