How do I reference a second class than the codebehind page?

N

needin4mation

Hi, I have my .aspx page and its .cs codebehind page that Visual Studio
created.

What if I want to create a function or class in another .cs page but
want to use it in the codebehind page. How can I reference this new
c-sharp (or vb.net page) in the codebehind page? Thanks.
 
G

Grant Merwitz

A code behind is in fact a class and can be called

Ensure the method you are calling is public.
Ensure the method you are calling does not rely on elements on its page like
textboxes etc.

Hope this can help, haven't the time to test it, but should work

Here goes:


First page:

namespace MyNamespace
{
public class FirstPage : System.Web.UI.Page
{
public void DoSomething() //make sure public
{
//HERE IS STUFF YOU WANT TO DO FROM ANOTHER PAGE
//Like populate a database or return a string - but then would
be public string DoSomething()
}
}
}


Second page:

namespace MyNamespace
{
public class FirstPage : System.Web.UI.Page
{
private void DoSomethingFromFirstPage()
{
MyNamespace.FirstPage.DoSomething(); //don't need MyNamespace if
in same class (like this example)
}
}
}
 
G

Guest

Although you may be able to call this from another class directly, you really
should break out any of these "comon" methods into its own class and have
each page use the functions on that class.

Things such as business logic and data access are examples that really
deserve their own classes, if not their own projects and namespaces.

Hope that helps,

Jason Lind
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top