Sharing Behind-page-code

Discussion in 'ASP .Net' started by Mr Flibble, Jun 6, 2006.

  1. Mr Flibble

    Mr Flibble Guest

    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!
    Mr Flibble, Jun 6, 2006
    #1
    1. Advertising

  2. 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

    --
    http://www.openmymind.net/
    http://www.fuelindustries.com/


    "Mr Flibble" <> wrote in message
    news:e63u27$1vvo$...
    >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!
    Karl Seguin [MVP], Jun 6, 2006
    #2
    1. Advertising

  3. Mr Flibble

    Mr Flibble Guest

    * Karl Seguin [MVP] wrote:
    > 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 ;-)
    Mr Flibble, Jun 6, 2006
    #3
  4. Mr Flibble wrote:

    > * Karl Seguin [MVP] wrote:
    > > 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 ;-)


    You need to reference System.Web to get to
    HttpContext.Current.Response.Write for non-code behind files.
    ^MisterJingo^, Jun 6, 2006
    #4
  5. Mr Flibble

    Ray Booysen Guest

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

    Mr Flibble wrote:
    > * Karl Seguin [MVP] wrote:
    >> 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 ;-)
    Ray Booysen, Jun 6, 2006
    #5
  6. Mr Flibble

    Mr Flibble Guest

    * ^MisterJingo^ wrote:
    > 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?
    Mr Flibble, Jun 6, 2006
    #6
  7. "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

    --
    http://www.openmymind.net/
    http://www.fuelindustries.com/


    "Mr Flibble" <> wrote in message
    news:e645tb$2b6p$...
    >* ^MisterJingo^ wrote:
    >> 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?
    >
    >
    Karl Seguin [MVP], Jun 6, 2006
    #7
  8. re:
    > basically I have to use HttpContext.Current.Response.Write
    > rather than Response.Write?


    Correct...



    Juan T. Llibre, asp.net MVP
    aspnetfaq.com : http://www.aspnetfaq.com/
    asp.net faq : http://asp.net.do/faq/
    foros de asp.net, en espaƱol : http://asp.net.do/foros/
    ===================================
    "Mr Flibble" <> wrote in message
    news:e645tb$2b6p$...
    >* ^MisterJingo^ wrote:
    >> 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?
    >
    >
    Juan T. Llibre, Jun 6, 2006
    #8
  9. Mr Flibble

    Mr Flibble Guest

    * Karl Seguin [MVP] wrote:
    > "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. :)
    Mr Flibble, Jun 6, 2006
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Miller [msft]

    Re: Code Behind vs. no code behind: error

    Ben Miller [msft], Jun 27, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    554
    Alphonse Giambrone
    Jun 28, 2003
  2. =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?=

    Fire Code behind code AND Javascript code associated to a Button Click Event

    =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?=, Feb 10, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    21,151
    =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?=
    Feb 11, 2004
  3. Daniel Manes

    Code-Behind Pain in the Behind!

    Daniel Manes, Jun 10, 2005, in forum: ASP .Net
    Replies:
    11
    Views:
    632
  4. keithb
    Replies:
    1
    Views:
    882
    Bruce Barker
    Mar 29, 2006
  5. John

    Non-code behind to code behind

    John, Feb 19, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    451
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Feb 19, 2007
Loading...

Share This Page