Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

accessing master page class from app_code

Status
Not open for further replies.

Jimi2Cool

Programmer
Jul 30, 2007
25
0
0
US
I’m currently working on merging the contents of a Web App project into an existing Website project both using .Net 2.0.

Aside from the many issues I’ve had I’m really stumped here.

I am using a page base class defined in app_code and a master page defined outside app_code. In the web app project the base class had no problem accessing the master page’s type/members but now that it’s in the website project in the same exact directories they were in in the web app project whenever I try to build I get a type or namespace not found error. Here’j the jist of the dir/namespace structure

App_code
-common
PageBase.cs (mysite.common namespace)


Common
-masterpages
MyMasterPage.master (namespace = mysite.common.masterpages)


And in my base class I have a strongly typed property returning the page’s Master property cast to MyMasterPage.

In the pagebase class intellisence doesn’t show the masterpages namespace

Any idea?
 
Where in your code is the error at? Can you post that code? Also, post the exact error... It is probably a reference issue, but hard to say without seeing the problem first hand.

Kevin Davie
Consultant
Sogeti USA
 
the error is occuring in the pagebase class which is under app_code

i have code like this in the PageBase class:

Public MySite.Common.MasterPages.MyMaster MyMasterPage
{
get
{
return (MySite.Common.MasterPages.MyMaster)Master;
}
}

and the exact error is:
"The type or namespace name 'MasterPages' does not exist in the namespace 'MySite.Common' (are you missing an assembly reference?)"

dont think it's a refference thing cause they are both within the same app and even same root namespace

Well, hope someone can help
 
Sorry, I misread.... The reason is because of where in the project your masterpage is.... Code in the code folder cannot access resources outside of the folder. The work-around for this is to take the code-behind file and drop it in the app_code folder, the update the codefile in the masterpage markup to reflect to correct path of the code-behind file... ie....

CodeFile="App_code/MasterPage.master.cs"

That's it... should work fine after making the change.

Kevin Davie
Consultant
Sogeti USA
 
FANTASTIC! IT WORKS! but...

so i guess i have a delema about this cause i'm starting to understand the access stuff with app_code

so, from app_code, my master page's code behind cant seem to see any of the controls in the masterpage.master file. this is the same type of issue isn't it?

So, i guess my next question is can i have a partial class in app_code and in the same dir as the .master? and if so, how do i tell the page it has code in both places?
 
Hmmm that is odd... works fine for me... Can you post example of how you are referencing the controls in your code? Also, maybe post the markup.

Kevin Davie
Consultant
Sogeti USA
 
ok, here's the code in the codebehind file

