Web Tutorials

Interview Q & A

Code Examples

Utility Tools

ASP.NET Page PreRender Event

ASP.NEt page PreRender event will fire before page start rendering in ASP.NET page life cycle.

Syntax

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

ASP.NET Page PreRender Event Example - Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" 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>lt;br />
        <asp:label ID="lblDisplayPageEventsSteps" runat="server" text="Label"></asp:label>
    </div>
    </form>
</body>
</html>

ASP.NET Page PreRender Event 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
{
    protected void Page_PreInit(object sender, EventArgs e)
    {
        //This will fire before page initialised and set value to lable. 
        lblDisplayPageEventsSteps.Text = "This is PreInit Event";
    }
	protected void Page_Init(object sender, EventArgs e)
    {
        //This will fire when the page is initialised and set value to lable. 
        lblDisplayPageEventsSteps.Text += lblDisplayPageEventsSteps.Text = "<br/>This is Init Event";
    }
	protected void Page_InitComplete(object sender, EventArgs e)
    {
        //This will fire when page initialization completed and set value to lable. 
        lblDisplayPageEventsSteps.Text += lblDisplayPageEventsSteps.Text = "<br/>This is InitComplete Event";
    }
	protected void Page_PreLoad(object sender, EventArgs e)
    {
        //This will fire before page load and set value to lable. 
        lblDisplayPageEventsSteps.Text += lblDisplayPageEventsSteps.Text = "<br/>This is PreLoad Event";
    }
	protected void Page_Load(object sender, EventArgs e)
    {
        //This will fire when page is loaded and set value to lable. 
        lblDisplayPageEventsSteps.Text += lblDisplayPageEventsSteps.Text = "<br/>This is Load Event";
    }
	protected void Page_LoadCompleted(object sender, EventArgs e)
    {
        //This will fire when page is loaded and set value to lable. 
        lblDisplayPageEventsSteps.Text += lblDisplayPageEventsSteps.Text = "<br/>This is LoadCompleted Event";
    }
	protected void Page_PreRender(EventArgs e)
    {
        //This will fire when page is loaded and set value to lable. 
        lblDisplayPageEventsSteps.Text += lblDisplayPageEventsSteps.Text = "<br/>This is PreRender Event";
    }
}

Above example will produce following output

ASP.NET Page Life Cycle
This is PreInit Event
This is Init Event
This is InitComplete Event
This is PreLoad Event
This is Load Event
This is LoadCompleted Event
This is PreRender Event

RequiredFieldValidator

RegularExpressionValidator

RangValidator

CompareValidator

CustomValidator

ValidationSummary