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

Auto-Comilation of .CS classes???

P: n/a
I love that I can create ASPX and ASCX files with CS code embedded or in code-behind files and ASP.NET will auto-compile it all for me. I *like* it that way

Is there ANY way to have classes defined in .cs files somewhere and have them accessed by my ASPX, ASCX, and Code-Behind files WITHOUT having to manually compile them first???
Nov 18 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a
Hi,

Based my understanding, does the problem you mentioned as:
"Is there ANY way to have classes defined in .cs files somewhere and have
them accessed by my ASPX, ASCX, and Code-Behind files WITHOUT having to
manually compile them first???"

means that you just want to provide a certain .cs codebehind class's source
file for a aspx page or ascx control without compiling the cs file into
assembly and let them be compiled at runtime when the certain page or ascx
control is requested, yes?

If so, I think you can use the "Src" attribute in the @Page or @Control
directive, for example:
<%@ Page language="c#" Src="xxx.cs" >
<%@ Control Language="c#" Src="xx.cs" >

The "Src" will make the code-behind class files be compiled on demand
instead of precompiled. And
the "Src" attribute is listed with the relative path of the code-behind
class file (SrcSample.aspx.cs), and that the Inherits attribute value is
set to reference the certain class(classname with full namespace). In
addition, when you use the "Src" attribute to make the code-behind class
compiled at runtime on demand, you must make sure no precompiled classes in
the existing assemblies in the web application's reference path, othewise,
you'll encounter some unexpected errors. For detailed info, please refer to
the following kb articles:

#INFO: ASP.NET Code-Behind Model Overview
http://support.microsoft.com/?id=303247

#HOW TO: Work with Code-Behind Class Files in an ASP.NET Application by
Using Visual C# .NET
http://support.microsoft.com/?id=308143

Regards,

Steven Cheng
Microsoft Online Support

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

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx

Nov 18 '05 #2

P: n/a
Steven,

Not to speak for Alex :) but, I believe his problem is similar to mine
(which is posted a few hours ago as subj: JIT assemblies)...

Basically, the problem is not using "compile on demand" (via specifying
SRC), it's using _other_ "compile on demand" classes from the code behind
class. In short, I can do this in the .aspx page simply by using a
@Assembly directive (src="mytestfile.cs") ... but, how to do this from the
CodeBehind? Is there a way to build a reference to the .cs file?

To copy my example below, assume the following testclass.cs file, that is
compiled on demand:

namespace testnamespace
{
public class testclass
{
public static string teststring = "contents of teststring";
public testclass()
{
}
}
}

If I have a page w/o a code behind, I can access this class using:
<%@ Assembly Src="testclass.cs" %>
....
<%
Response.Write(testnamespace.testclass.teststring) ;
%>

Can you show me how to use this class from a code behind (that is compiled
on demand)?

Brian


"Steven Cheng[MSFT]" <v-******@online.microsoft.com> wrote in message
news:bx**************@cpmsftngxa06.phx.gbl...
Hi,

Based my understanding, does the problem you mentioned as:
"Is there ANY way to have classes defined in .cs files somewhere and have
them accessed by my ASPX, ASCX, and Code-Behind files WITHOUT having to
manually compile them first???"

means that you just want to provide a certain .cs codebehind class's source file for a aspx page or ascx control without compiling the cs file into
assembly and let them be compiled at runtime when the certain page or ascx
control is requested, yes?

If so, I think you can use the "Src" attribute in the @Page or @Control
directive, for example:
<%@ Page language="c#" Src="xxx.cs" >
<%@ Control Language="c#" Src="xx.cs" >

The "Src" will make the code-behind class files be compiled on demand
instead of precompiled. And
the "Src" attribute is listed with the relative path of the code-behind
class file (SrcSample.aspx.cs), and that the Inherits attribute value is
set to reference the certain class(classname with full namespace). In
addition, when you use the "Src" attribute to make the code-behind class
compiled at runtime on demand, you must make sure no precompiled classes in the existing assemblies in the web application's reference path, othewise,
you'll encounter some unexpected errors. For detailed info, please refer to the following kb articles:

