28
Introducing ASP.NET Chapter 1 Web applications have revolutionized the way business is conducted. These applications enable organizations to share and access information from anywhere and at anytime. This has majorly moved the focus of application development from desktop applications to Web applications. One of the most popular server-side technologies used today for developing Web applications is ASP.NET. This chapter introduces the basics of Web development. It also discusses the architecture, functioning, and features of ASP.NET. In addition, it explains how to develop ASP.NET applications. In this chapter, you will learn to: Identify the basics of Web development Explore ASP.NET Develop ASP.NET applications Objectives

ASP.net Chapte

Embed Size (px)

DESCRIPTION

bhhhhvvmvmvfmvhh

Citation preview

Page 1: ASP.net Chapte

Introducing ASP.NET Cha

pter

1

Web applications have revolutionized the way business is conducted. These applications enable organizations to share and access information from anywhere and at anytime. This has majorly moved the focus of application development from desktop applications to Web applications. One of the most popular server-side technologies used today for developing Web applications is ASP.NET.

This chapter introduces the basics of Web development. It also discusses the architecture, functioning, and features of ASP.NET. In addition, it explains how to develop ASP.NET applications.

In this chapter, you will learn to: Identify the basics of Web development Explore ASP.NET Develop ASP.NET applications

Objectives

Page 2: ASP.net Chapte
Page 3: ASP.net Chapte

NIIT Introducing ASP.NET 1.3

Among all technologies, the Internet has been the fastest growing technology. Ever since its inception, the Internet has evolved exponentially. In the recent years, it has changed the way business is conducted.

Prior to the evolution of the Internet, organizations were unable to create awareness about themselves because the communication media were limited. Moreover, the organizations could deliver only limited information by using the existing communication media such as television and radio. However, with the inception of the Internet, organizations found a new medium through which they could reach a larger range of people irrespective of their geographical locations. Therefore, organizations increasingly became dependent on the Internet for sharing and accessing information. This resulted in changing the focus of application development from desktop applications to Web applications.

Web applications are programs that are executed on a Web server and accessed from a Web browser. These applications enable organizations to share and access information on the Internet and corporate intranets. This information can be accessed from anywhere and at any time. In addition, Web applications can support online commercial transactions popularly known as e-commerce. An online store accessed through a Web browser is an example of a Web application.

A Web application consists of Web pages. Web pages can be of the following types: Static Web page: A Web page that consists of only Hyper Text Markup Language

(HTML) is a static Web page. A static Web page does not respond dynamically to the actions performed by a user.

Dynamic Web page: A Web page that responds dynamically to the actions performed by a user is a dynamic Web page and an application that consists of dynamic Web pages is a dynamic Web application. Such an application can be created by using server-side scripts, client-side scripts, or both in addition to HTML pages.

With the increase in the usage of the Internet as the information medium, organizations are creating dynamic Web applications. Dynamic Web applications enable a user to:

Retrieve data from a database that may not reside on the computer where the Web server is located.

Submit data from HTML forms. Get information based on the input.

Introducing Web Development

Server-Side Scripting

Page 4: ASP.net Chapte

1.4 Introducing ASP.NET NIIT

To incorporate these features in a Web application, you need to implement server-side scripting. Server-side scripting provides users with dynamic content that is based on the information stored at a remote location, such as a back-end database. Server-side scripting includes code written in server-side scripting languages, such as Active Server Pages (ASP) and Java Server Pages (JSP). A server-side script is executed on the Web server.

When a browser requests for information on a website that is created by using server-side technology, the Web server to which the request is sent, first processes the script and then sends the results back to the browser. For example, if a Web page includes a server-side script to display the current time of the system on which the website is hosted, the script will be processed at the server and the current system time will be sent back to the browser.

The following figure shows the working of server-side scripts.

Working of Server-Side Scripts

Client-side scripting enables you to develop Web pages that can dynamically respond to user input without having to interact with a Web server. Suppose, you have a Web application that requires users to enter the user name and password before displaying the home page. You need to ensure that a user does not leave the user name and password fields blank. To check whether a user has left the user name and password fields blank, you can write client-side scripts.

Client-Side Scripting

Page 5: ASP.net Chapte

NIIT Introducing ASP.NET 1.5

