Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

2 or more partial classes and an aspx file in asp.net 2.0

1 view
Skip to first unread message

ptass

unread,
Nov 10, 2005, 5:07:01 PM11/10/05
to
Hi

In asp.net 2.0 an aspx files .cs file is a partial class and all works fine,
however,
I thought I’d be able to create another class file, call it a partial class
and have
that compile and load as a 3rd partial class. This would be handy so i can
generate
standard code into one of the partial classes, while having my custom code
untouched
by the code generator.

However, when i try this, the 3rd .cs file doesn't even seem to compile, and
any
methods contained therein are not visible by the 1st .cs file, or anything
else
for that matter.

If I try 3 partial classes that aren’t part of an aspx then things work as
expected.

So I have 3 files...

Default.aspx
Default.aspx.cs
Default_.aspx.cs

Default.aspx and Default.aspx.cs interact as expected.

Default.aspx.cs and Default_aspx.cs both implement partial class Default:
System.Web.UI.Page

like so...

Default.aspx.cs

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
TestMethod();
}
}

Default_.aspx.cs

public partial class _Default : System.Web.UI.Page
{
public bool TestMethod()
{
return true;
}
}

When i compile the project i get the following error:
Error 1 The name 'TestMethod' does not exist in the current context
C:\Documents and Settings\petert\My Documents\Visual Studio
2005\WebSites\WebSite2\Default.aspx.cs
15 9 C:\...\WebSite2\

If i have a non aspx class, with three parts like so...

Class1.cs
Class1_.cs
Class1__.cs

public partial class Class1
{
public Class1()
{
TestMethod1();
TestMethod2();
}
}

public partial class Class1
{
public bool TestMethod1()
{
return true;
}
}

public partial class Class1
{
public bool TestMethod2()
{
return true;
}
}

All compile and work as expected.

I this a limitation with aspx files, or am I missing something.

If there is a specific asp.net 2.0 newsgroup then apologies, and please
direct me to it.

Also, is there a specific newgroup for asp.net 2.0 / vs2005 issues ?

Juan T. Llibre

unread,
Nov 10, 2005, 5:48:26 PM11/10/05
to
re:

> So I have 3 files...
>
> Default.aspx
> Default.aspx.cs
> Default_.aspx.cs

Drop Default_.aspx.cs into the App_Code directory.


Juan T. Llibre, ASP.NET MVP
ASP.NET FAQ : http://asp.net.do/faq/
ASPNETFAQ.COM : http://www.aspnetfaq.com/
Foros de ASP.NET en Español : http://asp.net.do/foros/
======================================
"ptass" <pt...@newsgroup.nospam> wrote in message
news:B5E171BA-C5B8-46F2...@microsoft.com...

Bruce Barker

unread,
Nov 10, 2005, 5:54:48 PM11/10/05
to
the problem is there is no way to tell the asp.net compiler to include the
third file with an aspx page. each page is built into its own dll, the aspx
page has in the page directive then name of the codebehind file (see
CodeFile=""), so the two files are compiled into the same assembly.

the files in app_code dir are all compiled into one assembly, so you can use
3 files here.


-- bruce (sqlwork.com)


"ptass" <pt...@newsgroup.nospam> wrote in message
news:B5E171BA-C5B8-46F2...@microsoft.com...

ptass

unread,
Nov 10, 2005, 7:15:02 PM11/10/05
to
This doesn't fix the problem.
I still get the error that the test method doesnt exist in the current
context.

ptass

unread,
Nov 10, 2005, 7:17:08 PM11/10/05
to
So it can't be done. ?

ptass

unread,
Nov 10, 2005, 9:09:02 PM11/10/05
to
i tried your suggestion, but i still get the same error whenever i try to
reference the method from the aspx.cs page.

Also, i don't see an attachment ?

Could you attach or post the working code + specify the location where each
file should sit ?

Thanks alot for your time so far.

"Juan T. Llibre" wrote:

