How to order Script s, registered Script Register Manager Registration

I want to warn 1 about registering scripts in this order

 ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "a", "alert(a);", true); ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "b", "var a = 1;", true); 

But this raises an error saying that a is undefined.

I have different controls located in different parts of the page registration scripts, and some of them should be the first and the other the last.

Is there a way to set the order that it will display?

+4
source share
1 answer

No, it is not possible to determine the order in which RegisterClientScriptBlock methods are called. From the documentation for the ScriptManager.RegisterClientScriptBlock method (my selection):

Script blocks are not guaranteed to be displayed in the order in which they are registered . If the order of the script blocks is important, use the StringBuilder object to collect the scripts together on the same line , and then register them all in one client script.

In addition, you can define another key (second parameter; "a" ) for the second call to RegisterStartupScriptBlock , since a registered script is uniquely identified by its key. If you call this method with your parameters, ScriptManager will not register the second script, because another script with the same key ( "a" ) is already registered.

+3
source

All Articles