In addition to providing dynamic content, a client-side script helps reduce network traffic because it does not need to interact with a Web server to provide dynamic response to the user input. Client-side scripting also speeds up the response time of a Web application. This happens because a Web server is not overloaded with the job of processing the client-side script for every client. Scripting languages, such as VBScript and JavaScript, are used to write client-side scripts.

The following figure shows the working of client-side scripts.

Working of Client-Side Scripts

Limitations of Client-Side Scripting

In spite of the all the advantages, client-side scripting has some major limitations. These limitations are:

Browser support: Client-side scripting is not supported equally by all browsers and operating systems.

Isolation: Client-side scripts cannot access server-side resources. It is difficult and requires a lot of coding to access a file or interact with a database on the server by using client-side scripts.

Security: Client-side scripts are visible to the end users. Therefore, they can be tampered by malicious users.

Page 6: ASP.net Chapte

1.6 Introducing ASP.NET NIIT

Note

Thin clients: Web-enabled devices such as mobile phones, palmtop computers, and Personal Digital Assistants (PDAs) can communicate with Web servers. These devices act as thin clients that can use Web applications containing server-side scripting. However, these devices do not support client-side scripting such as JavaScript or VBScript.

In spite of all the limitations, client-side scripting is still in use. ASP.NET allows you to use client-side scripting in addition to the server-side scripting to create rich and responsive Web applications.

Page 7: ASP.net Chapte

NIIT Introducing ASP.NET 1.7

ASP.NET is a server-side technology that enables programmers to create dynamic Web applications. It has a number of advanced features such as simplicity, security, and scalability that help you develop robust Web applications. These advanced features of ASP.NET are based on the .NET Framework.

ASP.NET is built on the Microsoft .NET Framework. Microsoft introduced the .NET Framework to help developers create globally distributed software with Internet functionality and interoperability.

The following figure displays the elements of an ASP.NET application and how the elements fit in the broader context of the .NET Framework.

Elements of an ASP.NET Application in the .NET Framework

The preceding figure indicates that an ASP.NET application has the following elements: Web Form pages: Enable you to include user interfaces, such as Text box control,

List box control, and the application logic of Web applications.

Exploring ASP.NET

ASP.NET in the .NET Framework

Page 8: ASP.net Chapte

1.8 Introducing ASP.NET NIIT

Configuration files: Enable you to store the configuration settings of an ASP.NET application.

XML Web Service files: Define a Web service that can be called by an ASPX file over the Internet or from a local network.

Web forms and the state management feature of ASP.NET constitute the ASP.NET Page Framework. There are also a number of runtime services available in ASP.NET. The ASP.NET runtime services include view state, session state, and application state management, Web security, and the caching mechanism of ASP.NET applications. The runtime services of ASP.NET interact with .NET Framework base classes, which in turn, interact with the Common Language Runtime (CLR) to provide a robust Web-based development environment.

After creating an ASP.NET application, the ASP.NET files need to be stored on a Web server such as Internet Information Services (IIS) server, which is the Web server for the Windows platform. The Web server processes the ASP.NET files and then executes the scripts. Finally, the results are sent to the Web browser that requested the ASP.NET file.

The following figure depicts how a Web server processes a request for an ASP.NET file.

Processing of a Request for an ASP.NET File

Working of an ASP.NET Application

Page 9: ASP.net Chapte

NIIT Introducing ASP.NET 1.9

Note

The steps involved in the execution of an ASP.NET file are: 1. A Web browser sends a request for an ASP.NET file to a Web server by using a

Uniform Resource Locator (URL). 2. The Web server, such as IIS, receives the request and retrieves the appropriate

ASP.NET file from the disk or memory. 3. The Web server forwards the ASP.NET file to the ASP.NET script engine for

processing. 4. The ASP.NET script engine reads the file and executes any server-side script it

encounters. 5. The processed ASP.NET file is generated as an HTML page. 6. The Web server then sends the HTML page to the client. 7. The Web browser interprets the output and displays it.

A Web server generates and sends only the HTML output to the client. As a result, it helps to hide the code of the ASP.NET file from the users who access an ASP.NET Web page.

ASP.NET script engine is the engine that processes the server-side scripts contained in an ASP.NET application. After processing the scripts, the engine renders the result as an HTML document and sends it back to the browser.

In addition to hiding the code of the ASP.NET file from the users, ASP.NET has the following advanced features that help develop robust Web applications:

Compiled code: The code written in ASP.NET is compiled and not interpreted. This makes ASP.NET applications faster to execute than other server-side scripts, such as the scripts written in a previous version of ASP that are interpreted.

