Difference between RegisterClientScriptBlock and RegisterStartupScript asp.net

Difference between RegisterClientScriptBlock and RegisterStartupScript asp.net are as bellowed:

1) The method RegisterStartupScript adds Javascript to the web form right before the ending ‘form’ tag whereas the method RegisterClientScriptBlock adds Javascript to the web form, right after the starting ‘form’ tag .

2) The method RegisterStartupScript is used when you want to assign any value or property or want to get the value or property from any element whereas in case of the method RegisterClientScriptBlock we will get an error as ‘object undefined’.

3) The method RegisterStartupScript holds the code which will run as soon as the browser parses it but the method RegisterClientScriptBlock holds routines and libraries that the whole page uses.

Sample example of RegisterClientScriptBlock  :

Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "RegisterClientScriptBlock", "alert('Using Page.ClientScript.RegisterClientScriptBlock');", true);
ScriptManager.RegisterClientScriptBlock(this.bntClientScript, this.GetType(), "RegisterClientScriptBlock", "alert('Using Page.ClientScript.RegisterClientScriptBlock');", true);

Sample example of RegisterStartupScript :

Page.ClientScript.RegisterStartupScript(this.GetType(), "RegisterClientScriptBlock", "alert('Using Page.ClientScript.RegisterStartupScript');", true);
ScriptManager.RegisterStartupScript(this.bntClientScript, this.GetType(), "RegisterClientScriptBlock", "alert('Using Page.ClientScript.RegisterStartupScript');", true);

Download sample example for detailed study of Difference between RegisterClientScriptBlock and RegisterStartupScript asp.net



Folks you should also refer below articles for better understanding of differences between confusing concepts:



Leave a Reply