Partial class in ASP.NET

P

pawel.pabich

Hajo,

I would like to have 2 my own partial classes.
For example:

Default.aspx.cs
Default2.aspx.cs

and they both will relate to
Default.aspx page.

I have tried to do this in many ways but
I've been unlucky. Any ideas?

thanks in advance for any info

Gawel
 
P

pawel.pabich

Hajo,

I use beta 2 of .NET framework.
I can gain desired effect with Windows Forms Application
but I can not do the same with ASP.NET application.
VS.NET seems not to see the additional class let's say
Default2.aspx.cs. I understand the concept of partial classes
but I am not able to use them in web application.

Any ideas?

Gawel
 
G

Guest

You will have to compile the application to get it to work the way you
desire. By default, each page compiles on the fly. If you follow the default
model, only one partial class can be used per page.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************
 
P

pawel.pabich

You mean, that I need to change compilation model? Anyway is it
possible
to gain this effect directly from VS.NET? If so then can you let me
knwo how?

cheers

Gawel
 
P

pawel.pabich

Default2.aspx.cs is not a class

I can not agree. If it is not a class then what is it? Of course it is
partial class
but it is class anyway.

I know that all I need to do is to compile both classes into one
assembly. But If I
put Default2.aspx.cs along with Default.aspx.cs then compiler does not
see it.
I can put rubbish inside of it and I will get no compilation error. If
I put it into App_code then this class will be compiled into seperate
assembly and won't be merged with Default.aspx.cs. Any idea?

cheers

Pawel
 
J

Juan T. Llibre

P

pawel.pabich

Ok, maybe I haven't been precise enough. I marked them both with
partial keyword.
Anyway I enclose their content:

// First file Default.aspx.cs
public partial class _Default : System.Web.UI.Page
{


protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
}
}

// second file Default2.aspx.cs
public partial class _Default : System.Web.UI.Page
{
protected override void OnLoadComplete(EventArgs e)
{
base.OnLoadComplete(e);
}

}
 
S

Scott Allen

The compilation model has changed such that the code- behind files do
not compile at the same time. In 1.x we would have compiled them all
together into one assembly in the bin directory.

The runtime is going to generate code for the ASPX file, and compile
that generated code (which includes one partial class) with the source
code referenced by the CodeFile attribute in your @ Page directive.
Any other code files will be ignored when that happens, and it's
possible that the code for each web form will end up in a distinct
assembly.
 
P

pawel.pabich

It means that I can not use "my own" partial classes in asp.net?
I can compile every directory as separate assembly but
it doesn't help because my second class is still ignored.

anyway thank for info

Gawel
 
K

Kevin Spencer

You can write your own partial classes all day long with the .Net Platform
2.0.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Neither a follower nor a lender be.
 
P

pawel.pabich

Hajo,

Kevin, did you read the whole thread?
It is about .NET 2.0 and in short that I can not use more then one code
behind files.
I would like to have my code behind in two files. Do you know how to
achive this effect ?
Gawel
 
K

Kevin Spencer

See Juan's reply.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Neither a follower nor a lender be.
 
S

Scott Allen

I believe Pawel is trying to split his code-behind partial class
across multiple files. As far as I know this isn't possible, unless
the CodeFile attribute lets you reference multiple files for an ASPX
page. The runtime only compile the partial class it creates, and the
one partial class referenced by CodeFile - everything else will be
left out in the cold.
 
J

Juan T. Llibre

re:
I believe Pawel is trying to split his code-behind partial class
across multiple files.

Maybe we're geting bogged down in semantics here but,
isn't that what the images I sent show ?

person.birth.cs and person.name.cs are
getting compiled into a single *person class*.

Of course, you can't have 2 codebehind pages for the *same* page,
but the objective isn't to split the codebehind like that, so that UI
and codebehind get mixed.

That will not be possible and is not an objective.

The objective is to "containerize" different parts of the same class
into different files, so that -for example- different parts of the same
class can be worked on by different developers without risk of
overwriting code.

See http://msdn.microsoft.com/msdnmag/issues/04/05/C20/default.aspx

From that article :

C# 1.1 requires you to put all the code for a class in a single file.

C# 2.0 allows you to split the definition and implementation
of a class or a struct across multiple files.

You can put one part of a class in one file and another part of the class
in a different file, noting the split by using the new partial keyword.

In fact, you can have as many parts as you like in any given class. Partial type support
is available for classes, structures, and interfaces, but you cannot have a partial enum
definition.

Partial types are a very handy feature. Sometimes it is necessary to modify
a machine-generated file, such as a Web service client-side wrapper class.

However, changes made to the file will be lost if you regenerate the wrapper class.
Using a partial class, you can factor those changes into a separate file.

ASP.NET 2.0 uses partial classes for the code-beside class
(the evolution of codebehind), storing the machine-generated part of the page separately.

C# 2.0 supports partial types as follows: when the compiler builds the assembly,
it combines from the various files the parts of a type and compiles them into a
single type in Microsoft intermediate language (MSIL).

The generated MSIL has no recollection which part came from which file.

Just like in C# 1.1 the MSIL has no record of which file was used to define which type.

Also worth noting is that partial types cannot span assemblies, and that a type can refuse
to have other parts by omitting the partial qualifier from its definition.

Because all the compiler is doing is accumulating parts, a single file can contain
multiple parts, even of the same type, although the usefulness of that is questionable.

In C#, developers often name a file after the class it contains
and avoid putting multiple classes in the same file.

When using partial types, I recommend indicating in the file name that it contains
parts of a type such as MyClassP1.cs, MyClassP2.cs, or employing some
other consistent way of externally indicating the content of the source file.
end of article quote...

It's the *class* which can be, and gets, split into multiple files,
not the page's codebehind.



Juan T. Llibre
ASP.NET MVP
ASP.NET FAQ : http://asp.net.do/faq/
==========================
 
P

pawel.pabich

Ok, in short I can't. That's all. But for me it is a little bit strange
because codebehind is just form of partial class and therefore
I should have been able to split my code into many files. But it seems
that it's limitation of ASP.NET.

anyway thanks for hot :) discussion

Pawel Pabich
 

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

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,286
Latest member
ChristieSo

Latest Threads

Top