Because master pages are actually user controls, they do not have PerInit event. But this is the only time when you can change the MasterPageFile property. So, what can we do if we have sophisticated hierarchy of master pages and we need to change the master page of some master page within this master page? The only way to do this is to handle PreInit event of the page, find the appropriate master page and change its master page. But if you still want to have the logic which master page should be used runtime within the master page file, here is one solution to do that with reflection. Override page OnPreInit method and write this code:
// simulates PreInit event for Master pages
MasterPage master = Master;
while ( master != null )
{
MethodInfo mi = master.GetType().GetMethod( "PreInit" );
if ( mi != null )
{
mi.Invoke( master, null );
}
master = master.Master;
}
Then, in the .master file which you would like to change its master page runtime, you should define method like this:
<script runat="server" type="text/C#">
public void PreInit()
{
Account acc = LoggedAccount.GetFromSession( Session );
MasterPageFile = string.Format( "~/local/en-US/{0}/Main.master", acc.Group );
}
</script>
Showing posts with label Master Page. Show all posts
Showing posts with label Master Page. Show all posts
Monday, March 17, 2008
Sunday, November 4, 2007
Wizzard Control With Master Page Problem
I had a page that had a wizzard control and it worked fine. But after I've added a master page, it got the following error (when I click on the Next or Previous Button):
"The command 'MovePrevious' is not valid for the previous step, make sure the step type is not changed between postbacks".
When I debugged the page, I saw that always ActiveStepIndex = 0 and somehow it had been lost during the postback. The problem was that I had changed the ID of the master page on its Load event. That was the problem. When I changed it to use Init, not Load, everything got fine again. So, it is important to know exactly how the events are being raised. The order is the following:
Init (master page)
Init (page)
Load (page)
Load (master page)
You see that first is executed Load of the page and after that the Load event of the master page. And because I changed the ID of the master page on Load (it reflects the name of the controls in the page) the page lost its data.
Conclusion - be sure how the events are raised in page lifecycle.
PS: The bad thing was that I had to find it on my own. There's no info about that on the net.
"The command 'MovePrevious' is not valid for the previous step, make sure the step type is not changed between postbacks".
When I debugged the page, I saw that always ActiveStepIndex = 0 and somehow it had been lost during the postback. The problem was that I had changed the ID of the master page on its Load event. That was the problem. When I changed it to use Init, not Load, everything got fine again. So, it is important to know exactly how the events are being raised. The order is the following:
Init (master page)
Init (page)
Load (page)
Load (master page)
You see that first is executed Load of the page and after that the Load event of the master page. And because I changed the ID of the master page on Load (it reflects the name of the controls in the page) the page lost its data.
Conclusion - be sure how the events are raised in page lifecycle.
PS: The bad thing was that I had to find it on my own. There's no info about that on the net.
Thursday, November 1, 2007
Get Rid Of _ctl0 Prefix In UniqueID Of Master Page
Master Pages seemed to solve old problems pretty well but now we've got some new. If you wander how to get rid of the prefix _ctl0 that is the answer - just set an ID of the master page in Page_Init. For example if we have MyMaster.master file, we need to have similar to this code:
<script runat="server" type="text/C#">
protected void Page_Init( object sender, EventArgs e )
{
this.ID = "MyMaster";
}
</script>
NOTE: Do not use Load event! It causes problems with wizzard and multiview controls... more
<script runat="server" type="text/C#">
protected void Page_Init( object sender, EventArgs e )
{
this.ID = "MyMaster";
}
</script>
NOTE: Do not use Load event! It causes problems with wizzard and multiview controls... more
Subscribe to:
Posts (Atom)
