Tuesday, October 1, 2019

Asp.net Difference between Page_Load and Page_Init in C#, VB.Net

Page_Init Event

This event will raise whenever page initialized and its first step in page life cycle. In this event all the controls in the page have been initialized and any theme or skin properties will be applied. This Page_Init event can be used to read or initialize control properties.

Declaration of Page_init

Generally we will use page_init event like as shown below


protected void Page_Init(object sender, EventArgs e)
{
// Your Code Here
}

For example check this for page_init event change page themes dynamically in asp.net

Page_Load Event

This event occurs only after Page_Init event and this event also will raise for every postback operation and in this stage all control properties are loaded with information recovered from view state and control state.

Declaration of Page_Load Event

Generally we will use page_load event like as shown below


protected void Page_Load(object sender, EventArgs e)
{
// Your Code Here
}
If you want to check it in complete example you need to write the code in your aspx page like as shown below


<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form2" runat="server">
<div>
</div>
</form>
</body>
</html>
In code behind you need to write the code like as shown below

C# Code


using System;
public partial class BindDropdowninGridview : System.Web.UI.Page
{
protected void Page_Init(object sender, EventArgs e)
{
Response.Write("Init Event");
}
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("Page Load Event");
}
}
VB.NET Code


Partial Class VBCode
Inherits System.Web.UI.Page
Protected Sub Page_Init(ByVal sender As ObjectByVal e As EventArgs)
Response.Write("Init Event")
End Sub
Protected Sub Page_Load(ByVal sender As ObjectByVal e As EventArgs)
Response.Write("Page Load Event")
End Sub
End Class

No comments:

Post a Comment

Get max value for identity column without a table scan

  You can use   IDENT_CURRENT   to look up the last identity value to be inserted, e.g. IDENT_CURRENT( 'MyTable' ) However, be caut...