Power and flexibility: ASP.NET applications are based on CLR. As a result, the power and flexibility of the .NET platform is available to ASP.NET applications. ASP.NET applications enable you to ensure that the .NET Framework class library, messaging, and data access solutions are seamlessly accessible on the Web. ASP.NET is also language-independent. As a result, you can choose any .NET language to develop your application.

Features of ASP.NET

Page 10: ASP.net Chapte

1.10 Introducing ASP.NET NIIT

Simplicity: ASP.NET enables you to build user interfaces that separate application logic from the presentation content. In addition, CLR simplifies application development by using managed code services, such as automatic reference counting and garbage collection. Therefore, ASP.NET makes it easy to perform common tasks ranging from form submission and client authentication to website configuration and deployment.

Manageability: ASP.NET enables you to manage Web applications by storing the configuration information in an Extensible Markup Language (XML) file. You can open the XML file and modify it.

Scalability: ASP.NET has been designed with scalability in mind. It has features that help improve performance in a multiprocessor environment.

Security: ASP.NET provides a number of options for implementing security and restricting user access to a Web application. All these options are configured within the configuration file.

Extensibility: ASP.NET provides a number of options to create an extensible Web application. For example, you can create custom controls and extend the default Hypertext Transfer Protocol (HTTP) pipeline. The path through which the data flows from the client to the server is called the HTTP pipeline. It is a technique in which multiple HTTP requests are written out to a single socket without waiting for the corresponding responses. It improves the page loading time.

Page 11: ASP.net Chapte

NIIT Introducing ASP.NET 1.11

You can create ASP.NET applications by using any text editor. However, this will take a substantial amount of time because you will have to create the applications by creating all the Web page classes and coding the Web page controls manually. In addition to being time-consuming, this process increases the possibility of inducing errors that could otherwise be avoided by using a design tool. A design and productivity tool, such as Visual Studio, provides various features that enable the developers to quickly create robust ASP.NET applications. Some of the Visual Studio IDE features are:

Integrated error checking: Visual Studio can detect various problems such as data type conversion errors, missing namespaces or classes, and undefined variables. When you type code, Visual Studio detects and underlines these problems.

The Web form designer: Visual Studio provides the Web form designer that enables you to create a Web page by simply dragging the ASP.NET controls to the appropriate location and configuring their properties. The actual Web page markup is created automatically.

An integrated Web server: Web applications need to be hosted on a Web server such as IIS to be accessible for the users. Therefore, you need to install IIS on the development computer to test the applications. However, if you are creating a Web application by using Visual Studio, you need not install IIS on the computer. This is because Visual Studio has an integrated development Web server that enables you to run a Web application directly from the design environment.

Developer productivity enhancements: Visual Studio provides features such as automatic statement completion and color-coded syntax that makes coding quick and efficient. In addition, Visual Studio has an integrated debugger that allows you to watch code execution, pause your program at any point, and inspect the contents of any variable. This helps in debugging the code quickly and efficiently.

Complete extensibility: Visual Studio allows you to use macros, change project templates, and even add your own custom add-ins. This provides you to extend the existing functionality of the existing environment.

You can use ASP.NET to create websites and Web services. A website is the user interface of your application. Generally, a website represents a web application front-end, with screens, Cascading Style sheets (CSS), and User Interface (UI) components. A Web service enables you to remotely access certain functions at the server side. It enables the exchange of data in a client-to-server or a server-to-server environment over the Web by using the Simple Object Access Protocol (SOAP). A Web service does not have a user interface.

Developing ASP.NET Applications

Creating an ASP.NET Web Application

Page 12: ASP.net Chapte

1.12 Introducing ASP.NET NIIT

Task 1.1: Creating an ASP.NET Website

Task 1.2: Opening an Existing ASP.NET Website

ASP.NET applications are generally divided into multiple Web pages. Every Web page in an ASP.NET application shares a common set of resources and configuration settings. These resources and configuration settings cannot be shared by the Web pages from any other ASP.NET application, even if they are on the same Web server.

Every ASP.NET application is executed inside a separate application domain. These application domains ensure that even if a Web application causes a fatal error, it does not affect other applications that are currently running on the same computer. Application domains also restrict a Web page in one application from accessing the in-memory information of another application. Each Web application is a separate entity that has its own set of data. It can be described as a combination of files, pages, handlers, modules, and executable code that can be invoked from a virtual directory on a Web server.

