ASP.NET raises all of the following events (in this order):

Page.Init
• Page.Load
• TextBox.TextChanged
• Button.Click
• Page.PreRender
• Page.Unload

The following list shows the major stages in the process flow of an ASP.NET page:

• Page framework initialization
• User code initialization
• Validation
• Event handling
• Automatic data binding
• Cleanup

View State Chunking

The size of the hidden view state field has no limit. However, some proxy servers and firewalls refuse to let pages through if they have hidden fields greater than a certain size. To circumvent this problem, you can use view state chunking, which automatically divides view state into multiple fields to ensure that no hidden field exceeds a size threshold you set.
To use view state, you simply need to set the maxPageStateFieldLength attribute of the
element in the web.config file. This specifies the maximum view state size, in bytes. Here’s an example
that caps view state at 1 KB:






When you request a page that generates a view state larger than this, several hidden input fields will be created:





Remember, view state chunking is simply a mechanism for avoiding problems with certain
proxies (which is a relatively rare occurrence). View state chunking does not improve performance (and adds a small amount of extra serialization overhead). As a matter of good design, you should strive to include as little information in view state as possible, which ensures the best performance.

ASP..NET Page Processing

One of the key goals of ASP.NET is to create a model that lets web developers rapidly develop web forms in the same way that Windows developers can build made-to-measure windows in a desktop application. Of course, web applications are very different from traditional rich client applications.

There are two key stumbling blocks:

Web applications execute on the server: For example, suppose you create a form that allows the user to select a product record and update its information. The user performs these tasks in the browser, but in order for you to perform the required operations (such as updating the database), your code needs to run on the web server. ASP.NET handles this divide with a technique called postback, which sends the page (and all user-supplied information) to the server when certain actions are performed. Once ASP.NET receives the page, it can then fire the corresponding server-side events to notify your code.

Web applications are stateless: In other words, before the rendered HTML page is sent to the
user, your web-page objects are destroyed and all client-specific information is discarded. This model lends itself well to highly scalable, heavily trafficked applications, but it makes it difficult to create a seamless user experience. ASP.NET includes several tools to help you bridge this gap; most notable is a persistence mechanism called view state, which automatically embeds information about the page in a hidden field in the rendered HTML.
In the following sections, you’ll learn about both the postback and the view state features.

Together, these mechanisms help abstract the underlying HTML and HTTP details, allowing developers to work in terms of objects and events.

Core Assemblies for ASP.NET Pages Description

mscorlib.dll and System.dll Includes the core set of .NET data types, common exception
types, and numerous other fundamental building blocks.

System.Configuration.dll Includes classes for reading and writing configuration informationin the web.config file, including your custom settings.

System.Data.dll Includes the data container classes for ADO.NET, along with the SQL Server data provider.

System.Drawing.dll Includes classes representing colors, fonts, and shapes. Also includes the GDI+ drawing logic you need to build graphics on the fly.

System.Web.dll Includes the core ASP.NET classes, including classes for building web forms, managing state, handling security, and much more.

System.Web.Services.dll Includes classes for building web services—units of code that can be remotely invoked over HTTP.

System.Xml.dll Includes .NET classes for reading, writing, searching, transforming, and validating XML.

System.EnterpriseServices.dll Includes .NET classes for COM+ services such as transactions.

System.Web.Mobile.dll Includes .NET classes for the mobile web controls, which are
targeted for small devices such as web-enabled cell phones.

ASP.NET File Types Description

Ends with .aspx These are ASP.NET web pages (the .NET equivalent of the .asp file in
an ASP application). They contain the user interface and, optionally, the underlying application code. Users request or navigate directly to one of these pages to start your web application.

Ends with .ascx These are ASP.NET user controls. User controls are similar to web pages, except that they can’t be accessed directly. Instead, they must be hosted inside an ASP.NET web page. User controls allow you to develop an important piece of the user interface and reuse it in as many web forms as you want without repetitive code.

Ends with .asmx or .svc These are ASP.NET web services. Web services work differently than web pages, but they still share the same application resources, configuration settings, and memory. However, ASP.NET web services are gradually being phased out in favor of WCF (Windows Communication Foundation) services, which were introduced with .NET 3.0 and have the extension .svc. You’ll use web services with ASP.NET AJAX

