Tải bản đầy đủ (.ppt) (54 trang)

Asp.net slide5

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (1.62 MB, 54 trang )

Session 9
Application, Server
and Session Objects
Exploring ASP.NET / Session 9 / 2 of 54
Review

To make HTML elements programmatically accessible, it is necessary to
indicate that an HTML element be parsed and treated as a server
control. This can be done by adding a runat="server" attribute to the
HTML element.

The process of checking whether the user has filled up a form in the
right format, and has not left any fields blank, is called validation.

The validation controls available are as follows:

RequiredFieldValidator: Helps in ensuring that a value is entered for a field

CompareValidator: Checks if the value of a control is similar to the value of
another control

RangeValidator: Checks if the value entered in a control is in the specified
range of values

RegularExpressionValidator: Checks if the value entered fits the regular
expression that is specified

CustomValidator: The value entered is checked by a client-side or server-
side function written by the programmer

ValidationSummary: A list of all the validation errors occurring in all the


controls is created, and can be displayed on the page.
Exploring ASP.NET / Session 9 / 3 of 54
Review Contd…

The Page object has a property called IsValid, that returns true if all the
validation tests are successful, and vice-versa.

To disable client-side validation, the ClientTarget property can be set to
downlevel.

Code Behind is a feature that enables the developer to write the code to
provide the required functionality in a separate file than that of the code
to create the graphics for the web page
Exploring ASP.NET / Session 9 / 4 of 54
Objectives

Discuss the Global.asax file

Explain the events in the Global.asax file

Use the Application Object

Use the Server Object

Use the Session Object
Exploring ASP.NET / Session 9 / 5 of 54
Global.asax File
Stored in Root Directory of application
Defines boundary of application
Initialises application or session level variables

Connects to databases
Sends cookies
Exploring ASP.NET / Session 9 / 6 of 54
Events in Global.asax
Events Description
Application_Start Fired when the first ASP.NET page in the
current application directory (or its sub-
directories) is called.
Application_End
Fired when the last session of the application
ends. Also fired when the web application is
stopped, using the Internet Services Manger
snap-in.
Application_Begin
Request
Fired every time a page request begins (ideally
when a page is loaded or refreshed).
Exploring ASP.NET / Session 9 / 7 of 54
Events in Global.asax Contd…
Events Description
Application_EndRe
quest
Fired every time a page request ends (that is
every time the page executes on the browser)
Session_Start Fired every time a new session begins.
Session_End Fired when the session ends. For the various
ways in which a session can end, refer to the
session on the Session object.
Exploring ASP.NET / Session 9 / 8 of 54
Global.asax Example

Global.asax
<script language="C#" runat="server">
protected void Application_Start(Object sender,
EventArgs e)
{
}

protected void Session_Start(Object sender,
EventArgs e)
{
Response.Write( "Session Started <br>");
}