Anatomy of an ASP.NET Application

Page 13: ASP.net Chapte

NIIT Introducing ASP.NET 1.13

Note

The following figure shows a Web server that is hosting four Web applications.

A Web Server Hosting Four Web Applications

A virtual directory is a directory that is exposed to the public on a Web server.

Page 14: ASP.net Chapte

1.14 Introducing ASP.NET NIIT

Directories Used in ASP.NET Web Applications

An ASP.NET application uses some specialized directories. The following table lists these directories with their descriptions.

Directory Description

Bin Stores all the compiled .NET components (DLLs) that an ASP.NET Web application uses. For example, if you create a custom component, you need to place the component in this directory. Once placed in the Bin directory, the component can be used by any ASP.NET application.

App_Code Stores source code files that are dynamically compiled to be used in the Web application. It is similar to the Bin directory except for one difference. It contains source code files whereas the Bin directory contains compiled assemblies.

App_Browsers Stores browser definition files. Browser definition files are XML files that define a browser with its properties, capabilities, and features. This information is used by ASP.NET server controls to determine the browser that has requested the page and render the appropriate markup for that browser.

App_GlobalResources Stores global resources that are accessible to every page in the Web application. It is used for localization purposes where you want a website to display content in more than one language.

App_LocalResources Stores .resx files that are accessible to a specific page only.

App_WebReferences Stores references to Web services that are used by the Web application.

App_Data Stores data such as database files and XML files that are used by the application. It is not mandatory to store data files in this directory.

App_Themes Stores the themes that are used in the Web application.

Directories Used in an ASP.NET Application

Some of the subdirectories given in the preceding table are created in specific applications. For example, the App_Themes directory is created when you use themes in your application.

Page 15: ASP.net Chapte

NIIT Introducing ASP.NET 1.15

Files Used in ASP.NET Web Applications

The ASP.NET Web applications can include different types of files. The following table lists some of the important file types with their descriptions.

File Name Description

Ends with .aspx These are ASP.NET Web pages that contain the user interface and, optionally, the underlying application code. Users request or navigate directly to one of these pages to start a Web application.

Ends with .ascx These are ASP.NET user controls. These controls are similar to Web pages, except that the user cannot access these files directly. Instead, they must be hosted inside an ASP.NET Web page. User controls allow you to develop a small piece of user interface and reuse it in as many Web forms as you want without repeating code.

Ends with .asmx These are ASP.NET Web services. Web services are collections of methods that can be called over the Internet. Web services work differently than Web pages. However, they still share the same application resources, configuration settings, and memory.

web.config This is the XML-based configuration file for ASP.NET applications. It includes settings for customizing features such as security, state management, and memory management.

Global.asax This is the global application file. You can use this file to define global variables (variables that can be accessed from any Web page in the Web application). It is mostly used for defining the overall application events related to the Application & Session object.

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

Ends with .ashx These are default HTTP handlers for all Web handlers that do not have a user interface and that include the @ WebHandler directive.

Files Used in an ASP.NET Application

Configuration Files Used in ASP.NET Web Applications

ASP.NET uses a hierarchy of configuration files to keep application configuration settings separate from the application code. A configuration file is an XML file that contains configuration settings for an application and has a .config extension.

Page 16: ASP.net Chapte

1.16 Introducing ASP.NET NIIT

It provides the following benefits: Provides control and flexibility over the way you run applications. Eliminates the need to recompile the application every time a setting changes. Controls access to protected resources, and the location of remote applications and

objects by defining configuration settings.

To understand the benefits of configuration files, consider an example. Suppose, your Web application includes a number of forms that are required to connect to an SQL Server database. You have written the code to create the connection on each of these forms. However, if the location of the database is modified, you will have to update the code for connecting to the database in each form. A better option is to store the details of the connection string in a configuration file for the Web application and refer to the connection string from within the code. As a result of this, even if the database location is changed, only the connection string in the configuration file needs to be updated.

ASP.NET allows you to easily edit the configuration data before, or after an application is deployed on the server. The files in the configuration hierarchy allow you to make configuration settings at different levels.

The following table lists the levels, the corresponding configuration files, and their description.

Level File Name Description

Server Settings Machine.config Present at the root of the configuration hierarchy. It defines global configuration settings for all .NET Framework applications.

