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>
Monday, March 17, 2008
CodeFile vs Codebehind?
I have just migrated one vs2005 web site project to vs2005 web application project and noticed that instead of CodeFile attribute, tha Page/Control directive uses Codebehind and searched to find something more about these two.
CodeBehind
Specifies the name of the compiled file that contains the class associated with the control. This attribute is not used at run time.
Note:
This attribute is included for compatibility with previous versions of ASP.NET, to implement the code-behind feature. In ASP.NET version 2.0, you should instead use the CodeFile attribute to specify the name of the source file, along with the Inherits attribute to specify the fully qualified name of the class.
CodeFile
Specifies a path to the referenced code-behind file for the control. This attribute is used together with the Inherits attribute to associate a code-behind source file with a user control. The attribute is valid only for compiled controls.
CodeBehind
Specifies the name of the compiled file that contains the class associated with the control. This attribute is not used at run time.
Note:
This attribute is included for compatibility with previous versions of ASP.NET, to implement the code-behind feature. In ASP.NET version 2.0, you should instead use the CodeFile attribute to specify the name of the source file, along with the Inherits attribute to specify the fully qualified name of the class.
CodeFile
Specifies a path to the referenced code-behind file for the control. This attribute is used together with the Inherits attribute to associate a code-behind source file with a user control. The attribute is valid only for compiled controls.
Enable Edit and Continue in VS2005 Web Application Project
You must have used to keeping your project debugging while you write code. This is for sure because of the timeconsuming build that you could afford just once a day. But now with using web application project this is a bit different. First, it builds faster, second, you can enable edit and continue mode. This is how to do that:
1. Go to project Properties, tab Web. Check Enable and Continue. Note that this is available only when debugging in local VS dev server, but not IIS web server.
2. Go to Tools/Options/Debugging/General and check Break all processes when one process breaks.
3. Make sure you start your project in Debug mode.
4. Set a breakpoint in the page that you would like to edit. When the process stops at this breakpoint, you can edit this page (and only this page!)
See more about web application project and edit and continue here.
1. Go to project Properties, tab Web. Check Enable and Continue. Note that this is available only when debugging in local VS dev server, but not IIS web server.
2. Go to Tools/Options/Debugging/General and check Break all processes when one process breaks.
3. Make sure you start your project in Debug mode.
4. Set a breakpoint in the page that you would like to edit. When the process stops at this breakpoint, you can edit this page (and only this page!)
See more about web application project and edit and continue here.
Subscribe to:
Posts (Atom)
