Call multiple cs files from one aspx using codebehind without complile

I would like to be able to call not only the codebehind for an .aspx but also another .cs. I don't want to have to compile the second .cs.

I'm hoping to do the equivalent to:

In original.aspx:
<%@ Page Language="c#" inherits="original" src="original.cs" inherits="second" src="second.cs" %>

Would I @Register the other cs?

Please provide a simplistic example.
famosoAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
SteveH_UKConnect With a Mentor Commented:
That's all fine, but I'd still recommend that you use Visual Studio to produce the code, as it will help you find potential bugs and code more quickly.

The final deployment can be solved by moving all the .cs and .designer.cs files into the App_Code folder, and ASP.NET will compile it all automatically.

I'm not sure of the benefit you perceive in doing it the way you are, although it is of course a good learning experience.

The main thing you need to do, if you are doing it by hand is to register the control on the ASP.NET page.

If you want to post your code, I'd be happy to resolve the issue for you.
0
 
Ashish PatelCommented:
I guess this isnt possible and you comment

>>>> I don't want to have to compile the second .cs.

I dont undertsand why, as without compiling how can you use the codebehind structure to make the framework understand the code?
0
 
SteveH_UKCommented:
Pages are compiled by ASP.NET automatically to classes.  A class can't inherit from more than one class, and so the same restriction exists.

If you put the .cs file in the App_Code folder (see http://msdn2.microsoft.com/en-us/library/t990ks23(VS.80).aspx) then ASP.NET will compile it automatically.

However you proceed, you will need to determine the relationship between your two files.  The page can inherit from the second .cs file so long as it is derived from the System.Web.UI.Page class.

If you want to split the page functionality then you should consider User Controls or Master Pages.  Do a search in MSDN (www.microsoft.com/msdn) for both of these.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
famosoAuthor Commented:
As it sits I have one.aspx created from notepad and the associated one.cs also created in notepad.  I just put them on my webserver and it runs without me compiling.  I wanted to call a "Global" .cs from one.aspx.  Are you saying it isn't possible to call another .cs other than the .cs that is associated with one.aspx unless I compile a .cs into a dll?
0
 
famosoAuthor Commented:
I guess this leads me to my next similar question

I have one.aspx, one.cs, a.ascx, and a.ascx.cs.  I have SomeFunction() in a.ascx.cs.

How do I reference SomeFunction() from one.cs?
0
 
SteveH_UKCommented:
If it is public (or visible to the assembly), once you have placed the control (a) on one.aspx, and given it an ID, you can just use:

AnID.SomeFunction()

The user control should be registered on the page, in which case the code shouldn't have any problems accessing it.

I suggest that you download Visual Studio 2008 Express as it will help you learn ASP.NET and does not cost anything.  See http://www.microsoft.com/express/.
0
 
famosoAuthor Commented:
I have VS2005 Pro.  I'm converting .asp -> .aspx and so far I haven't had to compile a thing.  I'm calling the codebehind, stored procedures, accessing data, etc.  My situation it would be helpful to not have to duplicate a function I already have in another page by calling another .cs.  I'm trying also to do without a bin directory and a dll.
0
 
famosoAuthor Commented:
I appreciate your help.

Here is my code:  I've chopped off some code for brevity but the good stuff is there.

As you can see I have a session control on my default.aspx.
In my control, session.ascx.cs, I have public strings.
In default.cs I am trying to call session.session_BusinessID but I get an error:
"The name 'session' does not exist in the current context"
Ideas?
default.aspx:
 
<%@ Page Language="c#" inherits="_default" src="default.cs" %>
<%@ Register TagPrefix="ernstinfo" TagName="session" Src="../Controls/session.ascx" %>
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<ernstinfo:session ID="session" runat="server" />
<html>
 
<head>
//******************************************************************
 
session.ascx.cs:
 
public class Session: UserControl {
  public string session_BusinessID = "";
  void Page_Load(Object sender, EventArgs E) {
    System.Data.DataSet sessionDS = null;
    // Check for Session
    try {
      // Check and Log Session
      // Session_Load updates LastActivity field
      sessionDS = CheckAndLogSession(Session["SessionID"].ToString(), ref errorText);
 
      if(sessionDS != null && sessionDS.Tables.Count > 0 && sessionDS.Tables[0].Rows.Count > 0) {
        dr = sessionDS.Tables[0].Rows[0];
        session_BusinessID = dr["businessid"].ToString();
      } else
//******************************************************************
 
default.cs
 
public class _default: Page {
  void Page_Load(Object sender, EventArgs E) {
    Response.Write(session.session_BusinessID.ToString());

Open in new window

0
 
famosoAuthor Commented:
Could it be that the default.cs Page_Load() is firing before session.ascx.cs Page_Load()?
0
 
SteveH_UKCommented:
The code looks fine, however the default.aspx.cs file doesn't know about the existence of the "session" control.

You can take two approaches:

1)  You can declare a local class variable.  This will be used by the page when the control is instantiated.
2)  You can use FindControl and then cast the result to your control type

1)   Add to default.cs:

        protected Session session;

2)  Use this code in Page_Load in default.cs

        Response.Write( ( (session)
                                      (FindControl("session")) )
                                    .session_BusinessID.ToString());

I would also recommend changing the type name of the Session control to be more descriptive, even a simple change like SessionControl would be appropriate.  After all, the object is a control, not a session, even if the control is used to represent a session.
0
 
famosoAuthor Commented:
I appreciate all your help.  I tried even using Page_LoadComplete which was ok after placing a bunch of labels on my control and populated them in that event.  Anyway, I went with the full VS Solution and it is working great.  I was staying away because the original site was asp and I thought I'd just write the equivalent in aspx but - it's so true - there is a perfect tool for the job and for this one was to use the technology given and go with VS.
0
 
SteveH_UKCommented:
Glad you've chosen to use VS directly.  I think you'll find great improvements in productivity and you're more likely to spot errors quickly.  VS is possibly the best development environment that exists.  Java fans will point to Eclipse, but even if they are right that still makes VS number 2 and a very good choice for development.  Watch out for the benefits in VS 2008.  The express editions are already out, but the pro edition won't be cheap!

Good luck with the project, and thanks for the points.
0
All Courses

From novice to tech pro — start learning today.