Root Web Settings Web.config Present in the same directory as machine.config. It defines configuration settings for all ASP.NET applications.

Website settings (optional)

Web.config Present in the root directory of each IIS website. It contains settings that are specific to the website.

Application root Settings (optional)

Web.config Present in the root directory of each application. It contains settings that are specific to the application.

Application subfolder (optional)

Web.config Present in a subfolder of the application root. It contains settings for specific section of a Web application.

Configuration Files with their Levels and Descriptions

Page 17: ASP.net Chapte

NIIT Introducing ASP.NET 1.17

The two files included in the hierarchy of configuration files are machine.config and web.config. The machine.config file always resides at the root of the configuration hierarchy and contains the global and default settings for all .NET Framework applications on the server. The web.config file can reside at multiple levels in the hierarchy. In case two versions of the same configuration setting exist at different hierarchal levels, local settings will take precedence over global settings.

A Web page is made up of the following two components: The visual portion: Refers to the static HTML and ASP.NET server controls that

define the layout of a Web page. The programming logic: Refers to the code that is required to interact with the Web

page.

ASP.NET provides two models for managing the visual elements and code: Single-file page model Code-behind page model

The Single-File Page Model

In the single-file page model, HTML markup of the page and its programming code are in the same physical .aspx file. The programming code is contained in a <script> block that specifies the attribute runat="server".

The first line in the .aspx file contains the @Page directive. Every .aspx file should include the @Page directive. The @Page directive provides ASP.NET with the basic information about how to compile the Web page. It also indicates the language that will be used for coding and the way to connect the event handlers. The syntax of the page directive is:

<%@ Page Language="C#"%>

At run time, the compiler converts a single-file page into a class that derives from the System.Web.UI.Page class.

The single-file page model provides the following advantages: In pages with less code, keeping the programming code and the HTML markup in a

single file proves to be very convenient while studying the file. Pages written by using the single-file model are slightly easier to deploy or to send to

another programmer because there is only one file. A single-file page is easier to rename because there is no dependency between files.

ASP.NET Web Page Code Model

Page 18: ASP.net Chapte

1.18 Introducing ASP.NET NIIT

Managing files in a source code control system is slightly easier because the page is self-contained in a single file.

The Code-Behind Page Model

In the code-behind page model, HTML markup is kept in one file (with extension .aspx) and the programming code in another file (with extension .cs or .vb). The code file contains a partial class, which indicates that the class contains only some of the total code that makes up the full class for the page. The partial class inherits from a base Page class (either System.Web.UI.Page or a class derived from System.Web.UI.Page).

There are two differences in the .aspx page between the single-file and the code-behind models:

In the code-behind model, there is no <script> block with the runat="server" attribute.

In the code-behind model, the @Page directive contains attributes that reference an external file and a class, as shown in the following example: <%@ Page Language="C#" CodeFile="Default.aspx.cs" Inherits="_Default" %>

In the preceding example, the CodeFile attribute specifies the code-behind file with which the page is associated, and the Inherits attribute specifies a code-behind class for the page to inherit. This class can be any class that is derived from the Page class. This attribute is used with the CodeFile attribute, which contains the path to the source file for the code-behind class.

When the page is compiled, ASP.NET creates a new partial class for the .aspx file. This class is a peer of the code-behind partial class file and contains the declarations for the controls of the page. Finally, ASP.NET generates a class that inherits from the partial class created from the .aspx file and the code-behind partial class. The generated class is compiled into an assembly that runs in order to render output to the browser.

The code-behind model provides the following advantages: It allows a developer to separate the UI display from the UI processing, thereby

enabling the programmer to reuse the programming logic. It enables a developer to quickly debug the code because the code is not clubbed

with the UI. It enables developers to quickly create a Web application by separating the design

and development functions. For example, designers can work on the .aspx file and at the same time programmers can implement logics in the code-behind file.

Task 1.3: Adding a New Web Form to an ASP.NET Website

Page 19: ASP.net Chapte

NIIT Introducing ASP.NET 1.19

All Web forms are instances of the ASP.NET Page class, which is defined in the System.Web.UI namespace. The Page class inherits the TemplateControl class, which, in turn, inherits the Control class. As a result, the Page class provides useful events that can be used in the code.

Page Events

ASP.NET uses an event-driven model of programming. This model of ASP.NET defines a sequence of events that are raised during the lifecycle of a Web page. The following table lists some of the page events.