----------------------
namespace MyWebsite.Common.MasterPages
{
public partial class MasterPage : System.Web.UI.MasterPage
{

#region Properties

private string title;
public string Title
{
get { return title; }
set { title = value; }
}


private bool _topicPage = false;
public bool TopicPage
{
get { return _topicPage; }
set { _topicPage = value; }
}

private bool _docPage = false;
public bool DocPage
{
get { return _docPage; }
set { _docPage = value; }
}

private bool _searchPage = false;
public bool SearchPage
{
get { return _searchPage; }
set { _searchPage = value; }
}

private MyWebsite.Common.PageName _pagename = PageName.Undefined2008;
public MyWebsite.Common.PageName PageName
{
get { return _pagename; }
set { _pagename = value; }
}

#endregion

protected void Page_Load(object sender, EventArgs e)
{

}

protected void Page_PreRender(object sender, System.EventArgs e)
{

if (_topicPage)
{
PlaceHolderTopicStyleSheet.Visible = true;
}
else
{
PlaceHolderTopicStyleSheet.Visible = false;
}

if (_docPage)
{
PlaceHolderDocStyleSheet.Visible = true;
}
else
{
PlaceHolderDocStyleSheet.Visible = false;
}

if (_searchPage)
{
PlaceHolderSearchStyleSheet.Visible = true;
}
else
{
PlaceHolderSearchStyleSheet.Visible = false;
}

OmnitureTag1.PageName = _pagename;
//base.OnPreRender(e);

}

}
------------------
I get "ControlName" does not exist in the current context errors

 
Here's the markup

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="~/App_Code/CodeBehind/MyMasterPage.Master.cs"
Inherits="EncMyWebsiteyclopedia4.Common.MasterPages.MasterPage" %>
<%@ Register Src="../Controls/MasterPages/OmnitureTag.ascx" TagName="OmnitureTag"
TagPrefix="uc3" %>
<%@ Register Src="../Controls/Header.ascx" TagName="Header" TagPrefix="uc2" %>
<%@ Register Src="../Controls/Footer.ascx" TagName="Footer" TagPrefix="uc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "<html xmlns="<head id="Head1" runat="server">
<title>My Website -
<%# Title %>
</title>
<link href="/CSS/global.css" rel="stylesheet" type="text/css" />
<link href="/CSS/shorthand.css" rel="stylesheet" type="text/css" />
<asp:placeholder id="PlaceHolderTopicStyleSheet" runat="server">
<!-- TODO:eek:nly present when on topics page -->
<link href="/CSS/hbe-topic.css" rel="stylesheet" type="text/css" />
</asp:placeholder>
<asp:placeholder id="PlaceHolderDocStyleSheet" runat="server">
<!-- TODO:eek:nly present when on doc page -->
<link href="/CSS/hbe-article.css" rel="stylesheet" type="text/css" />
</asp:placeholder>
<asp:placeholder id="PlaceHolderSearchStyleSheet" runat="server">
<!-- TODO:eek:nly present when on doc page -->
<link href="/CSS/hbe-search.css" rel="stylesheet" type="text/css" />
</asp:placeholder>
<script type="text/javascript">
<!--
function LoadOtherSite()
{
var obj =document.getElementById('iframe_OtherSite');
obj.src = "/OtherSiteAd.aspx";
return false;
}

function LoadYouTube()
{
var obj =document.getElementById('iframe_youtube');
obj.src = "/YouTubeDisplay.aspx";
return false;

}
// -->
</script>
</head>
<body onload="LoadOtherSite();LoadYouTube();">
<form id="form1" runat="server">
<uc2:Header ID="Header1" runat="server" />
<div id="shell">
<!-- Start of Shell -->
<div id="center-container">
<!-- center content start here -->
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
<!-- center content ending tag -->
<div class="clearfix">
</div>
</div>
<div id="footer">
<uc1:Footer ID="Footer1" runat="server" />
</div>
<uc3:eek:mnituretag id="OmnitureTag1" runat="server" />
</form>
<!-- end of Shell-->
</body>
</html>
 
take the tilda out of the codefile. It should look like this:

CodeFile="App_Code/CodeBehind/MyMasterPage.Master.cs"




Kevin Davie
Consultant
Sogeti USA
 
nope, no effect

I'm wondering if this has anything to do with moving the files from a web app to a website?

the structure was the same in the web app. it wasn't until i moved it to a website i started getting these errors
 
DING DING DING!
WE HAVE A WINNER!

so, i was able to split the code into 2 files, the properties (accessed all over the place) went in app_code, the event handlers went int the same folder as the master page. i refferenced the one in it's own directory for codefile since codefile's just for the designer and everything seems to work.

I realize this is more of a work-around than a solution and would still be intersted in hearing other ideas. but i think i can finally move on:)
 
Nice, at least you got it working.... also, FYI... There is an add-on for visual studio 2005 called "Microsoft Visual Studio Web Application Projects." You may find it helpful... You can get it off of MSDN... I believe it is part of the latest service pack too.

Kevin Davie
Consultant
Sogeti USA
 
yeah, i think that was part of the issue.

originally the site was a standalone web application project
i merrged the contents into an existing website project.

the web application project uses codebehind and the website uses codefile and i found out right away THERE IS A DIFFERENCE:)

unfortunately i moved forward straight into loads of errors from code inside my app_code trying to refference my DAL (external project but clearly refferenced). even though intellisence says the DAL, it's classes and there methods, every declaration of the datasets ort tables (though syntax highlighting says they're found) throws an error at compile time.

Oddly the compiler says the tableadapter's not found but also says that adapter doesn't define those methods... strange

Seriously, integrating these 2 sites is making me feel like a newbie:(
 
Yeah... Microsoft recommends that you install the add-on first and then run their migration wizard... I've migrated (from 1.1 to 2.0) over 100 projects for the client I am currently working at and have had great success. If you continue to have problems, you may want to just start over... It only takes a few seconds to migrate with the wizard. even if everything doesn't convert successfully, you will get a log of any failures and can resolve the issues manually.

Good Luck,

-Kevin

Kevin Davie
Consultant
Sogeti USA
 
well, i guess my problem stems from the fact that i'm migrating from a 2.0 web app to a 2.0 website, neither are in 1.1 and i read that the wizard doesn't migrate from 2.0 web app (created using the web app pluggin" to a 2.0 website, this is irritating and think i'm the one who originally (and without reading) said "well, they just put out this web application add-on, must be better, lets use that" not realizing that it was more for integration
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top