Cookie in class

H

hansiman

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
 
K

Kevin Spencer

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

Martin Marinov

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
 
P

Patrice

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
 
R

Raterus

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

John Saunders

hansiman said:
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.
 
H

hansiman

Aha.... Thanks :)

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

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,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top