views:

167

answers:

2

How can we access and reference classes and variables in different pages in ASP.NET? In version 2003, it was straight forward, just reference the class name of the page and the whole page becomes at your command, but with Websites instead of Web Applications, that does not seem to be the case. I have an App_Code folder, with a class that has the following string defined:

public string GetPath()
    {
         return mypage.thisUrl;
      }

and another page that looks like this:

public partial class mypage : System.Web.UI.Page
{
    public string thisUrl
    {
        get { return "/newfolder/mypage.aspx"; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {

    }
}

but obviously it doesnt work... it would be great if someone can refer me to a good source of knowledge as I tried MSDN, this site, and Google, and I also have a book sitting next to me, finding your way around is not easy :(

A: 
public string GetPath()    
{
     return mypage.thisUrl;   
}

does not look like a class. Maybe that is your problem in the first place. It looks like you should get some basic virtual labs training from Microsoft.

Microsoft Virtual Labs

CodeToGlory
i fixed the question
Ayyash
+2  A: 

You would need an instance of the mypage class or make thisUrl static.

Not a good idea to make pages dependent on each other like that IMHO

public partial class mypage : System.Web.UI.Page
{
    public static string thisUrl
    {
        get { return "/newfolder/mypage.aspx"; }
    }
}

public partial class mypage : System.Web.UI.Page 
{
   ... Page_Load() {
        mypage.thisUrl

        or

        new mypage().thisUrl
   }
}
Chad Grant
oh i dont mean to make them dependable, its a way to store page-specific details like path, it reduces maintenance time by tons
Ayyash
how do I make an instance of a class I cannot see?
Ayyash
You may have them in different namespaces, so import the namespaces. I highly recommend getting yourself a copy of Resharper to ease problems like these.
Chad Grant
I agree, this is not the correct way of thinking anyway, I will use sitemaps instead
Ayyash