3

i need individual explanation and individual advantages regarding RegisterClientScriptBlock & RegisterStartupScript & responce.write("script");

i got some information like

RegisterClientScriptBlock() methods will inject the script after the form open tag but before page controls

RegisterStartupScript() methods will inject the scripts after page controls but before form close tag.

we can also simply wrie responce.write("script") it aslo include the script.

but i need individual explanation

if any link is available regarding this topic also pls reply me.

1 Answer 1

3

If you use the RegisterClientScriptBlock() and you need to reference other javascript methods or html objects that may get injected, you run the risk of them not being created when the script is run. Using the RegisterStartupScript(), as you said, puts the script at the bottom of the page, garanteeing that all objects will have been rendered and created before your script is run.

If you are using either to add a script that is a function like:

<script type="text/javascript>
    function myFunc(){
    ...
}
</script>

Then is doesn't matter, because that function needs to be called explicitly, by something else.

But if you are trying to have a script run after the document loads like:

<script type="text/javascript>
    doStuff();
</script>

Then you should use RegisterStartupScript() so you are garanteed that any objects called by you or the functions you call exists.

Sign up to request clarification or add additional context in comments.

1 Comment

ya i understand build in functions can be injected using RegisterClientScriptBlock() but same can be done through responce.write(); also know ???? like responce.write(<script>alert("HI")</script>); same can be donw through RegisterClientScriptBlock() then waht is differnce to use RegisterClientScriptBlock() ??????

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.