Friday, July 06, 2012

AJAX

AJAX - Asynchronous Javascript + XML

AJAX is not a one new technology or a language. It's just a way of using a set existing standards and technologies. It's all about updating a parts of the web pages by exchanging data with the server without reloadng(postback) the whole page. Rather than reloading the whole page parts of the web page does callbacks to the server. There are no any prerequisites (additional tools or software) to use AJAX in one of your web pages you just have to know how to write some code in javascript.

XMLHttpRequest(XHR) - API in web scripting languages such as JavaScript to send direct HTTP/HTTPS requests to the server and return the response back to the script.

The example below shows how to update a part of a web page without reloding the whle page(postback). The AJAX script or the code to implement the scenario 

is written in Javascript. 


Creating a XMLHttpRequest Object

xmlhttp = new XMLHttpRequest();

This is the basic syntax for creating a XMLHttpRequest object. This syntax might differ for older versions of some of the browsers.

xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); - for IE 5 and 6


Request

Request can be sent to the server by using open and send methods of the XMlHttpRequest object. 

xmlhttp.open("GET", "movie_catalog.xml", true);
xmlhttp.send();

There are 3 parameters passed to the open method.

method - Request type. This can be either GET or POST
url - Location of the file which is in the server where the response is returned from
async - asynchronous or not (true - asynchronous)


Response

There are 2 properties which can be used to get the response from the server.

responseText - when the response is in string format.
responseXML - when the response is in XML format.


onreadystatechange

A function can be stored in this property and it is called everytime when the readyState property of the XMLHttpRequest object changes.


readyState

This holds the status of the request object which ranges from 0 to 4
0: request is not initialized 
1: connection established to the server
2: request received 
3: request processing
4: response is ready


status

This holds standard HTTP status codes returned by the request
E.g:-
200: OK
403: Forbidden
404: Not Found


Example 1 : Get the content from a text file which is located in the server and display in a part of the web page.






































Example 2 : Get the content from a XML file which is located in the server and disply in a part of the web page




 


Example 3 : Get the response another web page which is located in the server.










Thursday, July 05, 2012

ScriptManager.RegisterDataItem Method (Control, String)

How can you update a control which resides outside the update panel in an asynchronous post back?


You can use ScriptManager.RegisterDataItem to send data from the server to client whether the response control is inside the update panel or not. RegisterDataItem method can be called only during an asynchronous postbacks. The data item which is registered with the RegisterDataItem method can be accessed through the client script in  pageLoading, pageLoaded, and endRequest events of the PageRequestManager object. When you handle the event, custom data is passed in an event argument object. If you have a handler for pageLoaded event, customer data is passed in the PageLoadedEventArgs class which exposed dataitems property.





Wednesday, July 04, 2012

Named and Optional Parameters

Named and optional parameters have been there for Visual Basic for sometime but haven't been available in C# until the version 4 release. Name parameters allow you to pass parameters by the parameter names disregarding the order of parameters in the method signature. Optional parameters allow you to provide default values for some of the parameters in your method.


Named Parameters
















Optional Parameters

Optional parameters allow you to provide default values for some of the parameters in your method and acts as a type of overloading. If you want to add one or more new parameter to one of your existing methods and you still need the existing functionality to behave as it is and the new parameters doesn't effect the existing functionality, the solutions is to repeat the same method with the additional parameters and a different method body. This is called overloading. 

If you use optional parameters for this purpose you can add the extra parameters as default parameters and handle those in the existing method body. You don't have to add a new method or modify the code where the existing method was called.



Monday, July 02, 2012

.Net: Instantiating



When instantiating classes, the class which is on the left side of the equation is called the declarative class and the class which is on the right side is called the instantiating class.

































Base class called "Animal" contains 2 methods as

public void Eat()
public void Sleep()



Sub class called "Lion" which derived from "Animal" contains 2 new methods as
public void Hunt()
public void Walk()
















When instantiating with base class as the declarative class and the derived class as the instantiating class, what will be structure of the instance? You'll see it carried 2 public methods of the base class. Actually this instance will carry all the methods, properties and variables from base class (according to encapsulation and other object oriented principles). This will not relevant if you override or shadow the base class methods.