Sunday, December 26, 2010

ASP.NET – Introduction and Concepts

What is ASP.NET?

ASP.NET is a server side scripting technology,  this technology allows the programmer to write scripts in a web page that will be executed in server side.

  • ASP, Active Server Pages
  • ASP.NET runs in the IIS server
  • IIS, Internet Information Services, is an internet server built by Microsoft
  • IIS is free of charge and comes with windows, but some features are not supported on client versions, to install you just need to access the turn windows features on or off section in windows.

ASP.NET is compiled and not interpreted:

ASP.NET, as well as all .NET applications, are compiled. Is impossible to run C# or VB code without compiling it.

Fortunately the ASP.NET applications doesn’t need to be compiled every time they are executed, instead the Interpreted Language code is created once and updated just when the source code is change. The files stay in the server cache, in the directory  C:\WINDOWS\MICROSOFT.NET\FRAMEWORK\[VERSION]\TEMPORARY ASP.NET FILES

This statement depends on the type of project you choose. If you will choose a Web Project, the source code is compiled when the project is, otherwise if you choose a Web Site, each page is compiled when the Request is made.

Web Forms

The ASP.NET pages, known as Web Forms, are a vital part of an ASP.NET application, because they provide the browser output after the client Request.

  • Same as a HTML page
  • Can contain HTML, XML and scripts
  • Scripts are executed in the server
  • ASPX extension
  • All the controls must be contained inside a
  • The tag must contain the attribute runat=server
  • The attribute runat=server means the form will be processed in server side

How does it work?

  • Applications are executed in server side, so imagine that you have a page that allows users to write comments. The user does this operations in the browser, but to save the comments is needed to make a update in the database, for that the code must run in server side. ASP.NET handles those events using a technique called PostBack, which sends the form when certain operations are executed. When the server receives the page it handles the corresponding events in server side.
  • Web Applications are Stateless, it means they don’t keep the state. When the page is rendered to html, the objects are destroyed and all the client information is discarded. This model is good to increase the scalability and application traffic, but is hard to offer an experience similar to a windows application.
ASP.NET is based in a event model, where the programmer adds a control to the page and choose which event wants to handle.  Each Event Handler is a discrete method that keeps the code clean and tidy.

Events, how do they work:

  1. Page and associated controls are created, initialization code is executed, the html page is rendered and returned to the client and the objects are released from server memory.
  2. At some point the user performs an action that causes a PostBack, such as clicking a button, and every page information is sent to the server.
  3. The server recreates the objects, putting them in the same state as the last time they were sent to the client.
  4. The event that caused the PostBack is handled and the associated code executed.
  5. The modified page is rendered to Html and sent to the client with the new data. The objects are released from server memory.

Page Lifecycle

There are two types of events that cause a PostBack:

  • Immediate Response Events: Executed whenever the user clicks in a button or the javascript function, __DoPostBack() , is used.
  • Change Events: When a control state is changed, like the value of a checkbox or textbox. If the property AutoPostBack is active, the event is executed immediately, otherwise is executed in the next PostBack.
Imagine a page with a submit button and a textbox without the AutoPostBack property active. The user change the text and click in the button, the events execution order is:

  1. Page.Init
  2. Page.Load
  3. TextBox.TextChanged
  4. Button.Click
  5. Page.PreRender
  6. Page.Unload

As for now as introduction this is everything you need to know, the next article will be about Application Anatomy. 

No comments:

Post a Comment