Event Description

Init It is the first event that occurs when the server executes an ASP.NET page. It occurs only once in the entire lifecycle of an ASP.NET page.

Load It is fired after the Init event, when the page is requested for the first time and whenever the page is reloaded. This event can be used to initialize the variables and the state of the controls that are used in the page.

Unload It represents the last event that occurs in the lifecycle of an ASP.NET page. This event is used to perform final cleanup work, such as the closing of open database connections, discarding objects, or closing files.

Page Events and Their Description

ASP.NET has a number of built-in objects that are programmed using methods, properties, and collections. These objects are made available to the Page object, which is an instance of the System.Web.UI.Page class from which all ASP.NET pages are inherited.

ASP.NET provides the following built-in objects: Application

Request

The Page Class

Intrinsic Objects of ASP.NET

Page 20: ASP.net Chapte

1.20 Introducing ASP.NET NIIT

Response

Server

Session

HttpContext

Application

The Application object provides a reference to an object of the HttpApplicationState class. The Application object is used to store information that is defined for the entire Web application. For example, the connection string, which is used to connect to the database server, is defined for the entire Web application. Therefore, the connection string can be stored in the Application object. The following table describes some of the properties of the Application object.

Property Description

Count Returns the number of objects in the HttpApplicationState collection.

Item Provides access to an object in the HttpApplicationState collection.

Application Object’s Properties with Description

The Application object provides various methods and properties. The following table describes some of the methods of the Application object.

Method Description

public void Lock() Blocks the access to an Application variable or object. The Lock() method enables access synchronization when more than one process is accessing a particular variable or object.

public void UnLock() Releases the lock procured on an Application variable or object.

Application Object’s Methods with Description

Page 21: ASP.net Chapte

NIIT Introducing ASP.NET 1.21

Request

The Request object provides a reference to an object of the HttpRequest class. The object enables ASP.NET applications to access information sent by the client during a Web request. The following table describes some of the properties of the Request object.

Property Description

ApplicationPath Returns the application root path of ASP.NET on the server. This is a read-only property.

Url Returns a URI object, which will contain the complete address of the requested page. This is a read-only property.

UserHostAddress Returns the IP host address of the requesting client.

UserLanguage Returns a sorted string array containing the language preferences of the client.

Browser Returns the HttpBrowserCapabilities object, which contains information about the browser capability of the client.

Cookies Returns the HttpCookieCollection object, which provides access to the cookie variables of the client.

QueryString Provides access to the parameters passed from the client to the server along with a GET request.

Request Object’s Properties with Description

The following table describes an important method of the Request object.

Method Description

byte[] BinaryRead(int) Helps retrieve the data sent to the server from the client as part of a post request and stores it in an array.

Request Object’s Method with Description

Page 22: ASP.net Chapte

1.22 Introducing ASP.NET NIIT

Response

The Response object provides a reference to an object of the HttpResponse class. The object enables ASP.NET applications to send information to the client. The following table describes some of the properties of the Response object.

Property Description

Buffer Sets the buffer feature of the Response object. When the Buffer property is set to true, the Response object buffers the responses together. When the Buffer property is set to false, the Response object sends the response as it receives. The default value is true.

Cache Provides access to the caching policy, such as the expiration time and privacy settings of the Web page. This is a read-only property.

ContentType Sets or gets the type of data that is sent to the client through the Response object.

Cookies Sets a cookie to the client browser.

IsClientConnected Returns a Boolean value indicating whether the client is connected or disconnected.

Response Object’s Properties with Description

The following table describes some of the methods of the Response object.

Method Description

public void Clear() Clears the content in the buffer of the Response object.

public void Close() Closes the connection to the client.

public void End() Sends the buffered data to the client and closes the connection to the client. The End() method will raise an Application_EndRequest event.

public void Flush() Sends the data in the buffer to the client but does not close the connection to the client.

Page 23: ASP.net Chapte

NIIT Introducing ASP.NET 1.23

Method Description

public void Redirect(string) Redirects the new URL specified in the string parameter to the client.

public void Write(string) Writes a string on the form. It is an overloaded method.

Response Object’s Methods with Description

Task 1.4: Displaying a Message on a Web Page

Server

The Server object provides a reference to an object of the HttpServerUtility class. The Server object provides methods that can be used to access the methods and properties of the Web server. The following table describes some of the properties of the Server object.

