25

I am using ASP.NET 3.5 and i used earlier 1.1 i am having difficulty to find where can i attach/declare the page init event ?

In 1.1 there was auto generated code which used to have initialization code. Where we can add the page init method. So i am confused please help.

Anil Namde
  • 6,452
  • 11
  • 63
  • 100

7 Answers7

62

ASP.NET 2.0 changed the default designing/compilation model.

By default AutoEventWireup is set to true, which instructs compiler to automatically attach event handlers from the code behind using naming convention, so when you write:

protected void Page_Load(...)
{

}

it automatically puts this code in behind the scenes:

this.Load += new EventHandler(this.Page_Load)

This was previously done by InitialiseComponent() (i believe).

Nonetheless, the answer is to write the code yourself:

protected void Page_Init(object sender, EventArgs e)
{
    // do the bartman
}
RPM1984
  • 72,246
  • 58
  • 225
  • 350
  • The only problem is that there is no autocomplete available on this. You have to know exact syntax of the event handler. – FrenkyB May 13 '17 at 06:12
49

Just declare this in your code behind:

protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
    }
kemiller2002
  • 113,795
  • 27
  • 197
  • 251
7

You don't have to bind the event. Just create an event handler for it, and it will be bound automaticlaly:

protected void Page_Init(object sender, EventArgs e) {
  ...
}
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
2

For those using asp/vb.net you need to declare in code behind as: Protected Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Init

0

you can add the page_init method in page's CS file. For example, if you have Default.aspx you can put the method in Default.aspx.cs

When you create a page in VS you will have the Page_Load method created for you. You can put your page_init code & other code for the page int the CS file.

PS: If you use VB as the server side code, you will have to put it in the VB file

Abdel Raoof Olakara
  • 19,223
  • 11
  • 88
  • 133
0

It is no different in ASP.NET 3.5 - there is a code-behind page where you can declare/attach the OnInit event.

To see the code behind, right click on the file in the solution explorer and select View code.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
0

just add yourself with the signature

protected void Page_Init() 
{
    //
}
Kritner
  • 13,557
  • 10
  • 46
  • 72