Docsity

ASP.NET TUTORIAL FOR PROGRAMMERS, Essays (university) of Computer Science

Partial preview of the text

Download ASP.NET TUTORIAL FOR PROGRAMMERS and more Essays (university) Computer Science in PDF only on Docsity!

i

About the Tutorial

ASP.NET is a web application framework developed and marketed by Microsoft to allow programmers to build dynamic web sites. It allows you to use a full-featured programming language such as C# or VB.NET to build web applications easily.

This tutorial covers all the basic elements of ASP.NET that a beginner would require to get started.

Audience

This tutorial is prepared for the beginners to help them understand basic ASP.NET programming. After completing this tutorial, you will find yourself at a moderate level of expertise in ASP.NET programming from where you can take yourself to next levels.

Prerequisites

Before proceeding with this tutorial, you should have a basic understanding of .NET programming language. As we are going to develop web-based applications using ASP.NET web application framework, it will be good if you have an understanding of other web technologies such as HTML, CSS, AJAX, etc.

Disclaimer & Copyright

 Copyright 2014 by Tutorials Point (I) Pvt. Ltd.

All the content and graphics published in this e-book are the property of Tutorials Point (I) Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republish any contents or a part of contents of this e-book in any manner without written consent of the publisher. We strive to update the contents of our website and tutorials as timely and as precisely as possible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt. Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of our website or its contents including this tutorial. If you discover any errors on our website or in this tutorial, please notify us at [email protected].

iii

    1. EVENT HANDLING..............................................................................................................
    • Event Arguments
    • Application and Session Events
    • Page and Control Events........................................................................................................................
    • Event Handling Using Controls
    • Default Events
    1. SERVER SIDE
    • Server Object
    • Request Object
    • Response Object
    1. SERVER CONTROLS
    • Properties of the Server Controls
    • Methods of the Server Controls
    1. HTML SERVER
    • Advantages of using HTML Server Controls
    1. CLIENT SIDE
    • Client Side Scripts
    • Client Side Source Code
    1. BASIC CONTROLS
    • Button Controls
    • Text Boxes and Labels
    • Check Boxes and Radio Buttons
    • List Controls
    • The ListItemCollection Object
    • Radio Button list and Check Box List
    • Bulleted lists and Numbered Lists
    • HyperLink Control iv
    • Image Control
    1. DIRECTIVES
    • The Application Directive
    • The Assembly Directive
    • The Control Directive
    • The Implements Directive
    • The Import Directive
    • The Master Directive
    • The MasterType Directive
    • The OutputCache Directive
    • The Page Directive
    • The PreviousPageType Directive
    • The Reference Directive
    • The Register Directive
    1. MANAGING STATE
    • View State
    • Control State
    • Session State
    • Application State
    1. VALIDATORS
    • BaseValidator Class
    • RequiredFieldValidator Control
    • RangeValidator Control
    • CompareValidator Control
    • RegularExpressionValidator
    • CustomValidator
    • ValidationSummary............................................................................................................................... v
    • Validation Groups
    1. DATABASE ACCESS.............................................................................................................
    • Retrieving and Displaying Data
    1. ADO.NET..........................................................................................................................
    • The DataSet Class
    • The DataTable Class
    • The DataRow Class
    • The DataAdapter Object......................................................................................................................
    • The DataReader Object
    • DbCommand and DbConnection Objects
    1. FILE UPLOADING..............................................................................................................
    1. AD ROTATORS
    • The Advertisement File
    • Properties and Events of the AdRotator Class
    • Working with AdRotator Control.........................................................................................................
    1. CALENDARS
    • Properties and Events of the Calendar Control
    • Working with the Calendar Control
    1. MULTI VIEWS...................................................................................................................
    • Properties of View and MultiView Controls
    1. PANEL CONTROLS............................................................................................................
    • Working with the Panel Control
    1. AJAX CONTROLS
    • The ScriptManager Control
    • The UpdatePanel Control
    • The UpdateProgress Control vi
    • The Timer Control
    1. DATA SOURCES................................................................................................................
    • Data Source Views
    • The SqlDataSource Control..................................................................................................................
    • The ObjectDataSource Control
    • The AccessDataSource Control
    1. DATA BINDING.................................................................................................................
    • Simple Data Binding
    • Declarative Data Binding
    1. CUSTOM CONTROLS
    • User Controls
    • Custom Controls
    • Working with Custom Controls
    1. PERSONALIZATION
    • Understanding Profiles
    • Attributes for the Element
    • Anonymous Personalization
    1. ERROR HANDLING
    • Tracing
    • Error Handling
    1. DEBUGGING
    • Breakpoints
    • The Debug Windows
    1. LINQ
    • LINQ Operators
    1. SECURITY vii
    • Forms-Based Authentication
    • IIS Authentication: SSL
    1. DATA CACHING................................................................................................................
    • What is Caching?
    • Caching in ASP.NET
    • Output Caching
    • Data Caching
    • Object Caching
    1. WEB SERVICES
    • Creating a Web Service
    • Consuming the Web Service
    • Creating the Proxy...............................................................................................................................
    1. MULTITHREADING
    • Creating Thread...................................................................................................................................
    • Thread Life Cycle
    • Thread Priority
    • Thread : Properties and Methods
    1. CONFIGURATION
    • Configuration Section Handler declarations
    • Application Settings
    • Connection Strings
    • System.Web Element
    1. DEPLOYMENT
    • XCOPY Deployment
    • Copying a Website

