Click here to Skip to main content
15,891,136 members
Articles / Web Development / ASP.NET

Two Important Notes On RegisterStartupScript

26 Dec 2014CPOL2 min read 27.7K   5   8
In this blog, we will explore some interesting stuff related to the RegisterStartupScript method.

In this blog, we will explore some interesting stuff related to the RegisterStartupScript method.

Showing One Alert Box

C#
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 code to run another script.

C#
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…

C#
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, the 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.

C#
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 it, please share among your friends.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Proud Indian | Author | TEDx Speaker | Microsoft MVP | CodeProject MVP | Speaker | DZone Most Valuable Blogger| jsfiddler

My Website

taditdash.com

Programming Community Profiles

jsfiddle | Stack Overflow

Social Profiles

Facebook | Twitter | LinkedIn

Awards


  1. DZone Most Valuable Blogger
  2. Microsoft MVP 2014, 2015, 2016, 2017, 2018
  3. Code Project MVP 2014, 2015, 2016
  4. Star Achiever of the Month December 2013
  5. Mindfire Techno Idea Contest 2013 Winner
  6. Star of the Month July 2013

Comments and Discussions

 
Questionalternate for appapp_start Pin
AJdebugger28-Dec-14 5:26
AJdebugger28-Dec-14 5:26 
AnswerRe: alternate for appapp_start Pin
Tadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)28-Dec-14 17:55
protectorTadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)28-Dec-14 17:55 
GeneralRe: alternate for app_start Pin
AJdebugger8-Feb-15 4:58
AJdebugger8-Feb-15 4:58 
GeneralRe: alternate for app_start Pin
Tadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)8-Feb-15 19:44
protectorTadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)8-Feb-15 19:44 
Questionparameter Pin
thewazz26-Dec-14 7:24
professionalthewazz26-Dec-14 7:24 
AnswerRe: parameter Pin
Tadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)26-Dec-14 16:23
protectorTadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)26-Dec-14 16:23 
GeneralMy vote of 2 Pin
BillWoodruff25-Dec-14 20:10
professionalBillWoodruff25-Dec-14 20:10 
GeneralRe: My vote of 2 Pin
Tadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)25-Dec-14 20:23
protectorTadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)25-Dec-14 20:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.