#INFO: ASP.NET Code-Behind Model Overview
http://support.microsoft.com/?id=303247

#HOW TO: Work with Code-Behind Class Files in an ASP.NET Application by
Using Visual C# .NET
http://support.microsoft.com/?id=308143

Regards,

Steven Cheng
Microsoft Online Support

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

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx

Nov 18 '05 #3

P: n/a
Steve

So close, but not quite what I meant. I know how to have ASP.NET automatically compile the code-behind .cs filr for my ASPX or ASCX. My problem is that I want to create a few utility classes that are used around my site and called from within some of those code-behing .cs pages, etc. But I don't want to have to pre-compile those utility classes. Do I absolutely HAVE to precompile a .CS stand-alone class to use it from other .ASPX, .ASCX, and Code-Behind .CS files

And if I *do* have to pre-compile them, I'm just curious as to why I have to when I don't have to manually pre-compile the ASPXs and their code-behind .CS files

Thanks

Ale

----- Steven Cheng[MSFT] wrote: ----

Hi

Based my understanding, does the problem you mentioned as
"Is there ANY way to have classes defined in .cs files somewhere and have
them accessed by my ASPX, ASCX, and Code-Behind files WITHOUT having to
manually compile them first???

means that you just want to provide a certain .cs codebehind class's source
file for a aspx page or ascx control without compiling the cs file into
assembly and let them be compiled at runtime when the certain page or ascx
control is requested, yes

If so, I think you can use the "Src" attribute in the @Page or @Control
directive, for example
<%@ Page language="c#" Src="xxx.cs" ><%@ Control Language="c#" Src="xx.cs"

The "Src" will make the code-behind class files be compiled on demand
instead of precompiled. An
the "Src" attribute is listed with the relative path of the code-behind
class file (SrcSample.aspx.cs), and that the Inherits attribute value is
set to reference the certain class(classname with full namespace). In
addition, when you use the "Src" attribute to make the code-behind class
compiled at runtime on demand, you must make sure no precompiled classes in
the existing assemblies in the web application's reference path, othewise,
you'll encounter some unexpected errors. For detailed info, please refer to
the following kb articles

#INFO: ASP.NET Code-Behind Model Overvie
http://support.microsoft.com/?id=30324

#HOW TO: Work with Code-Behind Class Files in an ASP.NET Application by
Using Visual C# .NE
http://support.microsoft.com/?id=30814

Regards

Steven Chen
Microsoft Online Suppor

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

Get Preview at ASP.NET whidbe
http://msdn.microsoft.com/asp.net/whidbey/default.asp
Nov 18 '05 #4

P: n/a
Hi Alex,

Thanks for the response. I've noticed that this issue is related with
another one whose titile is
"Accessing Parent Page members from a UserControl"

I've posted my reply there and provided my suggestions on this issue. Since
the classes imported in a certain source file need to be contained in a
compiled assembly so that the certain util assembly can reference it. I'm
afraid you need to make the "BasePage" recompiled. And as for the pages'
"Src" attribute, we have to specify another page class rather than the
"BasePage", maybe a class derviced from the "BasePage" Class. Please have a
look and if you have any other concerns, please feel free to post here.
Thanks.

Regards,

Steven Cheng
Microsoft Online Support

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

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx

Nov 18 '05 #5

P: n/a
Steven could you answer my question above in this thread and below (under
JIT assemblies)? I'm dying for an answer on this one :)

This solution somewhat answers it, but not exactly. It must be technically
feasible, though perhaps not possible, to reference JIT classes from other
JIT classes....

-Brian

"Steven Cheng[MSFT]" <v-******@online.microsoft.com> wrote in message
news:4L*************@cpmsftngxa06.phx.gbl...
Hi Alex,

Thanks for the response. I've noticed that this issue is related with
another one whose titile is
"Accessing Parent Page members from a UserControl"

I've posted my reply there and provided my suggestions on this issue. Since the classes imported in a certain source file need to be contained in a
compiled assembly so that the certain util assembly can reference it. I'm
afraid you need to make the "BasePage" recompiled. And as for the pages'
"Src" attribute, we have to specify another page class rather than the
"BasePage", maybe a class derviced from the "BasePage" Class. Please have a look and if you have any other concerns, please feel free to post here.
Thanks.