web.config This is the XML-based configuration file for your ASP.NET application. It includes settings for customizing security, state management, memory management, and much more.

global.asax This is the global application file. You can use this file to define global variables and react to global events, such as when a web application first starts (see Chapter 5 for a detailed discussion). Visual Studio doesn’t create a global.asax file by default—you need to add it if it’s appropriate.

Ends with .cs These are code-behind files that contain C# code. They allow you to separate the application from the user interface of a web page.

NEW FEATURES IN VISUAL STUDIO 2008

The latest version of Visual Studio has some long-awaited improvements. They include the following:
• Web projects: Visual Studio 2005 replaced the traditional project-based web application model with a lighterweight
system of projectless development. However, this change didn’t please everyone, and so Microsoft released an add-on that brought the web project option back. In Visual Studio 2008, developers get the best of both worlds, and can choose to create projectless or project-based web applications depending on their needs.

• Multitargeting: Web servers won’t shift overnight from .NET 2.0 to .NET 3.5. With this in mind, Visual Studio now gives you the flexibility to develop applications that target any version of the .NET Framework, from version 2.0 on.

• CSS: In order to apply consistent formatting over an entire website, developers often use the Cascading Style Sheets (CSS) standard. Now Visual Studio makes it even easier to link web pages to style sheets, and pick and choose the styles you want to apply to various elements in your page without editing the markup by hand.

HTML CONTROLS VS. WEB CONTROLS

When ASP.NET was first created, two schools of thought existed. Some ASP.NET developers were most interested in server-side controls that matched the existing set of HTML Controls exactly. This approach allows you to create ASP.NET web-page interfaces in edicated HTML editors, and it provides a quick migration path for existing ASP pages. However, another set of ASP.NET developers saw the promise of something more—rich server-side controls that didn’t just emulate individual HTML tags. These controls might render their interface from dozens of distinct HTML elements while still providing a simple object-based interface to the programmer. Using this model, developers could work with programmable menus, calendars, data lists, validators, and so on.

After some deliberation, Microsoft decided to provide both models. You’ve already seen an example of HTML server controls, which map directly to the basic set of HTML tags. Along with these are ASP.NET web controls, which provide a higher level of abstraction and more functionality. In most cases, you’ll use HTML server-side controls for backward compatibility and quick migration, and use web controls for new projects.
ASP.NET web control tags always start with the prefix asp: followed by the class name. For example, the following
snippet creates a text box and a check box:


Again, you can interact with these controls in your code, as follows:
myASPText.Text = "New text";
myASPCheck.Text = "Check me!";
Notice that the Value property you saw with the HTML control has been replaced with a Text property. The Html InputText.Value property was named to match the underlying value attribute in the HTML tag. However, web controls don’t place the same emphasis on correlating with HTML syntax, so the more descriptive property
name Text is used instead.

The ASP.NET family of web controls includes complex rendered controls (such as the Calendar and TreeView), along with more streamlined controls (such as TextBox, Label, and Button), which map closely to existing HTML tags.

In the latter case, the HTML server-side control and the ASP.NET web control variants provide similar functionality, although the web controls tend to expose a more standardized, streamlined interface. This makes the web controls easy to learn, and it also means they’re a natural fit for Windows developers moving to the world of the Web, because many of the property names are similar to the corresponding Windows controls.

C# Contextual Keywords Descriptions

from Used in a LINQ query. A query expression must begin with a from clause.

get Defines an accessor method in a property or indexer. It retrieves the value of the property or indexer element.

group Used in a LINQ query and returns a sequence of IGrouping < (Of < (TKey,TElement > ) > ) objects that contain zero or more items that match the key value for the group.

into Used in a LINQ query and can be used to create a temporary identifier to store the results of a group , join , or select clause into a new identifier.

join Used in a LINQ query for associating elements from different sources.

let Used in a LINQ query to store the result of a subexpression to be used in a subsequent clause.

orderby Used in a LINQ query to sort the result of a query in either ascending or descending order.

C# Keyword Description

abstract A modifier that can be used with classes, methods, properties, indexers, andevents. Use it to indicate that a class is intended to be used as a base class of other classes, and abstract methods must be implemented by classes that derive from the abstract class.

as An operator that performs conversion between compatible reference types.

base Used to access members of a base class from a derived class.

