Perform Javascript from codebehind with RegisterStartupScript

In a website, you might want to perform javascript after the postback to enhance the user experience of the visitor. Of course you’re able to perform the javascript within the document ready state of the page. But you might want to differ the function called, based on what the visitor filled into the form that has been posted.

Then you can still register a piece of javascript in the code-behind with the help of ClientScriptManager.RegisterStartupScript. This piece of javascript will then be executed after the page has loaded. I use the snippet below to navigate to an anchor after the postback of a form.

VB:

C#:

Leave a Reply

Your email address will not be published. Required fields are marked *