Exploring ASP.NET / Session 9 / 9 of 54
Global.asax Example
protected void Application_BeginRequest(Object
sender, EventArgs e)
{
Response.Write("<h1>Application
Begins</h1>");
Response.Write ("Application request begins
<br>");
}
protected void Application_EndRequest(Object
sender, EventArgs e)
{
Response.Write ("Application request ends
<br>");
}
Exploring ASP.NET / Session 9 / 10 of 54

Global.asax Example
protected void Session_End(Object sender, EventArgs
e)
{
Response.Write("Session ended");
}
protected void Application_End (Object sender,
EventArgs e)
{
}
</script>
Exploring ASP.NET / Session 9 / 11 of 54
Test Global.asax
<html>
<title>Testing Global</title>
<script language="C#" runat="server">
void Page_Load(Object sender, EventArgs e)
{
Response.Write ("Page Load event <br>");
}
</script>
</html>
Exploring ASP.NET / Session 9 / 12 of 54
Test Global.asax Output
Exploring ASP.NET / Session 9 / 13 of 54
Application Object
Represents an instance of an ASP.NET application.
Object[varName]
Application ["greeting"] = "Welcome to our sites";
Application Level

Session Level
Exploring ASP.NET / Session 9 / 14 of 54
Application Object Example 1
void Application_Start(Object sender, EventArgs E)
{
Application ["sessioncount"] = 0;
}
<HTML>
<script Language ="C#" runat ="server" Debug = "true">
void Page_Load(Object Src, EventArgs E)
{
Response.Write (“Your visitor number is “ +
Application ["sessioncount"]);
}
</script>
<form runat= "server" >
</form>
</HTML>
Exploring ASP.NET / Session 9 / 15 of 54
Application Example Output
Exploring ASP.NET / Session 9 / 16 of 54
<html>
<script Language ="C#" runat ="server" Debug = "true">
void Page_Load(Object Src, EventArgs E)
{
Response.Write ("Your visitor number is :" + Application
["sessioncount"]);
}
</script>
<form runat= "server" > </form>

</html>
Application Object Example 2
void Session_Start(Object sender, EventArgs e)
{
Application["sessioncount"]=(Int32)Application["sessionco
unt"] + 1;
}
Exploring ASP.NET / Session 9 / 17 of 54
Application Object Output
Output after reopening
the browser
Output after
refreshing
Output Initially
Exploring ASP.NET / Session 9 / 18 of 54
Controlling Access

Application variables can be accessed and updated
by all the pages of an ASP.NET application

Since the variable retains the currently assigned
value, if a user changes the value of a variable, the
value is changed for all the users of the application

To ensure that application-level variables are not
updated by more than one user simultaneously, the
Application object makes use of the Lock() and
UnLock() methods.
Exploring ASP.NET / Session 9 / 19 of 54
Controlling Access Contd…

Application.Lock();
//…code to change the value of the application
variables
……………
…………
Application.UnLock();
The Lock method locks all the variables in a script.
Ensures that only the current user has control
over the page.
When UnLock method is invoked, the current user
loses control of the application
Exploring ASP.NET / Session 9 / 20 of 54
Arrays
String [] job = new String [4];
job[0]= "Faculty";
job[1]= "Programmer";
job[2]= "Salesman";
job[3]= "Manager";
Application ["j"] = job;

Application-level arrays can be used to share common groups of
information across the entire application.

Typically, application-level arrays are used for information that is
static

While using arrays in the Application object, the elements stored in
the array should not be altered directly
Exploring ASP.NET / Session 9 / 21 of 54
Array.aspx

<HTML>
<script Language ="C#" runat ="server" >
void Page_Load(Object Src, EventArgs E)
{ int i = 0; String[] k;
k = (String[])Application["j"];
for (i = 0; i<k.Length;i++)
{
Response.Write(k[i] + "<br>");
}
}
</script>
</HTML>
Exploring ASP.NET / Session 9 / 22 of 54
Server Object
Execute and Transfer
HTMLEncode
URLEncode
MapPath
Property Description
ScriptTimeo
ut
Is used to specify the period for which a script can
run on the server before it is terminated.
MachineName Is used to get the machine name of the server.
Server.property | method
Allows the web server to be controlled, and acts as an
interface to HTTP service
Exploring ASP.NET / Session 9 / 23 of 54
Execute Method
<%@ Page Debug ="true"%><html>

<script language="C#" runat="server">
void clicked (Object Src, EventArgs E)
{Server.Execute (“Array.aspx");}
</script>
<form runat ="server">
<asp:button id = "btnClick" onclick = "clicked"
Text =" Click me to transfer execution" runat =
"server" />
</form>
</html>
The Execute() method is used to transfer execution from the current
page to another page, and return the execution back to the current
page
Exploring ASP.NET / Session 9 / 24 of 54
Execute Method Output
Exploring ASP.NET / Session 9 / 25 of 54
Transfer Method
Server.Transfer (“Array.aspx");
The Transfer() method is used to transfer the execution completely to
the specified page.
Unlike the Execute() method, the control is lost from the calling page
when this method is executed.
Syntax:

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×