Cookie in class

Discussion in 'ASP .Net' started by hansiman, Jun 11, 2004.

  1. hansiman

    hansiman Guest

    When I use the sub code below directly in the code behind page it
    works. However, if I move the code into a sub and place the sub in a
    class file I get a building error "Name 'Response' is not declared."

    I figure it has to do with the imports .... but I have tried both
    system.web.ui and a couple of others...

    Please help.

    /morten


    Public Sub addCookie(ByVal sName As String, ByVal sValue As String,
    ByVal dExpiry As DateTime)
    Dim cookie As HttpCookie = New HttpCookie(sName)
    cookie.Value = sValue
    cookie.Expires = dExpiry
    Response.Cookies.Add(cookie)
    End Sub
     
    hansiman, Jun 11, 2004
    #1
    1. Advertising

  2. The Response object is a member of the HttpContext class, which is a member
    of the Page class. That is why you can access it directly in your Page code.
    Every HTTP Request causes ASP.Net to create an HttpContext class, which is
    passed to the HttpHandler (in this case, an ASPX Page class). It contains
    all the elements vital to reading the Request, forming and sending the
    Response, and access to the Server. Fortunately, when a class is being used
    by an HttpHandler (such as a Page), it can access the entire contents of the
    HttpContext, by calling upon System.Web.HttpContext.Current. Among these
    static members is the Response. So, in your class, you can refer to
    System.Web.HttpContext.Current.Response.

    --
    HTH,
    Kevin Spencer
    ..Net Developer
    Microsoft MVP
    Big things are made up
    of lots of little things.

    "hansiman" <> wrote in message
    news:...
    > When I use the sub code below directly in the code behind page it
    > works. However, if I move the code into a sub and place the sub in a
    > class file I get a building error "Name 'Response' is not declared."
    >
    > I figure it has to do with the imports .... but I have tried both
    > system.web.ui and a couple of others...
    >
    > Please help.
    >
    > /morten
    >
    >
    > Public Sub addCookie(ByVal sName As String, ByVal sValue As String,
    > ByVal dExpiry As DateTime)
    > Dim cookie As HttpCookie = New HttpCookie(sName)
    > cookie.Value = sValue
    > cookie.Expires = dExpiry
    > Response.Cookies.Add(cookie)
    > End Sub
    >
     
    Kevin Spencer, Jun 11, 2004
    #2
    1. Advertising

  3. No it is not with the imports, it is connected with the context within the
    current sub is executing
    i whould suggest you to pass the current HttpResponse as a parametter to the
    sub

    Regards
    Martin

    "hansiman" <> wrote in message
    news:...
    > When I use the sub code below directly in the code behind page it
    > works. However, if I move the code into a sub and place the sub in a
    > class file I get a building error "Name 'Response' is not declared."
    >
    > I figure it has to do with the imports .... but I have tried both
    > system.web.ui and a couple of others...
    >
    > Please help.
    >
    > /morten
    >
    >
    > Public Sub addCookie(ByVal sName As String, ByVal sValue As String,
    > ByVal dExpiry As DateTime)
    > Dim cookie As HttpCookie = New HttpCookie(sName)
    > cookie.Value = sValue
    > cookie.Expires = dExpiry
    > Response.Cookies.Add(cookie)
    > End Sub
    >
     
    Martin Marinov, Jun 11, 2004
    #3
  4. hansiman

    Patrice Guest

    When your code is not in a Page, you can use the System.web.httpContext
    class to gain access to the current http request...

    Patrice

    --

    "hansiman" <> a écrit dans le message de
    news:...
    > When I use the sub code below directly in the code behind page it
    > works. However, if I move the code into a sub and place the sub in a
    > class file I get a building error "Name 'Response' is not declared."
    >
    > I figure it has to do with the imports .... but I have tried both
    > system.web.ui and a couple of others...
    >
    > Please help.
    >
    > /morten
    >
    >
    > Public Sub addCookie(ByVal sName As String, ByVal sValue As String,
    > ByVal dExpiry As DateTime)
    > Dim cookie As HttpCookie = New HttpCookie(sName)
    > cookie.Value = sValue
    > cookie.Expires = dExpiry
    > Response.Cookies.Add(cookie)
    > End Sub
    >
     
    Patrice, Jun 11, 2004
    #4
  5. hansiman

    Raterus Guest

    The best way to handle this is for your class to expose a function that returns a cookie to the calling sub/function, then your main page actually sets the cookie. This is the object orientated approach.

    "hansiman" <> wrote in message news:...
    > When I use the sub code below directly in the code behind page it
    > works. However, if I move the code into a sub and place the sub in a
    > class file I get a building error "Name 'Response' is not declared."
    >
    > I figure it has to do with the imports .... but I have tried both
    > system.web.ui and a couple of others...
    >
    > Please help.
    >
    > /morten
    >
    >
    > Public Sub addCookie(ByVal sName As String, ByVal sValue As String,
    > ByVal dExpiry As DateTime)
    > Dim cookie As HttpCookie = New HttpCookie(sName)
    > cookie.Value = sValue
    > cookie.Expires = dExpiry
    > Response.Cookies.Add(cookie)
    > End Sub
    >
     
    Raterus, Jun 11, 2004
    #5
  6. "hansiman" <> wrote in message
    news:...
    > When I use the sub code below directly in the code behind page it
    > works. However, if I move the code into a sub and place the sub in a
    > class file I get a building error "Name 'Response' is not declared."
    >
    > I figure it has to do with the imports .... but I have tried both
    > system.web.ui and a couple of others...
    >
    > Please help.
    >
    > /morten
    >
    >
    > Public Sub addCookie(ByVal sName As String, ByVal sValue As String,
    > ByVal dExpiry As DateTime)
    > Dim cookie As HttpCookie = New HttpCookie(sName)
    > cookie.Value = sValue
    > cookie.Expires = dExpiry
    > Response.Cookies.Add(cookie)
    > End Sub


    "Response" is short for System.Web.UI.Page.Response. On an aspx page, you
    can refer to it as "Response" because the page derives from
    System.Web.UI.Page.

    Your new class does not derive from System.Web.UI.Page (and it shouldn't),
    so you need to find the response object you want. If your sub was called
    from a page, you will find the response object at
    System.Web.HttpContext.Current.Response.
    --
    John Saunders
    johnwsaundersiii at hotmail
     
    John Saunders, Jun 11, 2004
    #6
  7. hansiman

    hansiman Guest

    Aha.... Thanks :)

    On Fri, 11 Jun 2004 09:16:56 -0400, "John Saunders"
    <> wrote:

    >"hansiman" <> wrote in message
    >news:...
    >> When I use the sub code below directly in the code behind page it
    >> works. However, if I move the code into a sub and place the sub in a
    >> class file I get a building error "Name 'Response' is not declared."
    >>
    >> I figure it has to do with the imports .... but I have tried both
    >> system.web.ui and a couple of others...
    >>
    >> Please help.
    >>
    >> /morten
    >>
    >>
    >> Public Sub addCookie(ByVal sName As String, ByVal sValue As String,
    >> ByVal dExpiry As DateTime)
    >> Dim cookie As HttpCookie = New HttpCookie(sName)
    >> cookie.Value = sValue
    >> cookie.Expires = dExpiry
    >> Response.Cookies.Add(cookie)
    >> End Sub

    >
    >"Response" is short for System.Web.UI.Page.Response. On an aspx page, you
    >can refer to it as "Response" because the page derives from
    >System.Web.UI.Page.
    >
    >Your new class does not derive from System.Web.UI.Page (and it shouldn't),
    >so you need to find the response object you want. If your sub was called
    >from a page, you will find the response object at
    >System.Web.HttpContext.Current.Response.
     
    hansiman, Jun 12, 2004
    #7
    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
    Replies:
    3
    Views:
    5,937
    Steven Cheng[MSFT]
    Jun 3, 2004
  2. Shapper

    Cookie and Session Cookie Questions.

    Shapper, Apr 27, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    613
  3. =?Utf-8?B?UGF1bA==?=

    Cookie Question (IP as domain and cookie file location)

    =?Utf-8?B?UGF1bA==?=, Jan 10, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    2,541
    Bruce Barker
    Jan 10, 2006
  4. ad
    Replies:
    2
    Views:
    7,683
    Kevin Spencer
    Jan 27, 2006
  5. E11
    Replies:
    1
    Views:
    4,958
    Thomas Weidenfeller
    Oct 12, 2005
Loading...

Share This Page