tags:

views:

1525

answers:

1

I have contentpage overriding the OnInit event of a masterpage. The override works fine, until I put a custom user control on the page: in this case the OnInit event does not fire for the contentpage (no overrides are used in the user control)

What are the possible causes/solutions for this? (I use the OnInit event to create dynamic controls)


Edit:

now i tried this in the content page:

(The OnPreInit part runs, but Masters_Init does not get called...)

    protected override void OnPreInit(EventArgs e)
    {
        base.Master.Init += new EventHandler(Masters_Init);
    }

    void Masters_Init(object sender, EventArgs e)
    { 
    //code 
    }
+2  A: 

Are you calling the base.OnInit?

public override void OnInit(EventArgs e)
{
  // code before base oninit
  base.OnInit(e);
  // code after base oninit
}

Update

public class Page1 : Page
{
  public Page1 : base() {
    PreInit += Page_PreInit;
  }
  void Page_PreInit(object sender, EventArgs e)
  {
    Master.Init += Master_Init;
  }
  void Master_Init(object sender, EventArgs e)
  {
    //code
  }
}

Also as mentioned in the comments I would recommend not overriding the events if you don't have to, but if you must be sure to call the base. so in your edit above it should be

protected override void OnPreInit(EventArgs e)
{
  base.OnPreInit(e);
  base.Master.Init += new EventHandler(Masters_Init);
}
bendewey
sure, but the event is not firing: so it does not matter anyway
akosch
Can you just subscribe to the Init event of the masterpage instead of override OnInit?
bendewey
If it doesn't work post some code in your question.
bendewey
try subscribing to you event in the constuctor of the page. BTW if you override any methods always call the base. and don't override if you don't have to.
bendewey
updated my answer
bendewey
public Page1 { Master.Init += Masters_Init; }throws:NullReferenceException
akosch
try adding the call to base() in the constructor.
bendewey
correction http://msdn.microsoft.com/en-us/library/system.web.ui.page.master.aspx states that "The contents of a master page are not available until after the PreInit event has been raised"
bendewey
sorry i updated my code again
bendewey
preinit part works fine, but master_init didn't get called
akosch
Does your master page have any code-behind? does this work if you don't have the usercontrol added?
bendewey
yes it works without the user control and my masterpage does NOT have any related code, just some events for standard controls and a simple Page_Load.
akosch
I have to ask then whats in your usercontrol?
bendewey
akosch
Can you provide some code. or reproduce the issue in a small code sample that you can append to the question so that I can try to reproduce the issue locally?
bendewey
I just tested it with an empty usercontrol and it works... it really must be something in this particular control
akosch
Now i removed everything from codebehind (and the events from the markup) in the user control and it still happens
akosch
I sounds like there is something else going on here. Can you repro it in a blank project?
bendewey
got it: I think VS did put in another scriptmanager automatically into some obscure place. The compiler reported no error or warning, but things where screwed up. problem solved by removing second scriptmanager... Thanks for your time!
akosch