Tuesday, December 20, 2011

Application in asp.net

Server Side State Management
(Session,Application,Profile Property)

For Session: Click to View Example

For Profile Property: Click to View Example

Application:



ASP.NET allows you to save values using application state — which is an instance of the HttpApplicationState class — for each active Web application. Application state is a global storage mechanism that is accessible from all pages in the Web application. Thus, application state is useful for storing information that needs to be maintained between server round trips and between requests for pages. For more information, see ASP.NET Application State Overview.


Application state is stored in a key/value dictionary that is created during each request to a specific URL. You can add your application-specific information to this structure to store it between page requests.


ASP.NET provides three events that enable you to initialize Application variables (free resources when the application shuts down) and respond to Application errors:

a. Application_Start: Raised when the application starts. This is the perfect place to initialize Application variables.

b. Application_End: Raised when an application shuts down. Use this to free application resources and perform logging.

c. Application_Error: Raised when an unhandled error occurs. Use this to perform error logging.
 


Example

in .aspx page

 <form id="form1" runat="server">

    <div>

        <h2 style="color:Red">asp.net application state example: Lock and UnLock</h2>

        <asp:Label 
            ID="Label1" 
            runat="server" 
            Font-Size="Large" 
            ForeColor="DodgerBlue"
            >
        </asp:Label>

        <br /><br />

        <asp:Button 
            ID="Button1" 
            runat="server" 
            Text="Show Button Click Status"
            OnClick="Button1_Click"
            Font-Bold="true"
            ForeColor="DodgerBlue"
            />

    </div>

    </form>




in .cs page

 protected void Button1_Click(object sender, System.EventArgs e)

 {
        Application.Lock();
        
        int clickCounter = 0;

        if(Application["ClickCounter"] !=null)
        {

           
                clickCounter = (int)Application["ClickCounter"];

        }
        clickCounter++;

        Application["ClickCounter"] = clickCounter;

        Application.UnLock();


        Label1.Text = "Button Clicked: " + clickCounter.ToString() + " times";
    }

















No comments:

Post a Comment