Thursday, August 30, 2007

Difference Between RegisterClientScript & RegisterStartupScript

The main difference is that the RegisterStartupScript method places the JavaScript at the bottom of the ASP.NET page right before the closing /form element. The RegisterClientScriptBlock method places the JavaScript directly after the opening <form> element in the page.

Both of these methods take two strings as input. The second parameter, script , is the client-side script-including the opening and closing script tags. The first parameter, key , serves as a unique identifier for the inserted client-side script.


The RegisterClientScriptBlock method inserts the client-side script immediately below the opening tag of the Page object's <form runat="server"> element.

The code cannot access any of the form's elements because, at that time, the elements haven't been instantiated yet. This explains why hdnView variable had a null value in my case. The RegisterStartupScript method inserts the specified client-side script just before the closing tag of the Page object's <form runat="server"> element.

The code can access any of the form's elements because, at that time, the elements have been instantiated. The choice of which method to use really depends on the "order" in which you want your script to be run by the browser when rendering the page.

As for example if you have declared a hidden variable and want to use in your java script by any of these method then java script method registred with RegisterClientScriptBlock method won't be able to find the hidden variable as the form elements are not instantiated yet.

Enjoy..



No comments:

back to top