472,779 Members | 1,987 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Common Code Class + Session + Request + Response

Hello,

(ASP.NET c#)
I want to put some common functions into one class and within this class I
need to have access to the Session, Request and Response. This class is not
page behind file!
I keep trying all sorts of namespaces, but to no avail. Keep getting:
"The name 'Session' does not exist in the class or namespace 'xyz xyz xyz'"
and "The type or namespace name 'Page' could not be found (are you missing a
using directive or an assembly reference?)"

Obviously I need to pass in information about the page where the function is
called from, but can't find a way of doing this.

Any help would be appreciated.

Nov 18 '05 #1
5 2566
Use System.Web.httpContext.Current.

The code behind is in a class that inherits from the Page class that
provides you with the current HTTP context. Outside of such a page using
the property above allows you to get access to the current request context.

Example of use from some non page or non code-behind class file:
int currnetID =
Convert.ToInt32(System.Web.HttpContext.Current.Ses sion["userID"])

EXPLANATION (cut and pasted from a response by the very helpful Kevin
Spencer from March 26 on the same topic):
The System.Web.UI.Page class is an HttpHandler; that is, it implements the
IHttpHandler Interface, because its purpose is to handle HTTP Requests. The
HttpHandler is given the entire context in which the HTTP Request occurs
(This is called the System.Web.HttpContext.Current object). The HttpContext
contains the Request, Response, Session, Application, and other elements of
the HttpContext of the current Request as Properties, in order to respond.
The HttpContext of the current Request is therefore, a member by default of
the Page class. In custom classes, you need to get a handle on the Current
HttpContext (which will only occur in the context of an HttpRequest) in
order to access these objects. This can be done by referring to the
System.Web.HttpContext.Current object. Once you've done that, you can refer
to the Application, for example, as
System.Web.HttpContext.Current.Application.

Good Luck

"Filip" <fi******@hotmail.com> wrote in message
news:ux*************@TK2MSFTNGP10.phx.gbl...
Hello,

(ASP.NET c#)
I want to put some common functions into one class and within this class I
need to have access to the Session, Request and Response. This class is not page behind file!
I keep trying all sorts of namespaces, but to no avail. Keep getting:
"The name 'Session' does not exist in the class or namespace 'xyz xyz xyz'" and "The type or namespace name 'Page' could not be found (are you missing a using directive or an assembly reference?)"

Obviously I need to pass in information about the page where the function is called from, but can't find a way of doing this.

Any help would be appreciated.

Nov 18 '05 #2
Hi,

you should use HttpContext.Current.XXX

XXX = Request, rESPONSE, Session ...

HTH

Natty Gur[MVP]

blog : http://weblogs.asp.net/ngur
Mobile: +972-(0)58-888377
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 18 '05 #3
Thanks Ketchup
Nov 18 '05 #4
Thanks Natty Gur
Nov 18 '05 #5
Hi ,

If you are creating this class separately from web project, then import
system.web in your project(if you dont have system.web reference, then add
that too). Then access your session variable using
HttpContext.Current.Session
--
Saravana
Microsoft MVP - ASP.NET
www.extremeexperts.com

"Filip" <fi******@hotmail.com> wrote in message
news:ux*************@TK2MSFTNGP10.phx.gbl...
Hello,

(ASP.NET c#)
I want to put some common functions into one class and within this class I
need to have access to the Session, Request and Response. This class is not page behind file!
I keep trying all sorts of namespaces, but to no avail. Keep getting:
"The name 'Session' does not exist in the class or namespace 'xyz xyz xyz'" and "The type or namespace name 'Page' could not be found (are you missing a using directive or an assembly reference?)"

Obviously I need to pass in information about the page where the function is called from, but can't find a way of doing this.

Any help would be appreciated.

Nov 18 '05 #6

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

Similar topics

11
by: doltharz | last post by:
Please Help me i'm doing something i though was to be REALLY EASY but it drives me crazy The complete code is at the end of the email (i mean newsgroup article), i always use Option...
0
by: collie | last post by:
Hi, I have an asp page that needs to create 2 different cookies: one for the admin and one for the user. The code that I have to work with was created by someone else. the page first requires to...
5
by: wrecker | last post by:
Hi all, I have a few common methods that I need to use at different points in my web application. I'm wondering where the best place would be to put these? I think that I have three options. ...
1
by: Nazim | last post by:
I have been facing an issue for past week. Kindly look at the code if some one can sort it out. Issue is with the code. I have this ASP code. it connects to a server where java is running. It...
0
by: TRB_NV | last post by:
I'd been using an Access database based shopping cart, but wanted to change it so that it would use session variables. I have a form that's submitted to a page called addtocart.asp that contains...
4
by: Joseph Geretz | last post by:
We use a Soap Header to pass a token class (m_Token) back and forth with authenticated session information. Given the following implementation for our Logout method, I vastly prefer to simply code...
0
by: karen987 | last post by:
Please could someone help me with this code? The problem appears to be a simple one, but i'm not html or ASP literate and need to modify the below code to add 2 things. First a bit about the...
5
by: lyealain | last post by:
<% If Session("username") = "" Then Response.Redirect("/CLS/Login.asp") End If Dim conn Dim connectstr Dim db_name, db_username, db_userpassword Dim db_server Dim res
3
by: dihola | last post by:
Hi, I have a website running in IIS7 and it seems to be creating a new session for every request I make. The values I store in Session are lost with every request. This is the forms bit in my...
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?

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.