asp.net p age o bjects. each asp.net page inherits the page object the page supplies 3 built in...

16
ASP.NET PAGE OBJECTS

Upload: brandon-wilcox

Post on 19-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ASP.NET P AGE O BJECTS.  Each ASP.NET page inherits the PAGE object  The PAGE supplies 3 built in objects:  REQUEST: All information passed to the

ASP.NET PAGE OBJECTS

Page 2: ASP.NET P AGE O BJECTS.  Each ASP.NET page inherits the PAGE object  The PAGE supplies 3 built in objects:  REQUEST: All information passed to the

Each ASP.NET page inherits the PAGE object

The PAGE supplies 3 built in objects: REQUEST: All information passed to the server from the browser.

Contains form and query data. Gets the HttpRequest object for the requested page.

RESPONSE: Writes HTML and other information back to the client browser. Gets the HttpResponse object associated with the Page. This object allows you to send HTTP response data to a client and contains information about that response.

SERVER: Provides server functionality for use in ASP. Gets the Server object, which is an instance of the HttpServerUtility class. Eg. using a database connection.

Global.asax and web.config are used for application control

Page 3: ASP.NET P AGE O BJECTS.  Each ASP.NET page inherits the PAGE object  The PAGE supplies 3 built in objects:  REQUEST: All information passed to the

Request Gets the HttpRequest object for the requested page.

Some properties:

ApplicationPath : Gets the ASP.NET application's virtual application root path on the server.

Browser : Gets information about the requesting client's browser capabilities.

ContentEncoding : Gets the character set of the entity-body.

Cookies : Gets a collection of cookies sent by the client.

FilePath : Gets the virtual path of the current request.

Files : Gets the collection of client-uploaded files (Multipart MIME format).

Form : Gets a collection of form variables.

Headers : Gets a collection of HTTP headers.

Page 4: ASP.NET P AGE O BJECTS.  Each ASP.NET page inherits the PAGE object  The PAGE supplies 3 built in objects:  REQUEST: All information passed to the

HttpMethod : Gets the HTTP data transfer method (such as GET, POST, or HEAD) used by the client.

IsAuthenticated : Gets a value indicating whether the user has been authenticated.

Params : Gets a combined collection of QueryString, Form, ServerVariables, and Cookies items.

Path : Gets the virtual path of the current request.

PhysicalPath : Gets the physical file system path corresponding to the requested URL.

QueryString : Gets the collection of HTTP query string variables.

RequestType : Gets or sets the HTTP data transfer method (GET or POST) used by the client.

ServerVariables : Gets a collection of Web server variables.

Url : Gets Information about the URL of the current request.

Page 5: ASP.NET P AGE O BJECTS.  Each ASP.NET page inherits the PAGE object  The PAGE supplies 3 built in objects:  REQUEST: All information passed to the

Request Eg. browsercheck

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Label1.Text = Request.Browser.Browser Label2.Text = Request.Browser.Version Label3.Text = Request.Browser.Platform Label4.Text = Request.Browser.CookiesEnd Sub

Page 6: ASP.NET P AGE O BJECTS.  Each ASP.NET page inherits the PAGE object  The PAGE supplies 3 built in objects:  REQUEST: All information passed to the

Response Cookies : Gets the response cookie collection. Write a cookie.

Expires : Gets or sets the number of minutes before a page cached on a browser expires. If the user returns to the same page before it expires, the cached version is displayed. Expires is provided for compatibility with previous versions of ASP.

Clear() : Clears all content output from the buffer stream.

End() : Sends all currently buffered output to the client, stops execution of the page, and raises the Application_EndRequest event.

Redirect() : Overloaded. Redirects a client to a new URL. No HTML is allowed to have already be sent to the browser

Page 7: ASP.NET P AGE O BJECTS.  Each ASP.NET page inherits the PAGE object  The PAGE supplies 3 built in objects:  REQUEST: All information passed to the

