473,387 Members | 1,693 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,387 software developers and data experts.

object reference is required for the nonstatic field method or property

ASP.NET 2.0

I have some code working in a code-behind that I want to move to a utility
class. In the code behind, I can reference the querystring and get the
Page value easily. However, when I try to reconstruct the method (below) in
a separate class, my attempt to reference the querystring of the page that
calls the PageArticle method is throwing the error in the subject of the
post.

public sealed class Utils
{
private Utils() {}

public static string PageArticle(string Article)
{
string Output;
string PageNo = System.Web.UI.Page.Request.QueryString["Page"];
//Throws error

if (Article.IndexOf("<!--pagebreak-->") != -1)
{
string[] Pages =
System.Text.RegularExpressions.Regex.Split(Article , "<!--pagebreak-->");
int TotalPages = Pages.GetUpperBound(0) + 1;
int Counter = 1;
int PageNumber = 1;
if (PageNo != null)
{
PageNumber = System.Convert.ToInt32(PageNo);
}
Output = Pages[PageNumber - 1] + "<br />";
Output += "Go to Page ";
while (Counter <= TotalPages)
{
if (Counter == PageNumber)
{
Output += Counter.ToString() + " ";
}
else
{
Output += "<a href=\"Article.aspx?Page=" +
Counter.ToString() + "\">" + Counter.ToString() + "</a";
}
Counter++;
}
}
else
{
Output = Article;
}
return Output;
}
}

Can anyone point out what I've done wrong?

Thanks
Apr 9 '07 #1
2 14230
Mike,

The Page class does not have a static method for the request. The
reason why you can use the Request property in your page is because your
page extends the Page class, which exposes the property on the instance.

Instead, you have to use the HttpContext class, use the static Current
property on it, which will give you an HttpContext instance which you can
then get the request from, using the Request property.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Mike" <bl***@blank.blankwrote in message
news:un**************@TK2MSFTNGP04.phx.gbl...
ASP.NET 2.0

I have some code working in a code-behind that I want to move to a utility
class. In the code behind, I can reference the querystring and get the
Page value easily. However, when I try to reconstruct the method (below)
in a separate class, my attempt to reference the querystring of the page
that calls the PageArticle method is throwing the error in the subject of
the post.

public sealed class Utils
{
private Utils() {}

public static string PageArticle(string Article)
{
string Output;
string PageNo = System.Web.UI.Page.Request.QueryString["Page"];
//Throws error

if (Article.IndexOf("<!--pagebreak-->") != -1)
{
string[] Pages =
System.Text.RegularExpressions.Regex.Split(Article , "<!--pagebreak-->");
int TotalPages = Pages.GetUpperBound(0) + 1;
int Counter = 1;
int PageNumber = 1;
if (PageNo != null)
{
PageNumber = System.Convert.ToInt32(PageNo);
}
Output = Pages[PageNumber - 1] + "<br />";
Output += "Go to Page ";
while (Counter <= TotalPages)
{
if (Counter == PageNumber)
{
Output += Counter.ToString() + " ";
}
else
{
Output += "<a href=\"Article.aspx?Page=" +
Counter.ToString() + "\">" + Counter.ToString() + "</a";
}
Counter++;
}
}
else
{
Output = Article;
}
return Output;
}
}

Can anyone point out what I've done wrong?

Thanks

Apr 9 '07 #2
Nicholas,

Thank you for your very helpful answer. All I needed was a nudge in the
right direction, and I'm very much obliged to you for providing it (and not
just a bunch of corrected code that I may have been none the wiser for).
I've dug around in the HttpContext class, and used it to make a few
adjustments to my earlier code, and now it works perfectly.

Best regards
Mike

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:%2****************@TK2MSFTNGP04.phx.gbl...
Mike,

The Page class does not have a static method for the request. The
reason why you can use the Request property in your page is because your
page extends the Page class, which exposes the property on the instance.

Instead, you have to use the HttpContext class, use the static Current
property on it, which will give you an HttpContext instance which you can
then get the request from, using the Request property.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Mike" <bl***@blank.blankwrote in message
news:un**************@TK2MSFTNGP04.phx.gbl...
>ASP.NET 2.0

I have some code working in a code-behind that I want to move to a
utility class. In the code behind, I can reference the querystring and
get the Page value easily. However, when I try to reconstruct the method
(below) in a separate class, my attempt to reference the querystring of
the page that calls the PageArticle method is throwing the error in the
subject of the post.

public sealed class Utils
{
private Utils() {}

public static string PageArticle(string Article)
{
string Output;
string PageNo = System.Web.UI.Page.Request.QueryString["Page"];
//Throws error

if (Article.IndexOf("<!--pagebreak-->") != -1)
{
string[] Pages =
System.Text.RegularExpressions.Regex.Split(Articl e, "<!--pagebreak-->");
int TotalPages = Pages.GetUpperBound(0) + 1;
int Counter = 1;
int PageNumber = 1;
if (PageNo != null)
{
PageNumber = System.Convert.ToInt32(PageNo);
}
Output = Pages[PageNumber - 1] + "<br />";
Output += "Go to Page ";
while (Counter <= TotalPages)
{
if (Counter == PageNumber)
{
Output += Counter.ToString() + " ";
}
else
{
Output += "<a href=\"Article.aspx?Page=" +
Counter.ToString() + "\">" + Counter.ToString() + "</a";
}
Counter++;
}
}
else
{
Output = Article;
}
return Output;
}
}

Can anyone point out what I've done wrong?

Thanks


Apr 9 '07 #3

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

Similar topics

6
by: Andrew Mueller | last post by:
Hello all, I am working on a project, converting code from VB 6.0 to C#. I have a COM object (SDK.dll) referenced and am having some issues using it. I don't think it is specific to the object...
1
by: TdJ | last post by:
No end of reading on the net and I can not get this to compile! The error message is: An object reference is required for the nonstatic field, method, or property...
7
by: Chris Clement | last post by:
I have been handed a project that someone else started and most of it was developed in the VS.NET design mode. For whatever reasons, when I try to make changes to the controls in VS.NET design...
5
by: Vicky via DotNetMonster.com | last post by:
Hi, I need help with "An object reference is required for the nonstatic field, method, or property 'dataReader.Class1.data'" Before I put folowing variable in class level, it works fine....
2
by: Beffmans | last post by:
Hi When I run this code: using System; namespace DelegateProject { public delegate void MyDelegate(string s);
6
by: tshad | last post by:
I am getting the following error for: C:\VSProjects\ClassLibrary4\NewHire.cs(72): An object reference is required for the nonstatic field, method, or property 'MyFunctions.NewHire.firstName' ...
2
by: simonZ | last post by:
I have class, which returns dataTable to populate my list box on some other pages: public class dataClass { private Int16 _lvlRights=1 public static DataTable dtAdv() {
2
by: rameshonweb | last post by:
Iam getting the error an object reference is required for the nonstatic field method or property 'System.Windows.Forms.Control.CreateGraphics()' for this part of code public static void...
4
by: cppquester | last post by:
I have a data set MMSDataAccess with: public partial class MMSDataSet : System.Data.DataSet { ... private TB_ACTHEATDATADataTable tableTB_ACTHEATDATA; ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...

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.