views:

90

answers:

1

Hi,

I am migrating from ASP.NET 1.1 and I used to subscribe to page events using designer which adds subscriptions to InitializeComponent method on the page.

In the ASP.NET 2.0, how can I subscribe to Page events from the designer?

Not using designer this seems to work.

protected void Page_Init(object sender, EventArgs e) {
  // The code, no safety here, just convention
}

// OR

protected override void OnInit(EventArgs e) {
    base.OnInit(e);
    // The code
}

What is the recommended way of subscribing to page events? Same question applies to the MasterPage.

Additionally what is the best way to subscribe to events on the UserControl? When I declare the Page_XXX on the UserControl the event gets called many times.

+1  A: 

Pages support auto event-wire-up, meaning that ASP.NET looks for methods with particular names and automatically runs those methods when certain events are raised. If the AutoEventWireUp attribute of the Page directive is set to true (bydefault it is true - asp.net 2.0), page events are automatically bound to methods that use the naming convention of Page_Event, such as Page_Load, Page_PreInit, Page_Init etc.

void Page_PreInit(){

}
void Page_Init(){

}

EDIT:

@Dmitriy : So only the method name matters?

Yes

@Dmitriy : Also how do you subscribe to events from designer?

You can't subscribe page events through designer.

See this example: How to add event handler for page events if AutoEventWireUp is false?

 public _Default() //Constructor
     {
        this.Load += new EventHandler(_Default_Load);
    }

    void _Default_Load(object sender, EventArgs e)
    {

    }
adatapost
I know that I have the same sample as you provided in the question. It doesn't answer anything. In the ASP.NET 2.0, how can I subscribe to Page events from the ***designer***? What is the recommended way of subscribing to page events?
Dmytrii Nagirniak
In fact, I am avoiding the use of two arguments event handler.
adatapost
I haven't mentioned that. So only the method name matters?Also how do you subscribe to events **from designer**?
Dmytrii Nagirniak