Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>See <a href="http://odetocode.com/blogs/scott/archive/2005/07/16/mastertype-in-asp-net-2-0.aspx" rel="nofollow">article</a> here.</p> <p>Here is something nice that comes from the new compilation model in ASP.NET 2.0. Let’s say you add a custom property to a master page code-behind file like so:</p> <pre><code>partial class otcMaster : System.Web.UI.MasterPage { public string FooterText { get { return Footer.Text; } set { Footer.Text = value; } } } </code></pre> <p>You can get to the master page for a web form using the inherited Master property, which returns a MasterPage reference. To get to a property defined in otcMasterPage though, you might think you need to use a cast.</p> <pre><code>((otcMaster)Master).FooterText == "foo" </code></pre> <p>Casting to a derived type is just a part of life when using frameworks and statically typed languages, but there is a better way. Use the @ MasterType directive in the ASPX.</p> <pre><code>&lt;%@ MasterType VirtualPath="~/otc.master" %&gt; </code></pre> <p>Now when ASP.NET codegens the page, it puts the following inside a partial class definition. Notice the Shadows keyword (that would be the new keyword in semicolon land [yeah, I’m experimenting with alternative languages]).</p> <pre><code>public new otc Master { get { return (otcMaster)base.Master; } } </code></pre> <p>The result is a strongly typed Master Page. We don’t need a cast, we can go right to the Master.FooterText property. Another way to do this is to specify a TypeName in the @MasterType directive. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload