The order of events in a page life cycle is this:
- MasterPage Init
- Page Init
- Page Load
- MasterPage Load
So, you should put your code in the Init event of your MasterPage
Here is the complete ordered list of events.
The general rule for how events are raised is that the initialization events are raised from the innermost control to the outermost one, and all other events are raised from the outermost control to the innermost one. It is helpful to remember that the master page is merged into the content page and treated as a control in the content page.