What is ASP.NET?

ASP.NET is a web development platform, which provides a programming model, a comprehensive software infrastructure and various services required to build up robust web applications for PC as well as mobile devices.

ASP.NET works on top of the HTTP protocol, and uses the HTTP commands and policies to set a browser-to-server bilateral communication and cooperation.

ASP.NET is a part of Microsoft .Net platform. ASP.NET applications are compiled codes, written using the extensible and reusable components or objects present in .Net framework. These codes can use the entire hierarchy of classes in .Net framework.

ASP.NET application codes can be written in any of the following languages:

 C#  Visual Basic.Net  Jscript  J#

ASP.NET is used to produce interactive, data-driven web applications over the internet. It consists of a large number of controls such as text boxes, buttons, and labels for assembling, configuring, and manipulating code to create HTML pages.

ASP.NET Web Forms Model

ASP.NET web forms extend the event-driven model of interaction to the web applications. The browser submits a web form to the web server and the server returns a full markup page or HTML page in response.

All client side user activities are forwarded to the server for stateful processing. The server processes the output of the client actions and triggers the reactions.

Now, HTTP is a stateless protocol. ASP.NET framework helps in storing the information regarding the state of the application, which consists of:

 Page state  Session state

The page state is the state of the client, i.e., the content of various input fields in the web form. The session state is the collective information obtained from various pages the user visited and worked with, i.e., the overall session state. To clear the concept, let us take an example of a shopping cart:

1. INTRODUCTION

User adds items to a shopping cart. Items are selected from a page, say the items page, and the total collected items and price are shown on a different page, say the cart page. Only HTTP cannot keep track of all the information coming from various pages. ASP.NET session state and server side infrastructure keeps track of the information collected globally over a session.

ASP.NET runtime carries the page state to and from the server across page requests while generating ASP.NET runtime codes, and incorporates the state of the server side components in hidden fields.

This way, the server becomes aware of the overall application state and operates in a two-tiered connected way.

The ASP.NET Component Model

The ASP.NET component model provides various building blocks of ASP.NET pages. Basically it is an object model, which describes:

 Server side counterparts of almost all HTML elements or tags such as

and .  Server controls, which help in developing complex user-interface. For example, the Calendar control or the Gridview control.

ASP.NET is a technology, which works on the .Net framework that contains all web- related functionalities. The .Net framework is made of an object-oriented hierarchy. An ASP.NET web application is made of pages. When a user requests an ASP.NET page, the IIS delegates the processing of the page to the ASP.NET runtime system.

The ASP.NET runtime transforms the .aspx page into an instance of a class, which inherits from the base class page of the .Net framework. Therefore, each ASP.NET page is an object and all its components i.e., the server-side controls are also objects.

Components of .Net Framework 3.

Before going to the next session on Visual Studio.Net, let us go through the various components of the .Net framework 3.5. The following table describes the components of the .Net framework 3.5 and the job they perform:

Components and their Description

(1) Common Language Runtime or CLR

It performs memory management, exception handling, debugging, security checking, thread execution, code execution, code safety, verification, and compilation. The code that is directly managed by the CLR is called the managed code. When the managed code is compiled, the compiler converts the source code

( 10 )Windows Presentation Foundation

It provides a separation between the user interface and the business logic. It helps in developing visually stunning interfaces using documents, media, two and three dimensional graphics, animations, and more.

( 11 ) Windows Communication Foundation (WCF)

It is the technology used for building and executing connected systems.

( 12 ) Windows CardSpace

It provides safety for accessing resources and sharing personal information on the internet.

( 13 ) LINQ

It imparts data querying capabilities to .Net languages using a syntax which is similar to the tradition query language SQL.

ASP.NET provides an abstraction layer on top of HTTP on which the web applications are built. It provides high-level entities such as classes and components within an object-oriented paradigm.

The key development tool for building ASP.NET applications and front ends is Visual Studio. In this tutorial, we work with Visual Studio 2008.

Visual Studio is an integrated development environment for writing, compiling, and debugging the code. It provides a complete set of development tools for building ASP.NET web applications, web services, desktop applications, and mobile applications.

The Visual Studio IDE

The new project window allows choosing an application template from the available templates.

When you start a new web site, ASP.NET provides the starting folders and files for the site, including two files for the first web form of the site.