> Maybe you should adopt a different naming scheme.
>
> See the attached 3-file.jpg, which shows, with the Class View
> and Solution Explorer windows, that the classes placed in App_Code
> ( person.birth.cs and person.name.cs ) are merged into the Person class
> which the person.aspx.cs calls with :
>
> public partial class person : System.Web.UI.Page
>
> person.birth.cs has the following class declaration :
>
> public partial class person
>
> and person.name.cs has the following class declaration :
>
> public partial class person
>
> It works...


>
>
>
>
> Juan T. Llibre, ASP.NET MVP
> ASP.NET FAQ : http://asp.net.do/faq/
> ASPNETFAQ.COM : http://www.aspnetfaq.com/
> Foros de ASP.NET en Español : http://asp.net.do/foros/
> ======================================
> "ptass" <pt...@newsgroup.nospam> wrote in message

> news:F6F84480-B2B2-46E1...@microsoft.com...

Steven Cheng[MSFT]

unread,
Nov 11, 2005, 1:17:24 AM11/11/05
to
Hi Ptass,

I Agree with Bruce's explanation. For ASPX page, their page class's partial
class file are particualrly specified through the "codeFile" attribute in
@Page directive. Also, the source files in App_Code folder will be compiled
into different assemlies with the Page class's assembly, and the dynamic
compilation time and sequence are also different. So manually provide
additional partial class file for aspx page is not supported naturally.

Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)


--------------------
| Thread-Topic: 2 or more partial classes and an aspx file in asp.net 2.0
| thread-index: AcXmVUDxW9ivrTZTQjiHYYD7b71cCQ==
| X-WBNR-Posting-Host: 203.58.99.1
| From: "=?Utf-8?B?cHRhc3M=?=" <pt...@newsgroup.nospam>
| References: <B5E171BA-C5B8-46F2...@microsoft.com>
<OtYhVnk5...@TK2MSFTNGP09.phx.gbl>
| Subject: Re: 2 or more partial classes and an aspx file in asp.net 2.0
| Date: Thu, 10 Nov 2005 16:17:08 -0800
| Lines: 127
| Message-ID: <03A1057C-7C0F-4567...@microsoft.com>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="Utf-8"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| Content-Class: urn:content-classes:message
| Importance: normal
| Priority: normal
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.2.250
| Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGXA03.phx.gbl
| Xref: TK2MSFTNGXA02.phx.gbl
microsoft.public.dotnet.framework.aspnet:357230
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet

ptass

unread,
Nov 13, 2005, 9:58:02 PM11/13/05
to
And un naturally :-) ? ie., Is there a workaround ?

Juan T. Llibre

unread,
Nov 14, 2005, 4:17:19 AM11/14/05
to
re:

> And un naturally :-) ?

Read Steven's comment as follows :
> manually providing an additional partial class file
> for an aspx page is not supported, naturally.

re:


>ie., Is there a workaround ?

No, there isn't.
You can't have two code-behind files for one aspx page.

What you *can* do is :

1. Write utility classes and compile *them* into
assemblies which you can reference in your aspx pages.

This is the way I prefer to handle this need.

2. Use the App_Code folder as a repository for partial classes.
You can place many partial class definitions in separate files in App_Code.
They will all be compiled into a single class.

Juan T. Llibre, ASP.NET MVP
ASP.NET FAQ : http://asp.net.do/faq/
ASPNETFAQ.COM : http://www.aspnetfaq.com/
Foros de ASP.NET en Español : http://asp.net.do/foros/
======================================

"ptass" <pt...@newsgroup.nospam> wrote in message

news:29A86EED-F90D-4F75...@microsoft.com...


> And un naturally :-) ? ie., Is there a workaround ?
>
> "Steven Cheng[MSFT]" wrote:
>
>> Hi Ptass,
>>
>> I Agree with Bruce's explanation. For ASPX page, their page class's partial
>> class file are particualrly specified through the "codeFile" attribute in
>> @Page directive. Also, the source files in App_Code folder will be compiled
>> into different assemlies with the Page class's assembly, and the dynamic
>> compilation time and sequence are also different. So manually provide
>> additional partial class file for aspx page is not supported naturally.
>>
>> Thanks,
>>
>> Steven Cheng
>> Microsoft Online Support

Steven Cheng[MSFT]

