Sharing Behind-page-code

M

Mr Flibble

I have some functions in Default.aspx.cs that are accessed by
Default.aspx. I have a new page with the imaginative name of
Default2.apsx. I want to be able to call my functions i Default.aspx.cs
from this new page (as well as from the original page). I could move my
shared code to .Net bin and then use the <@ Import
Namespace="MyCustomNamespace" > at the top of each page, but I was
wondering what other options are available so that I do things the
Asp.Net way and which is the most advised. I'm using .Net 2.0 and
Asp.Net 2.0.

Any suggestions warmly appreciated!
 
K

Karl Seguin [MVP]

Creating a separate class is the only good idea. You can simply reference it
and make the members static (if that makes sense in this case) or you can
create a base-page and have your pages inherit from it.

Karl
 
M

Mr Flibble

* Karl Seguin said:
Creating a separate class is the only good idea. You can simply reference it
and make the members static (if that makes sense in this case) or you can
create a base-page and have your pages inherit from it.

Karl

I've created a seperate class and called it Common.cs. I've then put it
in the app_code folder. However since moving the code from the
Default.aspx.cs class to the Common.cs class it seems I'm unable to
access the Response object.

I get:

The name 'Response' does not exist in the current context.

Does that mean that shared code can only be code that doesn't alter the
HttpResponse ?

In which case I'm up s**t creak without a paddle ;-)
 
M

^MisterJingo^

Mr said:
I've created a seperate class and called it Common.cs. I've then put it
in the app_code folder. However since moving the code from the
Default.aspx.cs class to the Common.cs class it seems I'm unable to
access the Response object.

I get:

The name 'Response' does not exist in the current context.

Does that mean that shared code can only be code that doesn't alter the
HttpResponse ?

In which case I'm up s**t creak without a paddle ;-)

You need to reference System.Web to get to
HttpContext.Current.Response.Write for non-code behind files.
 
R

Ray Booysen

If you need to access the Response object create a class that your pages
can inherit from. In the class place this shared functionality.
 
M

Mr Flibble

* ^MisterJingo^ said:
You need to reference System.Web to get to
HttpContext.Current.Response.Write for non-code behind files.

Ah OK, so basically I have to use HttpContext.Current.Response.Write
rather than Response.Write?
 
K

Karl Seguin [MVP]

"Response" is available to you in your codebehind, because the Page class
(which you inherit from) basically has a property:

protected HttpResponse Response
{
get { return HttpContext.Current.Response; }
}

i.e., it creates a shortcut for you....

since you aren't inheriting from Page, you've lost the shortcut

Karl
 
M

Mr Flibble

* Karl Seguin said:
"Response" is available to you in your codebehind, because the Page class
(which you inherit from) basically has a property:

protected HttpResponse Response
{
get { return HttpContext.Current.Response; }
}

i.e., it creates a shortcut for you....

since you aren't inheriting from Page, you've lost the shortcut

Karl

Thank you for your excellent explanation. :)
 

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,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top