Regards,

Steven Cheng
Microsoft Online Support

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

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx

Nov 18 '05 #6

P: n/a
Steve (and Brian)

Brian has it *exactly* right. For the moment, let's re-state the question like this

You have three files
1. AnyPage.asp
2. AnyPageCodeBehind.c
3. UtilClass.c

AnyPage.aspx has an "<%@Page" directive setting up "AnyPageCodeBehind.cs" as the code-behind source for AnyPage.asp

INSIDE the AnyPageCodeBehind.cs file (as well as from other .cs files around my site), I want to create and use the class and members from the UtilClass.cs file

THE QUESTION IS, DO I *HAVE* TO PRE-COMPILE UtilClass.cs IN ORDER TO ACCESS ITS CLASSES AND MEMBERS FROM OTHER .CS FILES OR IS THERE *ANY* WAY AT ALL TO REFERENCE IT FROM A .CS FILE SO THAT IT WILL BE AUTO-COMPILED??

Hope this clears up the question

Ale

----- Brian H wrote: ----

Steven

Not to speak for Alex :) but, I believe his problem is similar to min
(which is posted a few hours ago as subj: JIT assemblies)..

Basically, the problem is not using "compile on demand" (via specifyin
SRC), it's using _other_ "compile on demand" classes from the code behin
class. In short, I can do this in the .aspx page simply by using
@Assembly directive (src="mytestfile.cs") ... but, how to do this from th
CodeBehind? Is there a way to build a reference to the .cs file

To copy my example below, assume the following testclass.cs file, that i
compiled on demand

namespace testnamespac

public class testclas