Property Description

MachineName Returns the name of the server machine.

ScriptTimeout Helps get and set the request timeout in seconds for the Web server.

Server Object’s Properties with Description

The following table describes some of the methods of the Server object.

Method Description

public void ClearError() Clears the last error that has occurred on the server.

public object CreateObject(String) Creates the server instance of a COM object.

public Exception GetLastError() Returns an Exception object, which represents the last error that occurred on the server.

Page 24: ASP.net Chapte

1.24 Introducing ASP.NET NIIT

Method Description

public string MapPath(string path) Returns the physical file path on the Web server that corresponds to the specified virtual path.

public void Transfer(string) Terminates the processing of the current page and loads the page specified in the string format.

Server Object’s Methods with Description

Session

The Session object provides a reference to an object of the HttpSessionState class. The Session object enables ASP.NET applications to keep the state of the application as modified or changed by a client. It also provides access to the session wide cache, which can be used to store information pertaining to the client.

A session starts when a client logs on to a website and ends when he/she logs off from the website. The session is also terminated if the client remains inactive for a specific period. The default timeout period is 20 minutes. The following table describes some of the properties of the Session object.

Property Description

Count Returns the number of items in the session-state collection.

SessionID Returns the unique session ID assigned for a session.

Timeout Enables to get and set the period in minutes that is allowed between requests by the client before the session-state provider terminates the session.

Session Object’s Properties with Description

Page 25: ASP.net Chapte

NIIT Introducing ASP.NET 1.25

The following table describes some of the methods of the Session object.

Method Description

public void Add(string name, object value)

Adds a new item to the session-state collection.

public void Clear() Clears all the values stored in the session-state collection.

public void Remove(string name) Deletes an item from the session-state collection.

Session Object’s Methods with Description

HttpContext

The HttpContext object holds information about the current HTTP request. A new HttpContext object is created for every request sent to an ASP.NET application. The new object is created at the beginning of a request and destroyed when the request is completed. The HttpContext object holds current request-specific information such as Request, Response, Server, and Session. The following table describes some of the properties of the HttpContext object.

Property Description

AllErrors Gets an array of errors that are accumulated while processing an HTTP request.

ApplicationInstance Gets or sets the HttpApplication object for the current HTTP request.

Timestamp Gets the initial timestamp of the current HTTP request.

Users Gets or sets security information for the current HTTP request.

HTTPContext Object’s Properties with Description

Page 26: ASP.net Chapte

1.26 Introducing ASP.NET NIIT

The following table describes some of the methods of the HttpContext object.

Method Description

public void AddError(Exception errorInfo)

Adds an exception to the exception collection for the current HTTP request.

public void ClearError() Clears all errors for the current HTTP request.

public object GetConfig(string name)

Returns requested configuration information for the current HTTP request.

HttpContext Object’s Methods with Description

Page 27: ASP.net Chapte

NIIT Introducing ASP.NET 1.27

1. Differentiate between a static and a dynamic Web page.

2. Which of the following directories stores global resources that are accessible to a specific page only? a. App_Code b. App_GlobalResources c. App_LocalResources d. App_WebReferences

3. Identify the extension of a user control. a. .aspx b. .ascx c. .asmx d. .asax

4. Which of the following properties of the Request object returns the application root path of ASP.NET on the server? a. ApplicationPath b. Url c. UserHostAddress d. UserLanguage

5. Which of the following methods of the Response object is used to send the data in the buffer to the client but does not close the connection to the client? a. End() b. Flush() c. Clear() d. Close()

Practice Questions

Page 28: ASP.net Chapte

1.28 Introducing ASP.NET NIIT

In this chapter, you learned that: Web pages can be of the following types:

Static Web page Dynamic Web page

Server-side scripting provides users with dynamic content that is based on the information stored at a remote location.

Client-side scripting enables you to develop Web pages that can dynamically respond to user input without having to interact with a Web server.

An ASP.NET application has the following elements: Web Form pages Configuration files XML Web service files

ASP.NET has the following advanced features that help develop robust Web applications:

Compiled code Enriched tool support Power and flexibility Simplicity Manageability Scalability Security Extensibility

A Web form is made up of the following two components: The visual portion The programming logic

ASP.NET provides two models for managing the visual elements and code: Single-file page model Code-behind page model

ASP.NET provides the following built-in objects: Application

Request

Response

Server

Session

HttpContext

Summary