Invalid cast of master page class

Discussion in 'ASP .Net' started by Guest, Oct 4, 2006.

  1. Guest

    Guest Guest

    I have a page Default.aspx. Its first line is the following:

    <%@ Page Language="C#" MasterPageFile="~/MasterPage.master"
    AutoEventWireup="true" CodeFile="default.aspx.cs"
    Inherits="MyCompany.MyApp.default" Title="Untitled Page" %>

    The first line in MasterPage.master is the following:

    <%@ Master Language="C#" AutoEventWireup="true"
    CodeFile="MasterPage.master.cs" Inherits="MyCompany.MyApp.masterpage" %>

    In the MasterPage.master.cs file I am declaring a public property as follows:

    public MyCompany.MyApp.Controls.RelatedMenu MenuRelated
    get { return this.RelatedMenu1; }

    In the default.aspx.cs in Page_Load method I have the following line:

    ((masterpage)this.Master).MenuRelated.MenuItems = "select~add";

    Some times (not always) this line throws an InvalidCastException and its
    message says
    Unable to cast ASP.masterpage_master to MyCompany.MyApp.masterpage

    Any ideas why?

    Guest, Oct 4, 2006
    1. Advertisements

  2. Guest

    micklang Guest

    I'm encountering the same problem...

    It is only occurring on a page utilising a nested master page. Are you
    using a nested master page ?

    This problem is very strange because I find that the problem seems to
    be coming and going without changing any code. I think the problem is
    arising when I make changes to other pages (other than the page with
    the problem) and then click refresh in the browser without recompiling.

    If I go into Visual Studio do a full rebuild and redeploy.... the
    problem is gone and the this.Master property can suddenly be cast again
    to my nested master page type (without a single line of code being

    I can't think this can be anything other than a bug in the Framework.

    As a work around I'm considering not using nested master pages and
    simply duplicating the parent master and making modifications to it
    (yuck! kind of defeating the whole point of creating master pages).

    Can anyone offer me an explanation or a better work around ?
    micklang, Oct 5, 2006
    1. Advertisements

  3. Guest

    Guest Guest

    No, I am not using nested master pages. In fact the entrire application is
    using only one master page. So eliminating the parent master, might not solve
    the problem.
    Guest, Oct 6, 2006
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.