unread,
Nov 14, 2005, 7:08:58 AM11/14/05
to
Hi Ptass,

Thanks for your response. I'm afraid using Multiple partial class files for
aspx page is not allowed due to the fixed page compilation model. However,
we can alternatively provide a common base page class for our pages'
codebehind class. For example, all the pages which will share some common
functionalities can use one central base page class. Let's call it
"CommonBasePage", then, let all those pages' code behind class inherit from
this common class so that this class's non-private functions can be shared
by those concrete classes. Also, in asp.net 2.0, there is one new
attribute named "CodeFileBaseClass", we can define our common base page 's
class through this attribute, so that the runtime will be able to
dynamically compiled the page's structure and make the Controls references
in concrete pages also available in common base class. For example, here is
an example which use this attribute:


=====common base page class(in App_Code)=========

public partial class CommonBasePage : System.Web.UI.Page
{
protected Label Label1;

public CommonBasePage()
{}

protected virtual void Page_Load(object sender, EventArgs e)
{

Label1.Text = SayHello();
}

public string SayHello()
{
return "Hello CommonBasePage.";
}

}
===========================================

======concrete page aspx==========
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="PageOne.aspx.cs"
Inherits="CommonPage_PageOne"
CodeFileBaseClass="CommonBasePage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server"
Text="Label"></asp:Label></div>
</form>
</body>
</html>
===============================

=====concrete page code behind==========
public partial class CommonPage_PageOne : CommonBasePage
{

}

=====================

We can see that the Label1 control in concrete page can also be accessible
in base page as long as we define the control reference......

Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security


(This posting is provided "AS IS", with no warranties, and confers no
rights.)

--------------------
| From: "Juan T. Llibre" <nomail...@nowhere.com>
| References: <B5E171BA-C5B8-46F2...@microsoft.com>
<OtYhVnk5...@TK2MSFTNGP09.phx.gbl>
<03A1057C-7C0F-4567...@microsoft.com>
<1uMDeeo5...@TK2MSFTNGXA02.phx.gbl>
<29A86EED-F90D-4F75...@microsoft.com>


| Subject: Re: 2 or more partial classes and an aspx file in asp.net 2.0

| Date: Mon, 14 Nov 2005 05:17:19 -0400
| Lines: 192
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.3790.1830
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.1830
| X-RFC2646: Format=Flowed; Original
| Message-ID: <uOLTExP6...@tk2msftngp13.phx.gbl>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| NNTP-Posting-Host: 222stb33.codetel.net.do 64.32.114.222
| Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
| Xref: TK2MSFTNGXA02.phx.gbl
microsoft.public.dotnet.framework.aspnet:357857
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet

Steven Cheng[MSFT]

unread,
Nov 16, 2005, 9:31:49 AM11/16/05
to
Hi Ptass,

Does the suggestion that use a common base page class in my last reply
helps? If there're still anything else we can help, please feel free to
post here.

Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
--------------------

| X-Tomcat-ID: 218109604


| References: <B5E171BA-C5B8-46F2...@microsoft.com>
<OtYhVnk5...@TK2MSFTNGP09.phx.gbl>
<03A1057C-7C0F-4567...@microsoft.com>
<1uMDeeo5...@TK2MSFTNGXA02.phx.gbl>
<29A86EED-F90D-4F75...@microsoft.com>

<uOLTExP6...@tk2msftngp13.phx.gbl>
| MIME-Version: 1.0
| Content-Type: text/plain
| Content-Transfer-Encoding: 7bit
| From: stc...@online.microsoft.com (Steven Cheng[MSFT])
| Organization: Microsoft
| Date: Mon, 14 Nov 2005 12:08:58 GMT


| Subject: Re: 2 or more partial classes and an aspx file in asp.net 2.0

| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
| Message-ID: <Lp3E6QR6...@TK2MSFTNGXA02.phx.gbl>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| Lines: 237
| Path: TK2MSFTNGXA02.phx.gbl
| Xref: TK2MSFTNGXA02.phx.gbl
microsoft.public.dotnet.framework.aspnet:357887
| NNTP-Posting-Host: tomcatimport2.phx.gbl 10.201.218.182

0 new messages