Hi, I'm new to .NET and am still trying to get my head around OO concepts.
I am building an asp.net app and on several pages i want to check to see if
a session object exists, if so - contine to show the page, if not check if
cookie exists, if no cookie then redirect to a login page.
This is easy enough, but now i want to put this functionallity in a class so
that i can just call a checkSupplierID method from that class easily from
any page
This is what i have done:
//method in tools class
public void checkSupplierIDSession()
{
if(Session["supplierID"] == null)
{
HttpCookie cookie = Request.Cookies["supplierID"];
if (cookie != null)
{
Session["supplierID"] = cookie.Value.ToString();
}
else
{
Server.Transfer("Login.aspx?callingPage=" +
this.Page.ToString().Substring(4,this.Page.ToStrin g().Length - 9));
//redirect with this page as calling page
}
}
}
code in aspx.cs code behind file on page i want to check:
private void Page_Load(object sender, System.EventArgs e)
{
tools.checkSupplierIDSession();
}
I get this error when trying to compile:
An object reference is required for the nonstatic field, method, or property
'tools.checkSupplierIDSession()'
Initially the method was static, but that meant that i couldn't use the
Session, Request, and this.Page properties on the calling page?
can someone please advise on the best way to do what i am trying to achieve?
Thanks for your help
Tim 2 1159
Hi Tim,
It's not whether it's static or not that is the problem. The problem is that
it is not a Page class, or another class which intrinsicly gets the
HttpContext. However, all you have to do is reference the Current
HttpContext in your method to use it. Example:
if(HttpContext.Current.Session["supplierID"] == null)
--
HTH,
Kevin Spencer
Microsoft MVP
..Net Developer http://www.takempis.com
Big Things are made up of
Lots of Little Things.
"Tim T" <br*********@hotmail.com> wrote in message
news:bo***********@news.wplus.net... Hi, I'm new to .NET and am still trying to get my head around OO concepts.
I am building an asp.net app and on several pages i want to check to see
if a session object exists, if so - contine to show the page, if not check if cookie exists, if no cookie then redirect to a login page. This is easy enough, but now i want to put this functionallity in a class
so that i can just call a checkSupplierID method from that class easily from any page
This is what i have done:
//method in tools class
public void checkSupplierIDSession() { if(Session["supplierID"] == null) { HttpCookie cookie = Request.Cookies["supplierID"]; if (cookie != null) { Session["supplierID"] = cookie.Value.ToString(); } else { Server.Transfer("Login.aspx?callingPage=" + this.Page.ToString().Substring(4,this.Page.ToStrin g().Length - 9)); //redirect with this page as calling page } } }
code in aspx.cs code behind file on page i want to check:
private void Page_Load(object sender, System.EventArgs e) { tools.checkSupplierIDSession(); }
I get this error when trying to compile:
An object reference is required for the nonstatic field, method, or
property 'tools.checkSupplierIDSession()'
Initially the method was static, but that meant that i couldn't use the Session, Request, and this.Page properties on the calling page?
can someone please advise on the best way to do what i am trying to
achieve? Thanks for your help
Tim
Thanks Kevin, that worked a treat, (after a little change to the bit of code
that gets the 'callingPage')
I appreciate your help
Tim..
"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:O6**************@TK2MSFTNGP12.phx.gbl... Hi Tim,
It's not whether it's static or not that is the problem. The problem is
that it is not a Page class, or another class which intrinsicly gets the HttpContext. However, all you have to do is reference the Current HttpContext in your method to use it. Example:
if(HttpContext.Current.Session["supplierID"] == null)
-- HTH,
Kevin Spencer Microsoft MVP .Net Developer http://www.takempis.com Big Things are made up of Lots of Little Things.
"Tim T" <br*********@hotmail.com> wrote in message news:bo***********@news.wplus.net... Hi, I'm new to .NET and am still trying to get my head around OO
concepts. I am building an asp.net app and on several pages i want to check to see if a session object exists, if so - contine to show the page, if not check
if cookie exists, if no cookie then redirect to a login page. This is easy enough, but now i want to put this functionallity in a
class so that i can just call a checkSupplierID method from that class easily
from any page
This is what i have done:
//method in tools class
public void checkSupplierIDSession() { if(Session["supplierID"] == null) { HttpCookie cookie = Request.Cookies["supplierID"]; if (cookie != null) { Session["supplierID"] = cookie.Value.ToString(); } else { Server.Transfer("Login.aspx?callingPage=" + this.Page.ToString().Substring(4,this.Page.ToStrin g().Length - 9)); //redirect with this page as calling page } } }
code in aspx.cs code behind file on page i want to check:
private void Page_Load(object sender, System.EventArgs e) { tools.checkSupplierIDSession(); }
I get this error when trying to compile:
An object reference is required for the nonstatic field, method, or property 'tools.checkSupplierIDSession()'
Initially the method was static, but that meant that i couldn't use the Session, Request, and this.Page properties on the calling page?
can someone please advise on the best way to do what i am trying to achieve? Thanks for your help
Tim
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Kerry Neilson |
last post by:
For the past couple of months, Idle won't start when I invoke it. I
am at a complete loss for why this is. When this happens, they python
command line still starts, and python works fine...
|
by: Matt |
last post by:
I'm designing an application that will be run on LAN-connected desktop PCs,
and will also be run on laptops that may or may not be disconnected from the
LAN. The application also needs to continue...
|
by: Dean Trower |
last post by:
Does anyone know a clean method of preventing scrolling altogether in a
TEXTAREA
(i.e. ensuring the user CANNOT type more text than fits inside the box
area)?
I'd like to know how, if so... It...
|
by: Vijay Kumar R Zanvar |
last post by:
Did you know that the following if statement is semantically
right, but could logically be wrong?
if ( i = 2 )
{
/* do something */
}
Tip:
|
by: simon |
last post by:
I have simple html(aspx) page, but vertical height won't work.
Even if i had set the height of a table=100%, the table is not 100% height.
I spend a lot of time(my real page is more...
|
by: musosdev |
last post by:
Hi guys
Okay, I've setup my projects to open and compile fine in VS2005 using FPSE
and remote web, but it's *really* slow. So I thought I'd have a go at doing
it the normal way, by loading from...
|
by: John Ortt |
last post by:
Hi everyone.
I have a database which I have developed in Access 2000 which is working
nicely.
The problem is that my customer only has Access 97. I tried to convert the
database but the main...
|
by: Pucca |
last post by:
I'm getting an error when I tried to use this BerConverter class in my C#
code. Even though the Interent doc says that it runs on Win2000 sp4, I just
thgouth I'll double check. Does anyone know...
|
by: scitrenbaum |
last post by:
Here is the dilemma. I have a website where everything works perfectly in all browsers except for Safari.
When you are in Safari and you go to this page...
...
|
by: z55177 |
last post by:
My domain: http://www.esthevision.cz/
This is the cause of my problem.
The template is supposed to look somewhat like this: PINK STRIPE
http://themebot.com/website-templates/ht...
I created an...
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |