By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,267 Members | 1,808 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,267 IT Pros & Developers. It's quick & easy.

Partial class in ASP.NET

P: n/a
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

Nov 19 '05 #1
Share this Question
Share on Google+
16 Replies


P: n/a
First of all, partial classes only work in ASP.NET 2.0.

If you're using ASP.NET 2.0, check out
the generic documentation for partial classes :

http://msdn2.microsoft.com/library/m...us,vs.80).aspx

Documentation specific to C# partial classes is at :
http://msdn2.microsoft.com/library/w...us,vs.80).aspx

And, for VB.NET, at :
http://msdn2.microsoft.com/library/y...us,vs.80).aspx

Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Español
Ven, y hablemos de ASP.NET...
======================

<pa**********@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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

Nov 19 '05 #2

P: n/a
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

Nov 19 '05 #3

P: n/a
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!
***************************
"pa**********@gmail.com" wrote:
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

Nov 19 '05 #4

P: n/a
re:
VS.NET seems not to see the additional class let's say Default2.aspx.cs
Default2.aspx.cs is not a class.
It's a code-behind source file.

Specific sample code to implement partial classes in C# is at :
http://msdn2.microsoft.com/library/wa80x488.aspx

Try using the sample code as provided in that page.
Then, maybe, you can adapt it to your needs.

Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Español
Ven, y hablemos de ASP.NET...
======================

<pa**********@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com... 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

Nov 19 '05 #5

P: n/a
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

Nov 19 '05 #6

P: n/a
> 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

Nov 19 '05 #7

P: n/a
re:
Default2.aspx.cs is not a class
I can not agree. If it is not a class then what is it?
Let's not get bogged down in semantics.

re:Any idea?
Before you can take advantage of the partial class compilation model,
you must *mark* the classes as partial.

Take a look at ASP.NET 2.0 Internals :
http://msdn.microsoft.com/library/de.../internals.asp

There's sample code there.
Make sure your code conforms with the sample code shown.


Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Español
Ven, y hablemos de ASP.NET...
======================

<pa**********@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com... 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

Nov 19 '05 #8

P: n/a
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);
}

}

Nov 19 '05 #9

P: n/a
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.

--
Scott
http://www.OdeToCode.com/blogs/scott/

On 1 Sep 2005 06:12:23 -0700, pa**********@gmail.com wrote:
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);
}

}


Nov 19 '05 #10

P: n/a
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

Nov 19 '05 #11

P: n/a
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.

<pa**********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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

Nov 19 '05 #12

P: n/a
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

Nov 19 '05 #13

P: n/a
See Juan's reply.

--
HTH,

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

<pa**********@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
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

Nov 19 '05 #14

P: n/a
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.

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Fri, 2 Sep 2005 11:31:14 -0400, "Juan T. Llibre"
<no***********@nowhere.com> wrote:
Pawel,

Attached are 4 images.

1. Classes-partial.jpg - shows all source files
2. Class.person.birth.jpg - shows contents of partial class person.birth.cs

public partial class person
{
public int Age;
public DateTime Birth;
}

3. Class.person.name.jpg - shows contents of partial class person.name.cs
public partial class person
{
public string FirstName;
public string LastName;
}

4. ClassView.jpg - Show *all* members of the person class :
int Age, DateTime Birth, string FirstName and string LastName

Clearly, all members in both partial classes
are seen as part of *one* class : the person class.

Partial classes *do* work in Beta 2 and later builds of VS.NET.

Juan T. Llibre
ASP.NET MVP
ASP.NET FAQ : http://asp.net.do/faq/
===========================
<pa**********@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googleg roups.com...
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



Nov 19 '05 #15

P: n/a
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/is...0/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/
==========================
"Scott Allen" <sc***@nospam.odetocode.com> wrote in message
news:8a********************************@4ax.com...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.

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Fri, 2 Sep 2005 11:31:14 -0400, "Juan T. Llibre"
<no***********@nowhere.com> wrote:
Pawel,

Attached are 4 images.

1. Classes-partial.jpg - shows all source files
2. Class.person.birth.jpg - shows contents of partial class person.birth.cs

public partial class person
{
public int Age;
public DateTime Birth;
}

3. Class.person.name.jpg - shows contents of partial class person.name.cs
public partial class person
{
public string FirstName;
public string LastName;
}

4. ClassView.jpg - Show *all* members of the person class :
int Age, DateTime Birth, string FirstName and string LastName

Clearly, all members in both partial classes
are seen as part of *one* class : the person class.

Partial classes *do* work in Beta 2 and later builds of VS.NET.

Juan T. Llibre
ASP.NET MVP
ASP.NET FAQ : http://asp.net.do/faq/
===========================
<pa**********@gmail.com> wrote in message
news:11**********************@g47g2000cwa.google groups.com...
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



Nov 19 '05 #16

P: n/a
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

Nov 19 '05 #17

This discussion thread is closed

Replies have been disabled for this discussion.