Two Important Notes On RegisterStartupScript

In this Blog, we will explore some interesting stuff related to the RegisterStartupScript Method.

Showing one Alert Box

string script = "alert('Hello World !!!')";

ClientScript.RegisterStartupScript(this.GetType(), "script1", script, true);

If you write this code in code behind, it will show one Alert Box on the page. Let’s see how the script is added to the page dynamically. Below is the FireBug Script View.

Example 1 Rendered Script for Alert with RegisterStartupScript

Example 1 Rendered Script for Alert with RegisterStartupScript

Showing two Alert Boxes

Now, let’s write some more codes to run another script.

string script = "alert('Hello World !!!')";
string script1 = "alert('Hello World Again !!!')";

ClientScript.RegisterStartupScript(this.GetType(), "script", script, true);
ClientScript.RegisterStartupScript(this.GetType(), "script", script1, true);

Will this work !!! Let’s try in browser and see. Only, one Alert showing on browser instead of two.

Alert Box on Browser

Alert Box on Browser

This is how it looks in script Tab of FireBug…

Example 2 Rendered Script for Alert with RegisterStartupScript

Example 2 Rendered Script for Alert with RegisterStartupScript

So, the question here is where is the next Alert? Why it did not work? Why it did not get rendered on browser?

What happened to the second Alert Box?

So, after this, I dug more into the code, after getting a coffee and try to see carefully what I have written. The secondĀ parameter of the Method is actually a key.

A startup script is uniquely identified by its key and its type. Scripts with the same key and type are considered duplicates. Only one script with a given type and key pair can be registered with the page. Attempting to register a script that is already registered does not create a duplicate of the script.

Call the IsStartupScriptRegistered method to determine whether a startup script with a given key and type pair is already registered and avoid unnecessarily attempting to add the script.

The very first line clears everything. There is also one method to check whether the script is already registered or not.

We should have Unique Keys for every Script we register

So, without further delay, I quickly changed the key. So, the code will look like…

string script = "alert('Hello World !!!')";
string script1 = "alert('Hello World Again !!!')";

ClientScript.RegisterStartupScript(this.GetType(), "script", script, true);
ClientScript.RegisterStartupScript(this.GetType(), "script1", script1, true);

Now, question is, will it work? Yes/No !!! Let’s test.

Still Not Working !!!

Oops !!! Nothing worked. Rendered script is as follows.

Example 3 Rendered Script for Alert with RegisterStartupScript

Example 3 Rendered Script for Alert with RegisterStartupScript

Can you see, what is the issue? If not, then the following image of Console will clarify all our doubts.

Example 3 Console Error

Example 3 Console Error

Head bang, we are missing Semicolons !!!

So, we are actually missing a semicolon (;) after the first line of code. In JavaScript, semicolons are optional, provided the code lines are separated by new line character. But here, RegisterStartupScript adds the scripts in one line, which bugs the page eventually. Let’s modify our code again to include semicolons after the Alert statements.

string script = "alert('Hello World !!!');";
string script1 = "alert('Hello World Again !!!');";

ClientScript.RegisterStartupScript(this.GetType(), "script", script, true);
ClientScript.RegisterStartupScript(this.GetType(), "script1", script1, true);

Now, it perfectly works, showing two Alert Boxes one after the other.

Conclusion

We explored the following points.

  • Key in the RegisterStartupScript method should be unique.
  • Each statement of JavaScript should have a semicolon at last, so that it will treat the next JavaScript statement as code.

I hope you enjoyed reading the Blog. Feel free to comment on the Blog. If you liked, please share among your friends.

One thought on “Two Important Notes On RegisterStartupScript

Leave a comment