public knowledge blog_load(sender Virendra)

                                                                                                                          Nothing shocks me, I am a Software Engineer.

Difference Between RegisterClientScript & RegisterStartupScript

Posted by Virendra Dugar on February 13, 2009

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..

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: