Tuesday, October 23, 2007

Embedded Java Script in ASP .NET

Many developers like to use the flexibility of writing Java Script in ASP .NET to achieve the functionality they need or notify users of some server-side behavior. For example, if there was some server-side error and the data entered weren’t correctly saved, or the data entered by users are not in the correct format. In this case, we might want to pop up alert message box to inform the users.

Basically there are two ways to write a java script code in ASP .NET. Either call the java script in the html of the page by creating an onload event, or we can use an additional methods provided in ASP .NET to handle this in the code behind and register a client script block with the ASP .NET web page.

There are two methods available to register a script in .NET environment. There are RegisterStartUpScript and RegisterClientScriptBlock.


RegisterStartUpScript
This method takes two string inputs:
RegisterStartUpScript (string key, string script)
// Key – unique key that identifies a script block
// Script – content of the script that will be sent to the client

Typically what will happen is when the user clicks to submit his/her data, we will do some processing and validation checking, and then alert user of some unexpected behavior. Thus, we will call the RegisterStartUpScript() method in the Click event handler of the button.

Here is a simple example for that.

The code first generates an error message by checking field values (in this case if a txtName TextBox control was empty) and builds the message appropriately. Next, we will build the client-side JavaScript script block. Note that before I register the script, I added a conditional if statement, IsStartupScriptRegistered(string key). This is very essential calling this method to avoid unnecessary assembling of the client-side script.
IsStartUpScriptRegistered(string key)
// key – the string key of the start up script to search for. This method will check
// whether the script is already registered or not.


RegisterClientScriptBlock
This method takes two string inputs:
RegisterClientScriptBlock (string key, string script)
// Key – unique key that identifies a script block
// Script – content of the script that will be sent to the client

This is very similar with the RegisterStartupScript() method. You only need to write the Java Script string and pass it as the second parameter in the method with a string key. Before I register the script, I added a conditional if statement, IsClientScriptBlockRegistered(string key). This is very essential calling this method to avoid unnecessary assembling of the client-side script.
IsClientScriptBlockRegistered (string key)
// key – the string key of the start up script to search for. This method will check
// whether the script is already registered or not.



Difference between RegisterStartUpScript and RegisterClientScriptBlock
As you can see from the above, two of the methods are very similar. The main difference between them is RegisterStartUpScript will embed the script BEFORE the CLOSING TAG of the page object’s < form runat=”server” > element, while RegisterClientBlockScript will embed the script AFTER the OPENING TAG of the page object’s < form runat = “server” > element.

Happy Programming!

0 comments:

 

Get paid for your opinions! Click on the banner above to join Planet Pulse. Its totally free to sign up, and you can earn UNLIMITED. Find out more by visiting PLANET PULSE.
July Code Blog Copyright © 2010 Blogger Template Designed by Bie Blogger Template