Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET page_init event?

Tags:

asp.net

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.

like image 357
Anil Namde Avatar asked Nov 02 '10 10:11

Anil Namde


3 Answers

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
}
like image 172
RPM1984 Avatar answered Dec 21 '22 15:12

RPM1984


Just declare this in your code behind:

protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
    }
like image 29
kemiller2002 Avatar answered Dec 21 '22 15:12

kemiller2002


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) {
  ...
}
like image 26
Guffa Avatar answered Dec 21 '22 15:12

Guffa