Thursday, 26 April 2012

What is an application object in ASP.NET | where we use application object in asp.net | application object sample example using asp.net | What is an application object in .NET

Hi Friends, in this article I will explain about the application object in .NET.
Application Object:
The Application object is used to store and access variables from any page in an application.
The Application object holds information that will be used by many pages in the application.
.NET provides a class called HttpApplication, which is part of the System.Web namespace.
 This class represents the Application object in ASP.NET.

An instance of this class is already created for you and this is called "Application".
 You can access various methods and properties of the class HttpApplication using the object "Application".
First of all create one website.

File—New—Website---enter website name—OK
Then website is created.
After that add one Global Application file.

Right Click on website—Add New Item—Select Global Application—Global.asax—OK

Then Global.asax file is created.
This file is like shown  below.

<%@ Application Language="VB" %>

<script runat="server">

    Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
        ' Code that runs on application startup
         End Sub
   
    Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
        ' Code that runs on application shutdown
    End Sub
       
    Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
        ' Code that runs when an unhandled error occurs
    End Sub

    Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
        ' Code that runs when a new session is started
    End Sub

    Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
        ' Code that runs when a session ends.
        ' Note: The Session_End event is raised only when the sessionstate mode
        ' is set to InProc in the Web.config file. If session mode is set to StateServer
        ' or SQLServer, the event is not raised.
    End Sub
      
</script>

Write the below code in between application_start function.
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
        ' Code that runs on application startup
        Application("username") = "Kishore P"
    End Sub

In the default.aspx page copy and paste the below code.
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    </div>
    </form>
</body>
</html>


In the Default.aspx.vb write the following  code.

Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Label1.Text = "Username :" + Application("username") + "<br>"

    End Sub
End Class


Then execute the Default.aspx Page
The output is like
Username :  Kishore P
"If you like my blog or articles, you can appreciate by leaving your comments or Liking my Facebook pageAspdotnet-kishore, following on Google+ Aspdotnet-Kishore, Twitter  on AspdotnetKishore, Linked in Aspdotnet-Kishore, stumbling my posts on stumble upon and subscribing on  RSSfeed Aspdotnet-Kishore for free updates directly to your Email inbox . Watch my blog  for more articles." 

No comments:

Post a Comment

© 2012-2018 Aspdotnet-Kishore.blogspot.com. All Rights Reserved.
The content is copyrighted to Kishore and may not be reproduced on other websites without permission from the owner.