Wednesday, 19 March 2014

ASP.Net Page Life Cycle Sample Code\Example using ASP.Net, C#


ASP.Net Page Life Cycle Sample Code\Example using ASP.Net, C#

Description:
Add an asp.net page in your project
Rename it “ASPPage"

OR

Copy and Paste #region ASP.NET PAGE LIFE CYCLE EVENTS in your page from sample given in post


Output:

1 Pre-Init 
2 Init 
3 Init Completed 
4 Load 
5 Load Completed 
6 Pre Render 
7 Render


Codes:

1)   Filename: ASPPage.aspx


<%@ Page Title="Asp.Net Page Life Cycle" Language="C#"  AutoEventWireup="true"
    CodeBehind="ASPPage.aspx.cs" Inherits="Asp.Net_Page_Life_Cycle_Sample_Code.ASPPage" %>
<h2>
Hi
</h2>
<p>
Put content here.
</p>
<div>
<asp:Button ID="btnEnter" runat="server" Text="Page Button"
    onclick="btnEnter_Click" />
</div>


2) Filename: ASPPage.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Asp.Net_Page_Life_Cycle_Sample_Code
{
    public partial class ASPPage : System.Web.UI.Page
    {
        int index = 0;

        #region ASP.NET PAGE LIFE CYCLE EVENTS

        protected void Page_PreInit(object sender, EventArgs e)
        {
            Response.Write("" + (++index).ToString() + "  Pre-Init <br />");
        }
        protected override void OnInit(EventArgs e)
        {
            Response.Write("" + (++index).ToString() + "  Init <br />");

        }
        protected void Page_InitComplete(object sender, EventArgs e)
        {
            Response.Write("" + (++index).ToString() + "  Init Completed <br />");

        }
        protected void Page_Load(object sender, EventArgs e)
        {
            ViewState["test"] = "Test";
            Response.Write("" + (++index).ToString() + "  Load <br />");

        }
        protected override void OnPreRender(EventArgs e)
        {
            Response.Write("" + (++index).ToString() + "  Pre Render <br />");

        }
        protected override void Render(HtmlTextWriter writer)
        {
            Response.Write("" + (++index).ToString() + "  Render <br />");

        }
        protected override void OnUnload(EventArgs e)
        {
            ++index;
            //  Response.Write(" Unload"); Response object is not available during Dispose event

        }
        public override void Dispose()
        {
            ++index;
           //  Response.Write(" Dispose"); Response object is not available during Dispose event
        }

        protected override object SaveViewState()
        {
            Response.Write("" + (++index).ToString() + "  Save View State <br />");
            return base.SaveViewState();
        }

        public void RaisePostBackEvent(string eventArgument)
        {
            Response.Write("" + (++index).ToString() + "  Raise Post Back Event <br />");

        }

        public void RaisePostDataChangedEvent()
        {
            Response.Write("" + (++index).ToString() + "  Raise Post Data Change Event <br />");

        }
        protected override void LoadViewState(object o)
        {
            Response.Write("" + (++index).ToString() + "  Load View State <br />");
            base.LoadViewState(o);

        }
        private void Page_LoadComplete(object sender, System.EventArgs e)
        {
            Response.Write("" + (++index).ToString() + "  Load Completed <br />");
        }
        // On Button Click Event
        protected void btnEnter_Click(object sender, EventArgs e)
        {
            Response.Write("" + (++index).ToString() + "  Button Click <br />");
        }
        #endregion
    }
}


No comments:

Post a Comment