object reference is required for the nonstatic field method or property

  • Thread starter Thread starter Mike
  • Start date Start date
M

Mike

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
 
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,

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 said:
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]
- (e-mail address removed)

Mike said:
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
 
Back
Top