How to fix namespace problem with autogenerated Master property if MasterType is set
Asked Answered
C

5

5

after weeks of having this issue I finally decided to ask for a solution to the following problem:

In the .aspx page you can set

<%@ MasterType VirtualPath="~/Mastername.master" %>

This results in an auto generated property in the .aspx.designer

public new Mastername Master {
    get {
        return ((Masternamee)(base.Master));
    }
}

Works perfectly fine. But if I do changes in the .aspx file, the property will be new auto generated and it looks like the following:

public new NAMESPACE1.Mastername Master {
            get {
                return ((NAMESPACE1.Mastername)(base.Master));
            }
        }

Compiling will not be possible afterwards, because the class for the MasterPage cannot be resolved at the given namespace. The masterpage has NAMESPACE1 as namespace.

Every contentpage has the same NAMESPACE1. The autogenerated property tries to look for the masterpage class in NAMESPACE1.NAMESPACE1 which will fail, due to it does not exist. Of course I can remove the first NAMESPACE1. to make the app compilable again, but it just sucks to do this nearly every time I make changes in the .aspx file.

Is there a way to avoid this problem? The only way I can think of, is to ignore the auto generated property and make a explicit cast everytime I want have access to the masterpage.

Edit: I'm using Visual Studio 2008 Professional SP1.

Chemotherapy answered 4/1, 2010 at 11:17 Comment(1)
BTW, I've never had that issue after upgrading to VS2010Chemotherapy
C
0

I found a solution that works. I won't use the autogenerated property in the designerfile. I'll write my own wrapper property that I do implement in every contentpage.

Chemotherapy answered 14/1, 2010 at 14:5 Comment(2)
that isn't very satisfying at all.Woaded
@citronas: sadly, no. I apologize if my comment came across as demeaning. It was intended to be sympathetic. I'll post an answer here if I do find a cleaner solution.Woaded
B
1

For some reason the designer believes that the master page is defined in namespace NAMESPACE1, so look at the master page definition (and code behind) to check its namespace has not been modified (possibly accidentally).

If there is nothing obvious, a search in all files (*.cs, *.aspx, *.master, ...) for NAMESPACE1 may be needed.

(This is where using a VCS would help --- you could check the history of changes.)

Bradski answered 4/1, 2010 at 11:39 Comment(1)
This problem occurs in serveral different projects. The namespace of the masterpage hadn't changed in any of these projects.Chemotherapy
S
1

Actually it's more a designer "feature". ;-)

The Master name used in your designer file will be pulled from your .Master file's Inherits property. So change how you qualify the Inherits attribute, and that will change the class name used when the designer file is created.

Sharilyn answered 9/5, 2011 at 17:9 Comment(0)
C
0

I found a solution that works. I won't use the autogenerated property in the designerfile. I'll write my own wrapper property that I do implement in every contentpage.

Chemotherapy answered 14/1, 2010 at 14:5 Comment(2)
that isn't very satisfying at all.Woaded
@citronas: sadly, no. I apologize if my comment came across as demeaning. It was intended to be sympathetic. I'll post an answer here if I do find a cleaner solution.Woaded
I
0

I had this same problem when I added <%@ MasterType VirtualPath="~/TestMaster.Master" %> to my aspx page in SOURCE view. For some reason, the page never created correctly and kept giving me invalid namespace errors until I actually changed to DESIGN view and resized a control and finally the error went away. Somewhere it was using some cached data (even a Build/Clean Solution didn't clear it out) and until the designer recreates the page, it generates that error.

Instead answered 24/8, 2012 at 15:31 Comment(0)
G
0

Change

<%@ MasterType VirtualPath="~/Mastername.master" %>

to

<%@ MasterType TypeName="Mastername" %>

this will work perfectly

Grandmamma answered 9/6, 2015 at 5:42 Comment(1)
This will compile but at runtime it will error. Could not load type 'Mastername'. Remove any namespaces set in code that match the root namespace and the designer generated code will work with VirtualPath.Charlsiecharlton

© 2022 - 2024 — McMap. All rights reserved.