When an asp.net page runs, the page performs a number of steps such as Initialization, Instantiating controls, Maintaining & Restoring state, Event Handler, Rendering, which is called page life.
Rendering: -It is a process of creating a visual representation on a browser.
There are some various steps of the page life cycle in asp.net which are given below:-
Real-Life Example:Rendering: -It is a process of creating a visual representation on a browser.
There are some various steps of the page life cycle in asp.net which are given below:-
- PreInit
- Init
- InitComplete
- PreLoad
- Load
- Control Events
- LoadComplete
- PreRender
- SaveStateComplete
- Render
- Unload
Note:- Page life cycle is the sequence of steps that are followed by the all visual studio Compilers.
Before Understanding the concepts of the Page Life cycle, First we have to understand the concepts of the Software life cycle in Software Engineering.
we may be aware of the software life cycle, which means what are the steps involved to build a Software. We can't break any steps to develop good software. Nowadays all the software Industries follow these steps for developing and software (big, small) very carefully. If we escape any steps then the software may not be bug/error. Similarly, the asp.net compiler follows each step (Events) when any page display on the browser. There are some steps of the Software life cycle as given below:-
- Planning
- Designing
- Coding (Implementation)
- Testing
- Documentation
- Deployment
- Maintenance
1.) PreInit:- PreInit is the first stage of asp.net page event life cycle. In this stage following events can be fired on the asp.net page.
- Check the IsPostBack property to determine whether This page is processing the first time.
- Create & Recreate Dynamic controls
- Set A-Master page in the code dynamically
- Set the theme & Skin property on the page
- If the request is a Post back, the value of controls has not been stored from view state.
- View State:- View state stores page-specific information, when a page post back to the server.
Example of PreInit:-
protected void Page_PreInit(object sender, EventArgs e)
protected void Page_PreInit(object sender, EventArgs e)
{
// Text Box Control Dynamically
TextBox txtvalue = new TextBox();
txtvalue .ID = "mytext";
txtvalue .EnableTheming = true;
txtvalue .Text = "This am TextBox";
//When you create below event handler press Tab 2 times from your keyboard
txtvalue .TextChanged += new EventHandler(txt_TextChanged);
this.form1.Controls.Add(txtvalue );
txtvalue .Text = "Change Text&press Submit";
//Page.MasterPageFile = "~/MasterPage.master";
//Checking whether value is strored From view state or not in preIntit Event
int i = Convert.ToInt32(ViewState["p"]) + 1;
ViewState["p"] = i.ToString();
Label1.Text = " Hi..";
Label1.Text = Label1.Text + "
" + " I am PreInit Event " + ViewState["p"];
// create button control dynamically
Button submit_button = new Button();
submit_button.ID ="btntxt";
submit_button.Text="Submit";
//When you create below event handler press Tab 2 times from your keyboard
submit_button.Click +=new EventHandler(submit_button_Click);
this.form1.Controls.Add(submit_button);
} Description:-
Here I have performed following steps in above c# codes as given below:-
- I have created TextBox control and its event handler dynamically.
- I have created a master page dynamically.
- I have also checked whether view state property enable in this section or not
- I have created Button control and its event handler dynamically.
- I have Showed some value in the label control.
2.) Init:-
- This event fires after each control have been initialized.
- This is used to read the property of each initialized control.
- Set Unique Id and skin properties of the controls.
- We can't get the Text Box value due to view state property is false after clicking the Submit Button.
- In this event, we can't able to get the postback values of the controls.
protected void Page_Init(object sender, EventArgs e)
//Checking whether value is strored From view state or not in Intit Event
int i = Convert.ToInt32(ViewState["a"]) + 1;
ViewState["a"] = i.ToString();
Label1.Text = Label1.Text + "
" + "I am Init Event "+ViewState["a"];
}
Description:-
- I have created a view state and stored in I variable to check value is stored from the view state or not
- Display the value of view state when page post back to the server.
The initialization process will be completed in this section.
- It is raised by the page object.
- This is used for an event processing tasks.
Example of incomplete Events:-
protected void Page_InitComplete(object sender, EventArgs e)
{
//Checking whether value is strored From view state or not in InitComplete Event
int i = Convert.ToInt32(ViewState["b"]) + 1;
ViewState["b"] = i.ToString();
Label1.Text = Label1.Text + "
" + " I am InitComplete Event " + ViewState["b"];
}
Description :-
- I have created a view state and stored in i variable to check value is stored from view state or not
- Display the value of view state when page post back to the sever.
- In This Event ,From where View state functionality starts retrieving the page controls data (value).
- In this event, View State Data is loaded to the controls.
- In this event, Post Back data are also handled by the Page controls
- This Event (PreLoad) always Fired after the InitComplete Event.
Example of PreLoad Event:-
protected override void OnPreLoad(EventArgs e)
{
//Page.EnableViewState = false;
//Checking whether value is stored From view state or not in OnPreLoad Event
int i = Convert.ToInt32(ViewState["c"]) + 1;
ViewState["c"] = i.ToString();
Label1.Text = Label1.Text + "
" + " I am PreLoad Event " + ViewState["c"];
}
Description :-
- I have created a view state and stored in i variable to check value is stored from view state or not
- I have Displayed the value to the label of view state when page post back to the sever.
- It use the On Load event method to set the properties of the controls and establish database connection.
- The page calls the On Load event method on the page then recursively does the same for each child controls until the control load and page load.
- We can check IsValid Property on the Page Load event method.
- We can Create Dynamic controls also in this event method.
- All the page values restored in this method.
protected void Page_Load(object sender, EventArgs e)
{
//Checking whether value is strored From view state or not in Load Event
int i = Convert.ToInt32(ViewState["d"]) + 1;
ViewState["d"] = i.ToString();
Label1.Text = Label1.Text + "
" + "I am Load Event " + ViewState["d"];
}
Description :-
- I have created a view state and stored in i variable to check value is stored from view state or not
- I have Displayed the value to the label of view state when page post back to the sever.
- It raised at the end of the event handling stage.
- It uses this event for tasks that require that all other controls on the page be loaded.
- All the event processing has been done in this event method.
Example Of LoadComplete Event:-
protected void Page_LoadComplete(object sender, EventArgs e) {
//Checking whether value is strored From view state or not in LoadComplete Event
int i = Convert.ToInt32(ViewState["e"]) + 1;
ViewState["e"] = i.ToString();
Label1.Text = Label1.Text + "
" + "I am LoadComplete Event " + ViewState["e"];
}
Description:-
- I have created a view state and stored in I variable to check value is stored from the view state or not
- I have Displayed the value to the label of view state when page post back to the server.
- PreRender event method is called when the page has created all the controls and displayed in the browser.
- The PreRender method is the last place where you can change any things on the page.
- Any change will be stored on the page.
- The PreRender event occurs for each control on the page.
Example of Prerender Event:-
//Checking whether value is strored From view state or not in OnPrerender Event
int i = Convert.ToInt32(ViewState["f"]) + 1;
ViewState["f"] = i.ToString();
Label1.Text = Label1.Text + "
" + "I am PreRender Event " + ViewState["f"];
}
Description :-
- I have created a view state and stored in i variable to check value is stored from view state or not
- I have Displayed the value in the label of view state when page post back to the sever.
- Before this events occurs,View State has been saved for the page and for all controls.
- This event is fired after the complete the PreRenderComplete event.
- In this event method, if we change the server control then the state will not be available in the next Post Backs.
- In this Event Method, We can do the changes in server control also.
protected override void OnSaveStateComplete(EventArgs e)
{
//Checking whether value is strored From view state or not in OnSaveComplete Event
int i = Convert.ToInt32(ViewState["g"]) + 1;
ViewState["g"] = i.ToString();
Label1.Text = Label1.Text + "
" + "I am SaveStateComplete Event " + ViewState["g"];
}
Description:-
- I have created a view state and stored in I variable to check value is stored from the view state or not
- I have Displayed the value in the label of view state when page post back to the server.
- This method (Render ) displays the HTML, DHTML and Scripts codes in control on the Browser (client-side).
- A user code (.ascx file) automatically incorporates rendering so you will not need to explicitly render the control in the code.
protected void Render(HtmlTextWriter writer)
{
writer.Write("<html><<h>Hi friends how r u </h></html>");
base.Render(writer);
}
Description:-
- Here, I have overridden the Render Method in my code.
- I can see the Html text on the browser.
- If I remove base.Render() method then we can't see this text on the browser.
- This is the last event that fired on the page.
- This is the page cleaning process such as Instances cleaning or objects, closing the database connections, closing the opened file.
- In this phase, we can't do any manipulation.
- Suppose if we call a method such as Response.write method then the page will throw an exception.
Example:-
protected void Page_UnLoad(object sender, EventArgs e){ |
int i = Convert.ToInt32(ViewState["h"]) + 1;
ViewState["h"] = i.ToString();
Label1.Text = Label1.Text + "
" + "I am UnLoad Event" + ViewState["h"];
}
No comments:
Post a Comment