Hi
I am just wondering why some people suggest RegisterStartupScript() to call client side js while some suggests RegisterClientScriptBlock().
Please make me clear whats the difference between the two as they doing the same operations, for using js statement calls and which one is preferable if I only use js statements like alert, return confirm from codebehind.
The
RegisterClientScriptBlockmethod 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.The
RegisterStartupScriptmethod 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.Normally you would use
RegisterClientScriptBlockif you want to register js-functions and useRegisterStartupScriptif you want to call these functions or want to access controls on your page.For further informations have a look at MSDN.