bool A C# alias of the System.Boolean .NET Framework type. Its value can either true, false, or null.

break Used to transfer control out of a loop or switch statement.

byte Specifies a data type that can stores unsigned 8 - bit integer values from 0 to 255.
case Used together with the switch statement. It specifies the value to be matched so that control can be transferred to the case statement. catch Used with a try block to handle one or more exceptions.

char Specifies a data type that can store a 16 - bit Unicode character from U+0000 to U+ffff.

checked Used to explicitly enable overflow - checking integer operations.

class Used to declare classes.

const Used to specify a field or variable whose value cannot be modified.

continue Used within a loop such that control is transferred to the next iteration.

decimal Specifies a data type representing a 128 - bit data. It can approximately
represent a number from ± 1.0 – 28 to ± 7.9 1028.

default Used within a switch statement to indicate the default match if none of the other case statements is matched. Can also be used in generics to specify the default value of the type parameter.

delegate Used to declare a reference type variable that references a method name/
anonymous method.

do Executes a block of code repeatedly until a specified expression returns false.Used together with the while keyword to form a do - while statement.

double Specifies a data type that represents a 64 - bit floating point number. It can approximately represent a number from ± 5.0 10 – 324 to ± 1.7 10308.else Used with the if keyword to form an if - else statement. else defines theblock that will be executed if the expression specified in the if statement is evaluated to false.

enum Used to define an enumeration.

event Used to define an event within a class.

explicit Defines a cast operation that requires the programmer to explicitly select the
cast to be performed.

extern Declares a method that is implemented externally.false Used as either an operator or as a literal. One of the possible values in a boolvariable.

finally Used in a try - catch block to contain code that cleans up the code even if an exception occurs. Statements contained within a finally block are always executed.

fixed Prevents the garbage collector from relocating a movable variable.

float Specifies a data type that represents a 32 - bit floating point number. It can approximately represent a number from ± 1.5 10 – 45 to ± 3.4 1038. for Encloses a block of statements that will be executed repeatedly until a specified expression returns false.

foreach Used to iterate through a collection of items.

goto Used to transfer control of a program to a labeled statement. if Determines if a statement (or block of statements) is to be executed based on the result of a Boolean expression.

implicit Used to declare an implicit cast operation.

in Used in a foreach statement to specify the collection you want to iterate through.

int Specifies a data type that represents a signed 32 - bit integer number. It canrepresent a number from – 2,147,483,648 to 2,147,483,647.

interface Used to define an interface, which is a definition that contains the signatures of methods, delegates, and events. An interface does not contain any implementation.

internal An access modifier to indicate a member that can only be accessed within files in the same assembly.

is Used to check if an object is compatible with a given type.

lock Marks a statement block as a critical section so that other threads cannot execute the block while the statements within the block are being executed. long Specifies a data type that represents a signed 64 - bit integer number. It can represent a number from – 9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.

namespace Used to organize your code so that it belongs to a globally unique type. new Used to create objects and invoke a class ’ s constructor. Also can be used to explicitly hide a base class ’ s member in a derived class. When used in a generic declaration, it restricts types that might be used as arguments for a type declaration.

null Represents a null reference that does not refer to any object.

object A C# alias of the System.Object .NET Framework type.

operator Used to overload a built - in operator or provide a conversion operator.

out Indicates arguments that are to be passed by reference. It is similar to ref ,except that ref requires the variable to be initialized before it is passed.

override Extends or modifies the abstract or virtual implementation of an inherited
method, property, indexer, or event.

params Specifies a parameter array where the number of arguments is variable.

private An access modifier used to indicate a member that can only be accessed within the body of the class or struct in which it ’ s declared.

protected An access modifier used to indicate a member that can only be accessed within its class and derived classes.

public An access modifier used to indicate a member that can be accessed by all code.

readonly A modifier that indicates fields that can only be initialized at declaration or in a constructor.

ref Indicates arguments that are to be passed by reference.

return Terminates execution of a method and returns control to the calling method.

sbyte Specifies a data type that represents a signed 8 - bit integer number. It can represent a number from – 128 to 127.

sealed Specifies a class that does not allow other classes to derive from it.

short Specifies a data type that represents a signed 16 - bit integer number. It can represent a number from – 32,768 to 32767.