public static string teststring = "contents of teststring"
public testclass(

If I have a page w/o a code behind, I can access this class using
<%@ Assembly Src="testclass.cs" %
...
<
Response.Write(testnamespace.testclass.teststring)
%

Can you show me how to use this class from a code behind (that is compile
on demand)

Bria


"Steven Cheng[MSFT]" <v-******@online.microsoft.com> wrote in messag
news:bx**************@cpmsftngxa06.phx.gbl..
Hi
Based my understanding, does the problem you mentioned as "Is there ANY way to have classes defined in .cs files somewhere and hav
them accessed by my ASPX, ASCX, and Code-Behind files WITHOUT having t
manually compile them first???
means that you just want to provide a certain .cs codebehind class'

sourc file for a aspx page or ascx control without compiling the cs file int
assembly and let them be compiled at runtime when the certain page or asc
control is requested, yes
If so, I think you can use the "Src" attribute in the @Page or @Contro directive, for example
<%@ Page language="c#" Src="xxx.cs" >><%@ Control Language="c#" Src="xx.cs" >>> The "Src" will make the code-behind class files be compiled on deman
instead of precompiled. An
the "Src" attribute is listed with the relative path of the code-behin
class file (SrcSample.aspx.cs), and that the Inherits attribute value i
set to reference the certain class(classname with full namespace). I
addition, when you use the "Src" attribute to make the code-behind clas
compiled at runtime on demand, you must make sure no precompiled classe

i the existing assemblies in the web application's reference path, othewise
you'll encounter some unexpected errors. For detailed info, please refe t the following kb articles
#INFO: ASP.NET Code-Behind Model Overvie

http://support.microsoft.com/?id=30324
#HOW TO: Work with Code-Behind Class Files in an ASP.NET Application b

Using Visual C# .NE
http://support.microsoft.com/?id=30814
Regards
Steven Chen

Microsoft Online Suppor
Get Secure! www.microsoft.com/securit

(This posting is provided "AS IS", with no warranties, and confers n
rights.
Get Preview at ASP.NET whidbe

http://msdn.microsoft.com/asp.net/whidbey/default.asp

Nov 18 '05 #7

P: n/a
Hi Alex,

Thanks for the reply and the restate of the problem.
I've got that your questions is actually focus on
"How to make a certain cs file be referenced without recompiled and will be
compiled at runtime when requested"

As for this problem, I think the certain cs file need to be precompiled
into assembly first. Because you reference it in the codebehind cs file
rather than an aspx page. In other word, it is just like we reference a
class in assembly B from a class in assembly A. If the assembly B's source
code is not precompiled into assembly, when the assembly A is compiling ,
it'll occur error finding the certain reference. And as for the "Src"
attribute of the @Page directive which can cause the codebehind class be
auto-compiled at runtime, this is a certain feature provided by
ASP.NET(when a PAGE is requested) only not for common .net senario. So if
we want to reference a certain class from a external assembly(separate from
the caller class's assembly), we have to precompiled it into assembly first.

In addition, would you please tell use the reason why you'd like to put the
utitlity classes non-precompiled until runtime, is it frequently changed or
for any other concerns? Thanks.

Regards,

Steven Cheng
Microsoft Online Support

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

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx

Nov 18 '05 #8

P: n/a
Hi Brian,

As far as I known, there isn't any @directive which can help make the any
referenced source file auto-compiled at runtime in a codebehind( source
file). The @Assembly or @Page diretive which can make a assembly or
codebind 's source auto-compiled is provided by the ASP.NET runtime which
also make use of the System.Runtime.CompilerServices components
internally. So I think if you'd like to reference a certain class in
codebehind, you need to have the certain precompiled assembly. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

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

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx

Nov 18 '05 #9

P: n/a
I'm feeling invisible here ... can I jump in?

The thing is, though, you can sort-of do it in ASPX page using the "assembly
src=blah.cs" directive. Blah.cs will then be compiled.

Where this is a problem, for me, is that I have several user controls. For
example, suppose I have Product.aspx and Product.aspx.cs. Suppose I have
ProductDetail.ascx and ProductDetail.ascx.cs, Alternate.ascx and
Alternate.ascx.cs. If I want to load these controls dynamically from
Product.aspx and cast them as types defined within these controls, I cannot
do it without precompiling. I can post code if it would help.

-Brian
"Steven Cheng[MSFT]" <v-******@online.microsoft.com> wrote in message
news:p5*************@cpmsftngxa06.phx.gbl...
Hi Alex,

Thanks for the reply and the restate of the problem.
I've got that your questions is actually focus on
"How to make a certain cs file be referenced without recompiled and will be compiled at runtime when requested"

As for this problem, I think the certain cs file need to be precompiled
into assembly first. Because you reference it in the codebehind cs file
rather than an aspx page. In other word, it is just like we reference a
class in assembly B from a class in assembly A. If the assembly B's source
code is not precompiled into assembly, when the assembly A is compiling ,
it'll occur error finding the certain reference. And as for the "Src"
attribute of the @Page directive which can cause the codebehind class be
auto-compiled at runtime, this is a certain feature provided by
ASP.NET(when a PAGE is requested) only not for common .net senario. So if
we want to reference a certain class from a external assembly(separate from the caller class's assembly), we have to precompiled it into assembly first.
In addition, would you please tell use the reason why you'd like to put the utitlity classes non-precompiled until runtime, is it frequently changed or for any other concerns? Thanks.

Regards,

Steven Cheng
Microsoft Online Support

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

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx

Nov 18 '05 #10

P: n/a
Thanks for the reply, Steve!

"Steven Cheng[MSFT]" <v-******@online.microsoft.com> wrote in message
news:r9**************@cpmsftngxa06.phx.gbl...
Hi Brian,

As far as I known, there isn't any @directive which can help make the any
referenced source file auto-compiled at runtime in a codebehind( source
file). The @Assembly or @Page diretive which can make a assembly or
codebind 's source auto-compiled is provided by the ASP.NET runtime which
also make use of the System.Runtime.CompilerServices components
internally. So I think if you'd like to reference a certain class in
codebehind, you need to have the certain precompiled assembly. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

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

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx

Nov 18 '05 #11

P: n/a
Hi Alex,

Have you got any further ideas on this issue? If you have anything unclear
or if there're anything else we can help, please feel free to post here.
Thanks.

Regards,

Steven Cheng
Microsoft Online Support

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

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
Nov 18 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.