Tuesday, August 9, 2011

Understanding Postbacks

Hi newbies, in this session I try to explain about the term called "POSTBACK" in as unsophisticated and effortless way as I possibly can. In very simple terms, a postback means one single round trip to the server, i.e. a call(request) made to the server by a client (browser) and its corresponding response by the server, together is known as a postback.
          In ASP.NET, a postback can be identified by a page refresh, which means, a good page should have minimum number of postbacks- I mean, who likes to see page refreshes, until unless, its done intentionally, right??? To send some data, and then receive the response back, there is no alternate way but to do a postback. But nothing much to worry, since there already are ways, to hide such page refreshes (using AJAX calls, will talk about later). So, for now, one of the major thing to keep in mind is, to minimize the number of postbacks. It is one very major trick in developing a good, efficient and intuitive webpage using ASP.NET.
          Always attached with postbacks, is another very closely interlinked term "VIEWSTATE" . One thing to always keep in mind during web developemnt is that, HTTP is stateless at its core. HTTP (HyperText Transmission Protocol) is the predominant technology used to communicate on internet. Details are out of our topic of concern. Stateless, means, everytime a page loads, even though you see everything similar to the one before postback, it is not the same as the previous ones. The controls are regenerated and entirely new ones, which have no assoication or relation or link to the older ones. That's the innate characteristics of the HTTP, which has its own positives and negatives. So, even though we like it or not, we are bound to cope with it (but trust me, it's not as bad as it might sound in the first time). Thus, coming back to our discussion of VIEWSTATE, new controls implies controls with no data or default data. This is one major concern on the web. The users are not supposed to know about this stateless feature of the HTTP; and its the responsibility of the developers to hide it from the users. Imagine, you fill a long-long sign up form, click submit, and due to some error, the page returns back to the same page, with all the values inside the text-boxes gone. How hectic it would be, to refill the same form again-and-again. Yes, this is where, VIEWSTATE comes to our rescue. Viewstate is the ASP.NET's answer to HTTP-statelessness.  Here is an excerpt from the msdn site :

"View state is the method that the ASP.NET page framework uses to preserve page and control values between round trips. When the HTML markup for the page is rendered, the current state of the page and values that must be retained during postback are serialized into base64-encoded strings. This information is then put into the view state hidden field or fields."

         It is an automatic process that occurs with every postback, and the handler library (aspnet_isapi.dll) internally handles it. As discussed in my previous section about the page-events, viewdata maintenance is handled in between the Page.PreRender and Page.Render events. Every time a page is reloaded, the previous data is restored back to the controls just before it is re-server by the server. Of course, it is a matter of choice, and can be opted not to be restored according to the demand of the situation. The generated viewstate is encrypted for obvious reasons. The encrypted version of the viewstate stored inside the webpage can be viewed from its page source. It is stored inside the hidden form field, "__VIEWSTATE" variable . A sample code to maintain a page's viewstate using the built-in classes of the .NET platform is shown below :

/* CODE BLOCK */


       Thank you all for now. Next time, we'll get into the code and create our first ASP.NET webpage. Take care till then. Live life.

Monday, August 8, 2011

ASP.NET Page Life Cycle