sizeof Used to obtain the size in bytes for a value type.

stackalloc Used in an unsafe code context to allocate a block of memory on the stack.

static A modifier to indicate that a member belongs to the type itself, and not to a specific object.

string Specifies a data type that represents a sequence of zero or more Unicode characters. Also an alias for the System.String .NET Framework type.

struct Denotes a value type that encapsulates a group of related variables.

switch A control statement that handles multiple selections by matching the value of the switch with a series of case statements.

this Refers to the current instance of the class. Also used as a modifier of the firstparameter of an extension method.

throw Used to invoke an exception during runtime.

true Used either as an operator or as a literal. One of the possible values in a bool variable.

try Indicates a block of code that may cause exceptions. Used with one or more catch blocks to handle the exceptions raised.

typeof Used to obtain the System.Type object for a type.

uint Specifies a data type that represents an unsigned 32 - bit integer number. It can represent a number from 0 to 4,294,967,295.

ulong Specifies a data type that represents an unsigned 64 - bit integer number. It can represent a number from 0 to 18,446,744,073,709,551,615.

unchecked Used to suppress overflow - checking for integral - type arithmetic operations and conversions.

unsafe Denotes an unsafe context, which is required for any operation involving pointers.

ushort Specifies a data type that represents an unsigned 16 - bit integer number. I can represent a number from 0 to 65,535.

using A directive for creating a namespace alias or importing namespace references.It is also used for defining a scope at the end of which an object will be disposed.

virtual An access modifier to indicate a method, property, indexer, or event declaration and allow for it to be overridden in a derived class.

volatile Indicates that a field might be modified by multiple threads that are executing at the same time.

void Specifies that a method does not return any value.

while Executes a statement or a block of statements until a specified expression evaluates to false.

Keywords C#

Keywords In any programming language, there is always a list of identifiers that have special meanings to the compiler. These identifiers are known as keywords, and you should not use them as identifiers in your program.

abstract event new struct as explicit null switch base extern object this bool false operator throw break finally out true byte fixed override try case float params typeof catch for private uint char foreach protected ulong checked goto public unchecked class if readonly unsafe const implicit ref ushort continue in return using decimal int sbyte virtual default interface sealed volatile delegate internal short void do is sizeof while double lock stackalloc else long staticenum namespace string

Using the C# Compiler (csc.exe)

Besides using Visual Studio 2008 to compile and run the application, you can build the application using Visual Studio 2008 and use the C# compiler ( csc.exe ) to manually compile and then run the application. This option is useful for large projects where you have a group of programmers working on different sections of the application.

Alternatively, if you prefer to code a C# program using a text editor, you can use the Notepad (Programs Accessories Notepad) application included in every Windows computer. (Be aware, however, that using Notepad does not give you access to the IntelliSense feature, which is available only in Visual Studio 2008.)

1. Using Notepad, create a text file, name it HelloWorld.cs , and save it into a folder on your hard disk, say in C:\C#.

2. Populate HelloWorld.cs with the following:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(“Hello, world! This is my first C# program!”);
Console.ReadLine();
return;
}
}
}

Use the command - line C# compiler ( csc.exe ) that ships with Visual Studio 2008 to compile the program. The easiest way to invoke csc.exe is to use the Visual Studio 2008 command prompt, which has all the path references added for you.

To launch the Visual Studio 2008 command prompt, select Start Programs Microsoft Visual
Studio 2008 Visual Studio Tools Visual Studio 2008 Command Prompt.

In the command prompt, change to the directory containing the C# program (C:\C# for this
example), and type the following command
C:\C# > csc HelloWorld.cs

