object reference is required for the nonstatic field method or property

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
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
M

Mike

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top