The file named Default.aspx contains the HTML and asp code that defines the form, and the file named Default.aspx.cs (for C# coding) or the file named Default.aspx.vb

2. ENVIRONMENT SETUP

 To add an ASP.NET folder, right-click on the project in the Solution Explorer and select the folder from the list.

 To add an existing item to the site, right-click on the project or folder under which you are going to add the item in the Solution Explorer and select from the dialog box.

Projects and Solutions

A typical ASP.NET application consists of many items: the web content files (.aspx), source files (.cs files), assemblies (.dll and .exe files), data source files (.mdb files), references, icons, user controls and miscellaneous other files and folders. All these files that make up the website are contained in a Solution.

When a new website is created, VB2008 automatically creates the solution and displays it in the solution explorer.

Solutions may contain one or more projects. A project contains content files, source files, and other files like data sources and image files. Generally, the contents of a project are compiled into an assembly as an executable file (.exe) or a dynamic link library (.dll) file.

Typically a project contains the following content files:

 Page file (.aspx)  User control (.ascx)  Web service (.asmx)  Master page (.master)  Site map (.sitemap)  Website configuration file (.config)

Building and Running a Project

You can execute an application by:

 Selecting Start  Selecting Start Without Debugging from the Debug menu,  pressing F  Ctrl-F

The program is built meaning, the .exe or the .dll files are generated by selecting a command from the Build menu.

ASP.NET life cycle specifies how:

 ASP.NET processes pages to produce dynamic output  The application and its pages are instantiated and processed  ASP.NET compiles the pages dynamically

ASP.NET life cycle could be divided into two groups:

 Application Life Cycle  Page Life Cycle

ASP.NET Application Life Cycle

The application life cycle has the following stages:

  1. User makes a request for accessing application resource, a page. Browser sends this request to the web server.
  2. A unified pipeline receives the first request and the following events take place: i. An object of the class ApplicationManager is created. ii. An object of the class HostingEnvironment is created to provide information regarding the resources. iii. Top level items in the application are compiled.
  3. Response objects are created. The application objects such as HttpContext, HttpRequest and HttpResponse are created and initialized.
  4. An instance of the HttpApplication object is created and assigned to the request.
  5. The request is processed by the HttpApplication class. Different events are raised by this class for processing the request.

ASP.NET Page Life Cycle

When a page is requested, it is loaded into the server memory, processed, and sent to the browser. Then it is unloaded from the memory. At each of these steps, methods and events are available, which could be overridden according to the need of the application. In other words, you can write your own code to override the default code.

The Page class creates a hierarchical tree of all the controls on the page. All the components on the page, except the directives, are part of this control tree. You can see the control tree by adding trace= "true" to the page directive. We will cover page directives and tracing under 'directives' and ‘event handling'.

The page life cycle phases are:

3. LIFE CYCLE

Unload

The rendered page is sent to the client and page properties, such as Response and Request, are unloaded and all cleanup done.

ASP.NET Page Life Cycle Events

At each stage of the page life cycle, the page raises some events, which could be coded. An event handler is basically a function or subroutine, bound to the event, using declarative attributes such as Onclick or handle.

Following are the page life cycle events:

PreInit

PreInit is the first event in page life cycle. It checks the IsPostBack property and determines whether the page is a postback. It sets the themes and master pages, creates dynamic controls, and gets and sets profile property values. This event can be handled by overloading the OnPreInit method or creating a Page_PreInit handler.

Init

Init event initializes the control property and the control tree is built. This event can be handled by overloading the OnInit method or creating a Page_Init handler.

InitComplete

InitComplete event allows tracking of view state. All the controls turn on view-state tracking.

LoadViewState

LoadViewState event allows loading view state information into the controls.

LoadPostData

During this phase, the contents of all the input fields are defined with the tag are processed.

PreLoad

PreLoad occurs before the post back data is loaded in the controls. This event can be handled by overloading the OnPreLoad method or creating a Page_PreLoad handler.

Load

The Load event is raised for the page first and then recursively for all child controls. The controls in the control tree are created. This event can be handled by overloading the OnLoad method or creating a Page_Load handler.

LoadComplete

The loading process is completed, control event handlers are run, and page validation takes place. This event can be handled by overloading the OnLoadComplete method or creating a Page_LoadComplete handler.

PreRender

The PreRender event occurs just before the output is rendered. By handling this event, pages and controls can perform any updates before the output is rendered.

PreRenderComplete

As the PreRender event is recursively fired for all child controls, this event ensures the completion of the pre-rendering phase.

SaveStateComplete

State of control on the page is saved. Personalization, control state and view state information is saved. The HTML markup is generated. This stage can be handled by overriding the Render method or creating a Page_Render handler.

UnLoad

The UnLoad phase is the last phase of the page life cycle. It raises the UnLoad event for all controls recursively and lastly for the page itself. Final cleanup is done and all resources and references, such as database connections, are freed. This event can be handled by modifying the OnUnLoad method or creating a Page_UnLoad handler.