Once the program is compiled, you will find the HelloWorld.exe executable in the same directory (C:\C#). Type the following to execute the application.
C:\C# > HelloWorld

To return to the command prompt, press Enter.

.NET Framework 3.5 builds upon version 2.0 and 3.0

The .NET Framework 3.5 builds upon version 2.0 and 3.0 of the .NET Framework, so it essentially contains the following components:

.NET Framework 2.0 and .NET Framework 2.0 Service Pack 1
.NET Framework 3.0 and .NET Framework 3.0 Service Pack 1
New features in .NET 3.5

Versions of the . NET Framework and Visual Studio

Microsoft officially released the .NET Framework in January 2002. Since then, the .NET Framework has gone through a few iterations, and at the time of writing it stands at version 3.5. While technically you can write .NET applications using a text editor and a compiler, it is always easier to write .NET applications using Visual Studio, the integrated development environment from Microsoft. With Visual Studio, you can use its built - in debugger and support for IntelliSense to effectively and efficiently build .NET applications. The latest version of Visual Studio is Visual Studio 2008.
The following table shows the various versions of the .NET Framework, their release dates, and theversions of Visual Studio that contain them.
Version Number -Release Date-Versions of Visual Studio
1.0 1.0.3705.0 2002 - 01 - 05 Visual Studio .NET 2002
1.1 1.1.4322.573 2003 - 04 - 01 Visual Studio .NET 2003
2.0 2.0.50727.42 2005 - 11 - 07 Visual Studio 2005
3.0 3.0.4506.30 2006 - 11 - 06 Shipped with Windows Vista
3.5 3.5.21022.8 2007 - 11 - 19 Visual Studio 2008

Starting with Visual Studio 2005, Microsoft dropped the .Net name from the Visual Studio.

Assemblies and the Microsoft Intermediate Language ( MSIL )

In .NET, an application compiled into MSIL bytecode is stored in an assembly. The assembly is contained in one or more PE (portable executable) files and may end with an EXE or DLL extension.

Some of the information contained in an assembly includes:
Manifest — Information about the assembly, such as identification, name, version, and so on.
Versioning — The version number of an assembly.
Metadata — Information that describes the types and methods of the assembly .

To get a better idea of a MSIL file and its content, take a look at the following example, which has two console applications — one written in C# and the other written in VB.NET.
The following C# code displays the “ Hello, World ” string in the console window:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace HelloWorldCS
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(“Hello, World!”);
Console.ReadLine();
}
}
}

Likewise, the following VB.NET code displays the “ Hello, World ” string in the console window:

Module Module1
Sub Main()
Console.WriteLine(“Hello, World!”)
Console.ReadLine()
End Sub
End Module

When both programs are compiled, the assembly for each program has an .exe extension. To view the content of each assembly, you can use the ildasm (MSIL Disassembler) tool.
Launch the ildasm tool from the Visual Studio 2008 Command Prompt window (Start Programs Microsoft Visual Studio 2008 Visual Studio Tools Visual Studio 2008 Command Prompt).

The following command uses the ildasm tool to view the assemblies for the C# and VB.NET programs:

C:\MSIL > ildasm HelloWorldCS.exe
C:\MSIL > ildasm HelloWorldVB.exe


. NET Framework Class Library

The .NET Framework class library contains classes that allow you to develop the following types of applications:

  • Console applications
  • Windows applications
  • Windows services
  • ASP.NET Web applications
  • Web Services
  • Windows Communication Foundation (WCF) applications
  • Windows Presentation Foundation (WPF) applications
  • Windows Workflow Foundation (WF) applications

Common Language Runtime

The Common Language Runtime (CLR) is the virtual machine in the .NET Framework. It sits on top of the Windows operating system (Windows XP, Windows Vista, Windows Server 2008, and so on). A .NET application is compiled into a bytecode format known as MSIL

(Microsoft Intermediate Language). During execution, the CLR JIT ( just - in - time)

compiles the bytecode into the processor ’ s native code and executes the application. Alternatively, MSIL code can be precompiled into native code so that JIT compiling is no longer needed; that speeds up the execution time of your application.

The CLR also provides the following services:
Memory management/garbage collection
Thread management
Exception handling
Security

.NET developers write applications using a .NET language such as C#, VB.NET, or C++. The MSIL bytecode allows .NET applications to be portable (at least theoretically) to other platforms because the application is compiled to native code only during runtime.

What’s the . NET Framework?

The .NET Framework has two components:
  1. Common Language Runtime
  2. .NET Framework class library

The Common Language Runtime (CLR) is the agent that manages your .NET applications at execution time. It provides core services such as memory, thread, and resource management.

Applications that run on top of the CLR are known as managed code ; all others are known as unmanaged code.

The .NET Framework class library is a comprehensive set of reusable classes that provides all the functionalities your application needs. This library enables you to develop applications ranging from desktop Windows applications to ASP.NET web applications, and Windows Mobile applications that run on Pocket PCs.