Redirect example

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Response.Redirect(“http://www.google.com”)End Sub

Page 8: ASP.NET P AGE O BJECTS.  Each ASP.NET page inherits the PAGE object  The PAGE supplies 3 built in objects:  REQUEST: All information passed to the

Server MachineName : Gets the server's computer name.

ScriptTimeout : Gets and sets the request time-out in seconds. Default = 90

HtmlDecode() : Decodes a string that has been encoded to eliminate invalid HTML characters.

HtmlEncode() : Encodes a string/special characters like < > to &lt; and &gt; to be displayed in a browser.

UrlDecode() : Decodes a string encoded for HTTP transmission and sent to the server in a URL.

UrlEncode() : Encodes a string for reliable HTTP transmission from the Web server to a client via the URL. Eg spaces are converted to + and special characters like & to %26

Transfer() : Server.Transfer is executed on the server. The page transferred to should be another Web Forms page (.aspx page) in the same application. You cannot use Server.Transfer to redirect to an .asp or .asmx page. Server.Transfer("WebForm2.aspx")

Page 9: ASP.NET P AGE O BJECTS.  Each ASP.NET page inherits the PAGE object  The PAGE supplies 3 built in objects:  REQUEST: All information passed to the

Server Example-birthday1

<%@ Page Language="VB" %><html> <body> <% Dim Name As string Dim Age As string Age = "twenty three??" Name = "<Sniff> & <Jack>" Age = Server.UrlEncode(Age) Name = Server.UrlEncode(Name) %> <a href="birthday2.aspx?age=<% Response.Write(Age) %>&name=<% Response.Write(Name)%>">click here</a> </body> </html>

Page 10: ASP.NET P AGE O BJECTS.  Each ASP.NET page inherits the PAGE object  The PAGE supplies 3 built in objects:  REQUEST: All information passed to the

Server Example-birthday2

<%@ Page Language="VB" %><html> <body> <center><% Dim Name As string Dim Age as string Name = Request.QueryString("Name") Age = Request.QueryString("Age") Name = Server.HtmlEncode(Name) Age = Server.HtmlEncode(Age) %><h1>Happy Birthday <% Response.Write(Name) %></h1> <h3>May the next <% Response.Write(Age) %> years be as good!</h3> </center> </body> </html>

Page 11: ASP.NET P AGE O BJECTS.  Each ASP.NET page inherits the PAGE object  The PAGE supplies 3 built in objects:  REQUEST: All information passed to the

Global.asax The Global.asax also known as the ASP.NET application file is located in the root directory of an ASP.NET application

This file contains code that is executed in response to application-level and session-level events raised by ASP.NET or by HTTP modules

You can also define objects with application-wide or session-wide scope in the Global.asax file

These events and objects declared in the Global.asax are applied to all resources in that web application

The Global.asax is an optional file. Use it only when there is a need for it

You can use this file to implement application security, as well as other tasks

The Global.asax file is configured so that any direct HTTP request(via. URL) is rejected automatically, so users cannot download or view its contents

Page 12: ASP.NET P AGE O BJECTS.  Each ASP.NET page inherits the PAGE object  The PAGE supplies 3 built in objects:  REQUEST: All information passed to the

The ASP.NET page framework recognizes automatically any changes that are made to the Global.asax file

The framework reboots the application, which includes closing all browser sessions, flushes all state information, and restarts the application domain

The Global.asax file, which is derived from the HttpApplication class, maintains a pool of HttpApplication objects, and assign them to applications as needed

There are two set of methods that fire corresponding to the events

The first set which gets invoked on each request and the second set which does not get invoked on each request

Methods corresponding to events that fire on each request : Application_BeginRequest() – fired when a request for the web

application comes in

Application_AuthenticateRequest() – fired just before the user credentials are authenticated. You can specify your own authentication logic over here

Page 13: ASP.NET P AGE O BJECTS.  Each ASP.NET page inherits the PAGE object  The PAGE supplies 3 built in objects:  REQUEST: All information passed to the

Application_AuthorizeRequest() – fired on successful authentication of user’s credentials. You can use this method to give authorization rights to user.

Application_ResolveRequestcache() – fired on successful completion of an authorization request

Application_AcquireRequestState() – fired just before the session state is retrieved for the current request

Application_PreRequestHandlerExecute() – fired before the page framework begins before executing an event handler to handle the request

Application_PostRequestHandlerExecute() – fired after HTTP handler has executed the request

Application_ReleaseRequestState() – fired before current state data kept in the session collection is serialized

Application_UpdateRequestCache() – fired before information is added to output cache of the page

Page 14: ASP.NET P AGE O BJECTS.  Each ASP.NET page inherits the PAGE object  The PAGE supplies 3 built in objects:  REQUEST: All information passed to the

Application_EndRequest() – fired at the end of each request

Methods corresponding to events that do not fire on each request :

Application_Start() – fired when the first resource is requested from the web server and the web application starts

Session_Start() – fired when session starts on each new user requesting a page

Application_Error() – fired when an error occurs

Session_End() – fired when the session of the user ends

Application_End() – fired when the web application ends

Application_Disposed() – fired when the web application is destroyed

Page 15: ASP.NET P AGE O BJECTS.  Each ASP.NET page inherits the PAGE object  The PAGE supplies 3 built in objects:  REQUEST: All information passed to the

Web.Config

The web.config is an XML file, it can consist of any valid XML tags, but the root element should always be <configuration>

Nested within this tag you can include various other tags to describe your settings

It simply holds keys and values recognized by ASP.NET

These values are easily modifiable and you can add your own custom key and values to control other settings

ASP.NET provides a hierarchical configuration system that is very extensible, so the settings you supply in each web.config file apply only to the directory that contains the file and those below it

To provide setting for your entire application, place this file in your root application folder

Like the global.asax file, ASP.NET also prevents the web.config file from being accessed via. a Web client

Page 16: ASP.NET P AGE O BJECTS.  Each ASP.NET page inherits the PAGE object  The PAGE supplies 3 built in objects:  REQUEST: All information passed to the

Changes to this file are automatically detected, and the application is automatically restarted for the new settings to take effect

No script blocks or HTML code, just pure XML

Inside the <configuration> tags are two different elements: configuration section handlers and configuration section settings

The first section declares the types of data in the web.config file, and the second contains the actual key/value pairs of settings

Using the web.config is an ideal method of creating a robust application that can quickly adapt to changes in its environment

web.config exposes an <appSettings> element that can be used as a place to store application settings like connection strings, file paths etc.