building applications using asp.net and c# / session 5 / 1 of 19 session 5
TRANSCRIPT
Building Applications using ASP.NET and C# / Session 5 / 1 of 19
Session 5Session 5
Building Applications using ASP.NET and C# / Session 5 / 2 of 19
Session ObjectivesSession Objectives
Discuss the Global.asax file Explain events in Global.asax file Use the Application
object Use the Server object
Building Applications using ASP.NET and C# / Session 5 / 3 of 19
Global.asaxGlobal.asax
Stored in Root Directory of application
Defines boundary of application
Initialise application or session level variables
Connect to databases
Send cookies
Building Applications using ASP.NET and C# / Session 5 / 4 of 19
Events in Events in Global.asax - 1Global.asax - 1Events Fired When
Application_OnStart
Fired when the first ASP.NET page in the current application directory (or its sub-directories) is called.
Application_OnEnd
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_OnBe
ginRequestFired every time a page request begins (ideally when a page is loaded or refreshed).
Application_OnEndRequest
Fired every time a page request ends (that is every time the page executes on the browser)
Session_OnStart Fired every time a new session begins.
Session_OnEnd Fired when the session ends. For the various ways in which a session can end refer to the session on the Session object.
Building Applications using ASP.NET and C# / Session 5 / 5 of 19
Events in Events in GlobalGlobal..asax - asax -
22
Building Applications using ASP.NET and C# / Session 5 / 6 of 19
Application Application Object - 1Object - 1
Represents an instance of an ASP.NET application.
Object[varName]
Application ["greeting"] = "Welcome to my World";
Building Applications using ASP.NET and C# / Session 5 / 7 of 19
Application Application Object - 2Object - 2
<script language="C#" runat="server"> void Application_OnStart(Object sender, EventArgs E) { Application ["greeting"] = "Welcome to my World";}</script>
<HTML><script Language ="C#" runat ="server" Debug = "true">void Page_Load(Object Src, EventArgs E){ Response.Write(Application ["greeting"]);}</script> <form runat= "server" ></form></HTML>
Building Applications using ASP.NET and C# / Session 5 / 8 of 19
Application Application Object - 3Object - 3
<script language="C#" runat="server"> void Application_OnStart(Object sender, EventArgs E) {
Application ["Counter"] = 0; }</script>
<html><script Language ="C#" runat ="server" Debug = "true">void Page_Load(Object Src, EventArgs E){Application["Counter"] = (Int32) Application ["Counter"] + 1;Response.Write("You are visitor number :" + Application ["Counter"]);}</script> <form runat= "server" ><MARQUEE BEHAVIOR="scroll" Scrolldelay = 25><FONT SIZE = 5 COLOR = RED>Welcome to my World</FONT></MARQUEE></form></html>
Building Applications using ASP.NET and C# / Session 5 / 9 of 19
Application Application Object - 4Object - 4
Building Applications using ASP.NET and C# / Session 5 / 10 of 19
Controlling Controlling AccessAccess
<HTML><script Language ="C#" runat ="server" Debug = "true">void Page_Load(Object Src, EventArgs E){Application.Lock();Application["Counter"] = (Int32) Application ["Counter"] + 1;Application.UnLock();}</script><BODY><br>This page has been visited <%Response.Write(Application ["Counter"]);%> times!!</BODY></HTML>
Building Applications using ASP.NET and C# / Session 5 / 11 of 19
ArraysArrays<script language="C#" runat="server"> void Application_Start(Object sender, EventArgs E) { String [] job = new String [4]; job[0]= "Faculty"; job[1]= "Programmer"; job[2]= "Salesman"; job[3]= "Manager"; Application ["j"] = job; } </script>
<<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>
Building Applications using ASP.NET and C# / Session 5 / 12 of 19
Server ObjectServer Object
Execute and Transfer HTMLEncode URLEncode MapPath
Property Description
ScriptTimeout
Is used to specify the period for which a script can run on the server before it is terminated. Machine
NameIs used to get the machine name of the server.
Server.property | method
Building Applications using ASP.NET and C# / Session 5 / 13 of 19
Execute Execute MethodMethod
<%@ Page Debug ="true"%><html><script language="C#" runat="server">void clicked (Object Src, EventArgs E){Server.Execute("/test/ses6ex1.aspx");}</script><form runat ="server"><asp:button id = "btn1" onclick = "clicked" Text =" Click me to transfer execution" runat = "server" /></form></html>
Building Applications using ASP.NET and C# / Session 5 / 14 of 19
Transfer Transfer MethodMethod
Server.Transfer("/test/transfer.aspx");
Building Applications using ASP.NET and C# / Session 5 / 15 of 19
HTMLEncode HTMLEncode MethodMethod
Response.Write( Server.HtmlEncode("<H1> is an example of a Heading tag</H1>")); Response.Write( Server.HtmlEncode("<H1> is an example of a Heading tag</H1>"));
Server.HTMLEncode (string)
Response.Write("<br><H1> is an example of a Heading tag</H1>"); Response.Write("<br><H1> is an example of a Heading tag</H1>");
Building Applications using ASP.NET and C# / Session 5 / 16 of 19
URLEncode URLEncode Method - 1Method - 1
Response.Write(Server.UrlEncode("http://localhost/code/map.aspx"));Response.Write(Server.UrlEncode("http://localhost/code/map.aspx"));
Server.URLEncode (string)
Building Applications using ASP.NET and C# / Session 5 / 17 of 19
URLEncode URLEncode Method - 2Method - 2
<html><script language="C#" runat="server">void clicked (Object Src, EventArgs E){String name = Server.UrlEncode("John Saunders");Response.Redirect ("http://localhost/code/calendar.aspx?name=" + name);}</script><form runat ="server"><asp:button id = "btn1" onclick = "clicked" Text =" Click me to send values" runat = "server" /></form></html>
<html><script language="C#" runat="server">void clicked (Object Src, EventArgs E){String name = Server.UrlEncode("John Saunders");Response.Redirect ("http://localhost/code/calendar.aspx?name=" + name);}</script><form runat ="server"><asp:button id = "btn1" onclick = "clicked" Text =" Click me to send values" runat = "server" /></form></html>
Building Applications using ASP.NET and C# / Session 5 / 18 of 19
URLEncode URLEncode Method - 3Method - 3
<html><script language="C#" runat="server">void clicked (Object Src, EventArgs E){String name = Server.UrlEncode("John Saunders");String password = Server.UrlEncode("king");Response.Redirect("http://localhost/code/error.aspx?name=" + name + " &password=" + password);}</script><form runat ="server"><asp:button id = "btn1" onclick = "clicked" Text =" Click me to send values" runat = "server" /></form></html>
<html><script language="C#" runat="server">void clicked (Object Src, EventArgs E){String name = Server.UrlEncode("John Saunders");String password = Server.UrlEncode("king");Response.Redirect("http://localhost/code/error.aspx?name=" + name + " &password=" + password);}</script><form runat ="server"><asp:button id = "btn1" onclick = "clicked" Text =" Click me to send values" runat = "server" /></form></html>
Building Applications using ASP.NET and C# / Session 5 / 19 of 19
MapPath MapPath MethodMethod
Response.Write( Server.MapPath("/encode.aspx")); Response.Write( Server.MapPath("/encode.aspx"));
Response.Write( Server.MapPath("encode.aspx")); Response.Write( Server.MapPath("encode.aspx"));
Response.Write(Server.MapPath(Request.ServerVariables.Get("PATH_INFO"))); Response.Write(Server.MapPath(Request.ServerVariables.Get("PATH_INFO")));
Server.MapPath (path)