Tuesday 9 August 2011

RegisterStartupScript vs RegisterClientScriptBlock


I have a site with loads of pages with Ajax tabbed containers on them. Generally each tabbed container is contained within an Update Panel. Some have additional Update Panels on the tabs.

Whenever a user clicks an Update button, I go back to the server, check if they have permission to do the action they are doing and, if they don't return to the page and EITHER do this:



I find, on various pages, one works and the other does not. If I'm using RegisterClientScriptBlock and the alert does not show, I change it to RegisterStartupScript - and it works - the alert shows.

But the reverse can also be true. Sometimes RegisterStartupScript does not work, and if I change it to RegisterClientScriptBlock it works.

I have checked this out in detail on different pages - where the code is pretty much identical i.e. there is an Update Panel containing a tabbed container and nothing else. Sometimes I have to use RegisterStartupScript - sometimes I have to use RegisterClientScriptBlock.

RegisterClientScriptBlock -> Put a function or a block of code (javascript) on the page which you may not want executed immediately.

RegisterStartupScript -> Put some code on the page which you want executed as soon as the page finishes loading.


No comments:

Post a Comment