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.

DZone Most Valuable Blogger

MVB Logo

MVB Logo

Yes, you heard that right. I am now a MVB (Most valuable Blogger) of DZone. Yesterday I got the mail from Ryan Spain (Content Curator, DZone) saying…

DZone MVB Mail

DZone MVB Mail

What is the MVB Program?

Here is what DZone describes about this program:

DZone’s Most Valuable Blogger program brings together a group of highly talented bloggers, authors, and technologists actively writing about topics of interest to the developer community. These people are recognized in the industry for their contributions and deep technical knowledge on subjects ranging from software design and architecture to programming on a range of platforms including Java, .NET, Ruby and others.

Refer – About Most Valuable Blogger (MVB) Program to explore more.
My DZone profile – http://dotnet.dzone.com/users/taditdash looks really cool.

DZone MVB Profile

DZone MVB Profile

It all started…

…Way back in January, when Code Project awarded me as a Most Valuable Professional. I was contributing mostly on Forums and Social Sites. After I got the award, I started this Blog. Blogging gave me the platform to explain my day to day problems with my friends and followers.

Then, during my birthday month (April 2014), Microsoft recognized all my had work and awarded me as a Most Valuable Professional. Thanks to Mr. Biplab Paul, my MVP Lead and my fellow MVPs.

I was surprized when my CEO Mr. Chinmoy Panda wrote about me on Mindfire Solutions Blog. Let me quote couple of lines here…

His story inspires me. Coming from the small town of Nayagarh, Tadit joined Mindfire at Bhubaneswar 3 years back. After proving his worth at work, his voluntary energy led to responsibility for “extra non-work stuff”, and subsequent awards, at Mindfire. He moved ahead to receive a CodeProject MVP award few months back, and now he has received the Microsoft MVP recognition. That’s not all.

He is not only Mindfire’s first Microsoft MVP, but also the first Microsoft MVP from Bhubaneswar, a city with 10,000 software engineers! Wonderful.

Be awesome. Be Tadit.

The Blog really meant a lot me. Mindfire Solutions always encouraged my activities and motivated me to do more and more. I did not look back after that. I am also getting good response from the readers. So, I am enjoying.

Again, I would like to dedicate this to my…

Grand Father (Ganeswar Tripathy). He is always with me and guiding me towards every step I take.

What’s Next?

It’s another tag that has been added to my profile. By becoming a DZone MVB, I gave DZone permission to publish some of my blog articles on their site. This will give me chance to reach out to maximum people.

I have started speaking on Events. If you are doing any Event or know someone who is organizing, do connect them with me. I would love to speak about my Experience or about the Technology I am working on.

Following are my Social Profiles to connect with me.
Facebook – https://www.facebook.com/taditdash
Twitter – http://www.twitter.com/taditdash
LinkedIn – https://www.linkedin.com/in/taditdash
Google+ – https://plus.google.com/u/0/+TaditDash/

Also, if you have not followed my Blog, just register your mail id in the box at right side saying “Follow Blog Via Email“. All my new Blogs will be directly delivered to your inbox.

Thank you everyone for following my blog. Please don’t forget to share your feedback about my articles. This will help me to write much better contents in future.