Search code examples
asp.netvb.netweb-user-controls

Initialization in Asp.NET Master Page


I need to do a dynamically loaded Header and Footer on Master page where there are two placeholders to store the users controls which will become the Headers and Footers. The reason why I am doing it to make it more customizable and it is the how it is required from my supervisor as well.

So I have two key-value in web.config such as

    <add key="MasterHeader" key="~/Controls/Header.ascx"/>
    <add key="MasterFooter" key="~/Controls/Footer.ascx"/>

So I am wondering which Page event should I load these users controls by saying

UserControl uc = (UserControl) Page.LoadControl(ConfigFile.MasterHeader))
PlaceHolder1.Controls.Add(uc) 
  1. Where is the best Page event to put the codes above?
  2. Do I need to put something in the page directive to make it work?

Solution

  • I would use OnPreInit or OnPreLoad. You shouldn't need to put anything in the page directive to make this work, the way you're describing it.