DotNetSlackers: ASP.NET News for lazy Developers

Friday, January 7, 2011

Quick Start :To ASP.NET


File name extensions

Web applications written with ASP.NET will consist of many files with different file name extensions. The most common are listed here. Native ASP.NET files by default have the extension .aspx (which is, of course, an extension to .asp) or .ascx. Web Services normally have the extension .asmx.
Your file names containing the business logic will depend on the language you use. So, for example, a C# file would have the extension .aspx.cs. You already learned about the configuration file Web.Config.
Another one worth mentioning is the ASP.NET application file Global.asax - in the ASP world formerly known as Global.asa. But now there is also a code behind file Global.asax.vb, for example, if the file contains Visual Basic.NET code. Global.asax is an optional file that resides in the root directory of your application, and it contains global logic for your application.

All of these are text files

All of these files are text files, and therefore human readable and writeable.

The easiest way to start

The easiest way to start with ASP.NET is to take a simple ASP page and change the file name extension to .aspx.

Page Syntax

Here is quick introduction of syntax used in ASP.NET

Directives

You can use directives to specify optional settings used by the page compiler when processing ASP.NET files. For each directive you can set different attributes. One example is the language directive at the beginning of a page defining the default programming language.

Code Declaration Blocks

Code declaration blocks are lines of code enclosed in <script> tags. They contain the runat=server attribute, which tells ASP.NET that these controls can be accessed on the server and on the client. Optionally you can specify the language for the block. The code block itself consists of the definition of member variables and methods.

Code Render Blocks

Render blocks contain inline code or inline expressions enclosed by the character sequences shown here. The language used inside those blocks could be specified through a directive like the one shown before.

HTML Control Syntax

You can declare several standard HTML elements as HTML server controls. Use the element as you are familiar with in HTML and add the attribute runat=server. This causes the HTML element to be treated as a server control. It is now programmatically accessible by using a unique ID. HTML server controls must reside within a <form> section that also has the attribute runat=server.

Custom Control Syntax

There are two different kinds of custom controls. On the one hand there are the controls that ship with .NET, and on the other hand you can create your own custom controls. Using custom server controls is the best way to encapsulate common programmatic functionality.
Just specify elements as you did with HTML elements, but add a tag prefix, which is an alias for the fully qualified namespace of the control. Again you must include the runat=server attribute. If you want to get programmatic access to the control, just add an Id attribute.
You can include properties for each server control to characterize its behavior. For example, you can set the maximum length of a TextBox. Those properties might have sub properties; you know this principle from HTML. Now you have the ability to specify, for example, the size and type of the font you use (font-size and font-type).
The last attribute is dedicated to event binding. This can be used to bind the control to a specific event. If you implement your own method MyClick, this method will be executed when the corresponding button is clicked if you use the server control event binding shown in the slide.

Data Binding Expression

You can create bindings between server controls and data sources. The data binding expression is enclosed by the character sequences <%# and %>. The data-binding model provided by ASP.NET is hierarchical. That means you can create bindings between server control properties and superior data sources.

Server-side Object Tags

If you need to create an instance of an object on the server, use server-side object tags. When the page is compiled, an instance of the specified object is created. To specify the object use the identifier attribute. You can declare (and instantiate) .NET objects using class as the identifier, and COM objects using either progid or classid.

Server-side Include Directives

With server-side include directives you can include raw contents of a file anywhere in your ASP.NET file. Specify the type of the path to filename with the pathtype attribute. Use either File, when specifying a relative path, or Virtual, when using a full virtual path.

Server-side Comments

To prevent server code from executing, use these character sequences to comment it out. You can comment out full blocks - not just single lines.

First ASP.NET Program.

Now let us have our First ASP.NET program.
Let�s look at both the markup and the C# portions of a simple web forms application that generates a movie line-up dynamically through software.

Markup Portion

Web form application part 1 -- SimpleWebForm.aspx

 Collapse
<% @Page Language="C#" Inherits="MoviePage" Src="SimpleWebForm.cs" %> 

<html>
<body background="Texture.bmp">

<TITLE>Supermegacineplexadrome!</TITLE>

<H1 align="center"><FONT color="white" size="7">Welcome to 
Supermegacineplexadrome!</FONT></H1>

<P align="left"><FONT color="lime" size="5"><STRONG>

<U>Showtimes for <%WriteDate();%></U>

</STRONG></FONT></P>

<FONT size="5" color="yellow"><%WriteMovies();%></FONT>

</body>
</html>
And this is where the C# part of a web forms application comes in.

Web form application part 2 - SimpleWebForm.cs

 Collapse
using System;
using System.Web.UI;
using System.Web.UI.WebControls;

public class MoviePage:Page 
{
    protected void WriteDate()
    {
        Response.Write(DateTime.Now.ToString());
    }

    protected void WriteMovies()
    {
        Response.Write("<P>The Glass Ghost (R) 1:05 pm, 3:25 pm, 7:00 pm</P>");
        Response.Write("<P>Untamed Harmony (PG-13) 12:50 pm, 3:25 pm, " + 
                       "6:55 pm</P>");
        Response.Write("<P>Forever Nowhere (PG) 3:30 pm, 8:35 pm</P>");
        Response.Write("<P>Without Justice (R) 12:45 pm, 6:45 pm</P>");
    }
}

Execution Cycle :

Now let's see what�s happening on the server side. You will shortly understand how server controls fit in.
A request for an .aspx file causes the ASP.NET runtime to parse the file for code that can be compiled. It then generates a page class that instantiates and populates a tree of server control instances. This page class represents the ASP.NET page.
Now an execution sequence is started in which, for example, the ASP.NET page walks its entire list of controls, asking each one to render itself.
The controls paint themselves to the page. This means they make themselves visible by generating HTML output to the browser client.

Execution Process

We need to have a look at what�s happening to your code in ASP.NET.

Compilation, when page is requested the first time

The first time a page is requested, the code is compiled. Compiling code in .NET means that a compiler in a first step emits Microsoft intermediate language (MSIL) and produces metadata�if you compile your source code to managed code. In a following step MSIL has to be converted to native code.

Microsoft intermediate language (MSIL)

Microsoft intermediate language is code in an assembly language�like style. It is CPU independent and therefore can be efficiently converted to native code.
The conversion in turn can be CPU-specific and optimized. The intermediate language provides a hardware abstraction layer.
MSIL is executed by the common language runtime.

Common language runtime

The common language runtime contains just-in-time (JIT) compilers to convert the MSIL into native code. This is done on the same computer architecture that the code should run on.
The runtime manages the code when it is compiled into MSIL�the code is therefore called managed code.

No comments:

Post a Comment