190 likes | 328 Views
Session 5. The Application. and Server Objects. Session Objectives. Discuss the Global.asax file. Explain events in Global.asax file. Use the Application object. Use the Server object. Global.asax. Stored in Root Directory of application. Defines boundary of application.
E N D
Session 5 The Application and Server Objects Building Applications using ASP.NET and C# / Session 5 / 1 of 19
Session 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 / 2 of 19
Global.asax Stored in Root Directory of application Defines boundary of application Initialise application or session level variables Global.asax Connect to databases Send cookies Building Applications using ASP.NET and C# / Session 5 / 3 of 19
Events 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_OnBeginRequest Fired 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. Events in Global.asax - 1 Building Applications using ASP.NET and C# / Session 5 / 4 of 19
Events in Global.asax - 2 Refresh Building Applications using ASP.NET and C# / Session 5 / 5 of 19
Application Object - 1 Represents an instance of an ASP.NET application. Variables Page-level Object-level Object[varName] Syntax Application ["greeting"] = "Welcome to my World"; Building Applications using ASP.NET and C# / Session 5 / 6 of 19
Application Object - 2 <script language="C#" runat="server"> void Application_OnStart(Object sender, EventArgs E) { Application ["greeting"] = "Welcome to my World"; }</script> Global.asax <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> sess5ex3.aspx Building Applications using ASP.NET and C# / Session 5 / 7 of 19
Application Object - 3 <script language="C#" runat="server"> void Application_OnStart(Object sender, EventArgs E) { Application ["Counter"] = 0; }</script> Global.asax <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> sess5ex4.aspx Building Applications using ASP.NET and C# / Session 5 / 8 of 19
Application Object - 4 Building Applications using ASP.NET and C# / Session 5 / 9 of 19
Controlling Access <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 / 10 of 19
Arrays <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> Global.asax <<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> sess5ex6.aspx Building Applications using ASP.NET and C# / Session 5 / 11 of 19
Property Description ScriptTimeout 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 Object Syntax Server.property | method Properties • Execute and Transfer • HTMLEncode Methods • URLEncode • MapPath Building Applications using ASP.NET and C# / Session 5 / 12 of 19
Execute Method <%@ 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 / 13 of 19
Transfer Method Server.Transfer("/test/transfer.aspx"); Building Applications using ASP.NET and C# / Session 5 / 14 of 19
HTMLEncode Method Server.HTMLEncode (string) Syntax Response.Write( Server.HtmlEncode("<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 / 15 of 19
URLEncode Method - 1 Server.URLEncode (string) Syntax Response.Write(Server.UrlEncode("http://localhost/code/map.aspx")); http%3a%2f%2flocalhost%2fcode%2fmap.aspx Building Applications using ASP.NET and C# / Session 5 / 16 of 19
URLEncode Method - 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> Building Applications using ASP.NET and C# / Session 5 / 17 of 19
URLEncode Method - 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> Building Applications using ASP.NET and C# / Session 5 / 18 of 19
MapPath Method Server.MapPath (path) Syntax Response.Write( Server.MapPath("/encode.aspx")); c:\inetpub\wwwroot\encode.aspx Response.Write( Server.MapPath("encode.aspx")); C:\Tested Code\encode.aspx Response.Write(Server.MapPath(Request.ServerVariables.Get("PATH_INFO"))); C:\Tested Code\map.aspx Building Applications using ASP.NET and C# / Session 5 / 19 of 19