Hi newbies, I'm assuming that you all are already convinced about why you should choose ASP.NET as your web development platform, among several other options available; which again implicitly implies that, you all are well aware of the performance, efficiency, scalability, ease-of-use and other such features of this platform. There are many resources(books, whitepapers, forums, etc) available in the market as well as online, which discusses about those facets of ASP.NET in detail (Check out the resources page to get a list of my personal favorites). So, without any more fuss, let's come to the point.
    Today, I talk about ASP.NET page life cycle. As always, I personally believe that, to learn any new technology(or any other thing), stick to the KISS paradigm, where KISS(as I expect, most of you already know) = Keep It Simple Silly. This is the best way to approach a new fish in the sea; and let other complex things come your way naturally during the course of time.
    Every ASP.NET web application resides inside an application domain setup by the server(IIS, in most cases) itself. This application domain ensures the separation of concern between other web applications hosted on the same server and which are running simultaneously, so that the resources of each web application remain isolated and concealed from all others. That means, there is no way, a application can interfere in the operation of its neighbor application (and yes, that's a really good thing).
The image below shows an overview of an ASP.NET page life cycle.

click to view in large size





Whenever you request a page from the server(by typing a web-address in your browser), the request flow takes place in the following sequence:

- the browser sends the request to the server ==> it tells the server that my boss wants this page, so I don't care how, just send to me, as quick as you can.

- the server receives the request and checks for the assigned handler ==>as soon as the server detects an incoming request, it knows where to send it for further processing.

- the server redirects the request to the ASP.NET handler dll (aspnet_isapi.dll) ==> Mr. Request Handler,  please process the reqeust I am sending you, and send me back the output so that I can serve my client.

- the handler then, identifies-analyzes-processes the request and sends its output back to the server which then sends it back to the requesting client's browser ==> Here, Mr. Server, please have this data and send it back to your client, I've setup everything and packed it into the data I'm sending you, so that your client's browser should not have any sort of problem in displaying it.

[Note : REQUEST-HANDLING :  a class, ApplicationManager, creates an application domain, and sets up everything else required, for the application to run; which includes things like, HttpContext, HttpRuntime, HttpRequest, HttpResponse, etc. ]


A page is internally generated through a MHPM ( Module, Handler, Page and Module event) request. The sequence of the events that are fired, is tabulated below. This is the most important aspect of ASP.NET page request, and the only thing that you should keep in your mind, from this blog.



Section



Event



Description


HttpModule

BeginRequest

This event signals a new request; it is guaranteed to be raised on each request.

HttpModule

AuthenticateRequest

This event signals that ASP.NET runtime is ready to authenticate the user. Any authentication code can be injected here.

HttpModule

AuthorizeRequest

This event signals that ASP.NET runtime is ready to authorize the user. Any authorization code can be injected here.

HttpModule

ResolveRequestCache

In ASP.NET we normally use outputcache directive to do caching.  In this event ASP.NET runtime determines if the page can be served from the cache rather than loading the patch from scratch.  Any caching specific activity can be injected here.

HttpModule

AcquireRequestState

This event signals that ASP.NET runtime is ready to acquire session variables. Any processing you would like to do on session variables.

HttpModule

PreRequestHandlerExecute

This event is raised just prior to handling control to the HttpHandler. Before you want the control to be handed over to the handler any pre-processing you would like to do.

HttpHandler

ProcessRequest

Httphandler logic is executed. In this section we will write logic which needs to be executed as per page extensions.

Page

Init

This event happens in the ASP.NET page and can be used for :-
·


    

Creating controls dynamically, in case you have controls to be created on runtime.

·


    

Any setting initialization.

·


     

Master pages and them settings.

In this section we do not have access to viewstate , postedvalues and neither the controls are initialized.

Page

Load

In this section the ASP.NET controls are fully loaded and you write UI manipulation logic or any other logic over here.

Page

Validate

If you have valuators on your page, you would like to check the same here.

Render

It’s now time to send the output to the browser. If you would like to make some changes to the final HTML which is going out to the browser you can enter your HTML logic here.

Page

Unload

Page object is unloaded from the memory.

HttpModule

PostRequestHandlerExecute

Any logic you would like to inject after the handlers are executed.

HttpModule

ReleaserequestState

If you would like to save update some state variables like session variables.

HttpModule

UpdateRequestCache

Before you end if you want to update your cache.

HttpModule

EndRequest

This is the last stage before your output is sent to the client browser.

          Let me repeat once again, to have a good knowledge of the ASP.NET events, is a great asset to have, in your web-development career. You will always need to use one or more of them, in one situation or the other. We will delve into the complexities and scenarios where you might have to use those events, in our coming sessions. So, I guess this is more than you can take for this session. See you all next time. Cheers.Live life!!!

Tuesday, August 2, 2011

Introduction to ASP.NET

ASP.NET is a web-programming platform that features a completely object-oriented programming model, which includes an event-driven, control based architecture that encourages code encapsulation and code reuse. The .NET platform is very rich platform, and even more so, in terms of the number of languages it supports(C#, VB.NET, J#, F#, etc). And since ASP.NET is embedded into the .NET framework, it inherits the advantage of support of all those languages; in other words, you have the freedom to choose the language of your choice to create ASP.NET webpages. To absolute newbies, OOP or the Object Oriented Programming is a convention developed in the programming world which treats all the concepts of programming as an object with some definite attributes and properties. OOP is a subject of its own, so to delve into its details would derail us from our main focus. Interested readers can visit this wikipedia link for more detailed discussion of OOP : [wikipedia : Object-oriented-programming] .

Along with all that, developers at microsoft seem to have given special concern to the performance and efficiency of this platform. The components are compiled on-the-fly(during runtime) as and when required than being interpreted everytime they are needed, which results in a significant improvement in overall efficiency. To further boost the performance, ASP.NET comes with a fine-tuned data-access model and also includes very flexible data caching features.

So, to sum it all up, these are the following seven characteristic features of ASP.NET : 
1) ASP.NET is a sub-component of the .NET framework and hence is very-very tightly integrated to it.
2) ASP.NET is compiled and not interpreted.
3) ASP.NET supports all the language that abide by the rules and specifications defined by/in the framework's CLR(Common Language Runtime) . These set of rules form, what's more commonly called CLS(Common Language Specification).
4) ASP.NET is hosted by the CLR.
5) ASP.NET is fully object-oriented.
6) ASP.NET is multi-device and multi-browser.
7) ASP.NET is easy to deploy and configure(personally I'm not very sure, but can't ignore that all of 'em out there believe so) .