473,395 Members | 1,756 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Cookie in class

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

Nov 18 '05 #1
6 1342
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" <ha***@hotmail.com> wrote in message
news:d3********************************@4ax.com...
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

Nov 18 '05 #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" <ha***@hotmail.com> wrote in message
news:d3********************************@4ax.com...
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

Nov 18 '05 #3
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" <ha***@hotmail.com> a écrit dans le message de
news:d3********************************@4ax.com...
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

Nov 18 '05 #4
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" <ha***@hotmail.com> wrote in message news:d3********************************@4ax.com...
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

Nov 18 '05 #5
"hansiman" <ha***@hotmail.com> wrote in message
news:d3********************************@4ax.com...
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
Nov 18 '05 #6
Aha.... Thanks :)

On Fri, 11 Jun 2004 09:16:56 -0400, "John Saunders"
<jo**************@notcoldmail.com> wrote:
"hansiman" <ha***@hotmail.com> wrote in message
news:d3********************************@4ax.com.. .
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.


Nov 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Phil Powell | last post by:
URL: http://valsignalandet.com/cookiegrab.php?name=valIdentifier This page produces a cookie value and sends it back as HTML using PHP and Javascript to obtain it. This URL will be used as the...
1
by: RCHENG | last post by:
Hi guys, I am pretty new to network programming with c#, and is having problem using cookie with HTTPWEBREQUEST object. My question involves sending a request with cookie. What I am trying to do...
3
by: Karsten Grombach | last post by:
Hi, I'm trying the following: - Imitate a Logon using a Post with HttpWebRequest on remote Webserver (asp 3.0 page using https) - On success redirect to the page (encapsuled in an iframe)...
6
by: Jason Collins | last post by:
There seems to be an inconsistency (bug?) in the way the Set-Cookie header is handled by the WebHeaderCollection. That is, the values of Set-Cookie, when an Expires is specified, contain the ","...
2
by: Shawn Berg | last post by:
Some of my pages in the app I am developing inherit from a BasePage class I have created. I have done plenty of these in the past and they work fine. Now, however, I have an additional requirement....
2
by: | last post by:
Today I learned that creating cookies inside of a custom class in ASP.NET 2.0 requires that you prefix it with HttpContext.Current..., e.g. : ...
1
by: flutetones | last post by:
http://67.189.52.24/~metafusionserver/public_html/training.php Here is a link to my server. I have an issue that doen't make sense. What's hapening is this . . . What's going right . . .
1
by: domnicx | last post by:
Hi, I am using the httpweblistener class to build a web server kinda functionality. I have to send multiple set-cookie headers in a single response when queried for a particular request and i...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.