Click here to Skip to main content
15,942,449 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
ok so i am new at JavaScript and trying to understand this part of code:
if (flag == 1)    //On successful updation, shows a popup message
                    string msg = "Operation Successful";
                    System.Text.StringBuilder sb = new System.Text.StringBuilder();
                    sb.Append("<script type = 'text/javascript'>");
                    ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", sb.ToString());

I understand that it's creating a pop-up to display the message "Operation Successful" if the data has been inserted. And Append is to append all those elements together to create 1 big string. I would really love to know about
ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", sb.ToString());

what does this line do? also i am not very sure about

any kind of help is greatly appreciated! thanks!

RegisterClientScriptBlock is injecting the text you've built up in the string builder into the page. The different "Register" methods will inject the text in different locations on the page if you want to make sure the script is at the start of the page, or the bottom. If you view the source of the page you'll see your script block in the source.

The "window.onload=" line does nothing on it's own, all of the text is appended together so the finished text is

window.onload=function(){ // rest of js here };

when the page is viewed this js will create a function with the alert in it and sets that function to be called when the window.onload event is called, ie when the page has loaded in the browser. So after the page has loaded you'll see an alert.

The "alert" parameter of RegisterClientScriptBlock is the key. So the js you've inserted is given the key of "alert" which means that if other controls want to inject the same js they can check to see if js with a key of "alert" has already been added so that the same code is not added again.
Share this answer
Sana Farooq (Sana Qureshi) 6-May-15 5:16am    
thank you for making in fairly easy to understand!
1. ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", sb.ToString());To understand this you can follow Use & Call RegisterStartUpScript, RegisterClientScript and Client-Side Script[^]

2. sb.Append("window.onload=function(){");
I think you have understood the string builder part. For window.onload=function(){ you can follow this one.[^][^]
Share this answer
Sana Farooq (Sana Qureshi) 6-May-15 5:15am    
thank you!
Arkadeep De 6-May-15 6:11am    
you welcome
The first is simple to work out: Google[^] will give you that.

The other is also simple:
sb is a StringBuilder - it allows you to add text to it to build up your total string as you go (without the penalties associated with concatenating strings). You do that by using the Append method:
sb.Append("some string data);

In your case, the data being appended is a JavaScript function declaration.
Share this answer
Sana Farooq (Sana Qureshi) 6-May-15 5:15am    
thanks very much!
OriginalGriff 6-May-15 5:24am    
You're welcome!

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

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900