Web Tutorials

Interview Q & A

Code Examples

Utility Tools

ASP.NET PreInit Event and AutoEventWireup

ASP.NET PreInit is the first event of asp.net page life cycle. And fired before page initialised. If the AutoEventWireup attribute is set to false, the events need to be wired up manually.

Syntax

protected override void OnPreInit(EventArgs e)
{
	//Code will come here.  
}

ASP.NET PreInit Event and AutoEventWireup Example - Default.aspx

<%@ Page Language="C#" AutoEventWireup="false" CodeFile="Default.aspx.cs" 
Inherits="_Default" %>
<!DOCTYPE html">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="lblTitle" runat="server" Text="ASP.NET Page Life Cycle"></asp:Label><br />
        <asp:label ID="lblDisplayPageEventsSteps" runat="server" text="Label"></asp:label>
    </div>
    </form>
</body>
</html>

ASP.NET PreInit Event and AutoEventWireup Example - Default.aspx.cs

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

public partial class _Default : System.Web.UI.Page
{
	//Override PreInit event and wired up preinit and load event manually
    protected override void OnPreInit(EventArgs e)
	{
		PreInit += new EventHandler(Page_PreInit);
		Load += new EventHandler(Page_Load);
		base.OnPreInit(e);
	}

	protected void Page_PreInit(object sender, EventArgs e)
	{
		this.lblDisplayPageEventsSteps.Text = "Page_PreInit event fired!<br />"; 
	}

	protected void Page_Load(object sender, EventArgs e)
	{
		this.lblDisplayPageEventsSteps.Text = "Page_Load event fired!<br />";
	}
}

Above example will produce following output

ASP.NET Page Life Cycle
Page_PreInit event fired!
Page_Load event fired!

RequiredFieldValidator

RegularExpressionValidator

RangValidator

CompareValidator

CustomValidator

ValidationSummary