Friday, May 30, 2008

Using Ajax in Asp.Net


Before learning programming with Ajax It is necessary to understand Ajax and when it is worth full to use.

What is Ajax?
Ajax is a library to help you call methods of server side in client (browser) without refreshing the whole page but part of page. Let’s say you have list of countries as soon as user selects a country you need to show cities of that country in another dropdown. Imagine you have different elements in this page. What you need is just updating city dropdown. This is the best case to use Ajax since other part of page will stay in the page and users just feel that city dropdown is updated.

Ajax Help you to call server methods in client side without updating the whole page.

When using Ajax is worth full?
After working with Ajax a lot of programmers decide to use this technology a lot however, this is obviously a big mistake. Ajax should be used when implementing a functionality is completely impossible with javascript or massive data should be loaded to client. Let me give you an example if you need change data of a datagrid based on a dropdownlist it is not worth to write this code with javascript. If you need to just hide a part of data like a panel based on a check box it is not worth full to write this code with ajax.

There is a trade-off between using Ajax and JavaScript. Never use Ajax when you are able to implement the same functionality with JavaScript as long as you are not forced to load massive data to client.

How to create Ajax application?
If you are using visual studio 2005 you may need to download ajax library. If you are using visual studio 2008 you have ajax library. After installing ajax you can create Asp.net Ajax-Enabled Web site from file new Web site. If you take a look to this project you will understand that there are some codes being added in web.config to enable ajax also at least for default page you will see a tag which is ScriptManager.

Ajax Elements:
In each page that you need to use ajax technology you defiantly need a ScriptManger control which has no user interface but it mostly prepare ajax technology for that page. This control will add some library to browser and some javascript. It is important to put this control as the first control in html view in your page. Then you can use an update panel to place controls that are going to be updated between postback. For example let’s say we have a page with different content but a grid should be updated without posting back the whole page so we put that grid inside the Update Panel. If your task is time consuming you can use updateProgress control to show a picture identifying you are loading data. In this post I am going to talk about UpdatePanel. I will examine other controls in next posts.

A sample code:
In this sample I used the same product table was created for other samples of website which is a simple product table I need to show information of this table in a Gridview based on price. So when 200 is defined as price we need to show all products that are less than 200. Take a look to this picture. Then download the code and also see the clip.


Sunday, May 25, 2008

.Net Remoting Activator Sample

In the previous lesson of Remoting I talked about remoting basics I examined a code sample. If you take a look to that post some people get back to me with the question how we can hide service from client process well this lesson is talk about how to hide Service from Client. First take a look to what we discussed in last lesson.

As you see in above diagram client application is talking to server application to create an object which will be created in server. This object metadata (definition) is inside service app. What client needs to talk to server application is a proxy object. This proxy object will be created based on one of the classes in service app. For example if you have a class called Test with one method called Dosth. The proxy object in client will be a class called Test with one method called Dosth. When this method is called proxy object path this call to the serve so in the server the real object will be created then the method in serve is called. So Server definitely needs the service app to understand the Type “Test”. The Issue here is that in reality in most of the projects we do not like to give client the service application because a clever client may open this library and then could be a threat to our business rule or security. But how client can create proxy object if no Type is available in client process?

See the movie please I explained more about this issue.

There are two ways to solve this issue:

  1. You could create the same service app and change all code in new service app in a way that all methods even constructors throw an exception (I do not recommend this way since it is time consuming and there is no standardization. )

  2. Using interface between client and server is another approach. It means that each class in service app inherits from an interface it means that we need extra application that has just interfaces. Client has this library from where it can find metadata to create the proxy object. Meanwhile, client is not able to see the service implementation. We are done, However, there is a big issue here: if we just give client an interface then the proxy object will be like interface how we are going to create an object from interface? Well we do not create proxy object. We use Activator class to create an object from server application but map that to the interface take a look to this picture

In above diagram as you see client just access to IService app that has just interface and interface does not have any implementation. And server has both of them. Server exposes Test object. Remember Test object is a type of ITest as well (because of inheritance) and client receive The test object but the way client look to Test object is ITest then when client call ITest methods the Test methods are called.

How to write activator code?
The coding that we had in previous example in client slightly changes take a look at this line of code. Let’s say I have a Hello class and IHello interface then the code in client is like this:

IHello h = (IHello)Activator.GetObject (typeof(IHello),"http://localhost:4000/Myuri");

Activator object create an object in server as you see and then map that type to interface. Please see the code and also the clip.

download the Samaple Code


see the clip