Problem with C# Class and Server.HTMLEncode

A

Andrea Williams

I have a class that imports System.Web.UI.Page. The code below works:

public string JScriptEncode(string strValue)

{

strValue = strValue.Replace(@"'", "\x27"); //' JScript encode apostrophes

strValue = strValue.Replace(@"""", "\x22"); //' JScript encode double-quotes

strValue = Server.HtmlEncode(strValue); //' encode chars special to HTML

return strValue;

}



But as soon as I add the keyword "static" to the function, the
Server.HTMLEncode call fails. But if I don't use the "static" keyword, then
my pages cannot access it and use it. Am I missing something here? Is
there another .Net call I should be using instead?

Thanks,

Andrea
 
S

Steve C. Orr [MVP, MCSD]

Try using this syntax instead:
System.Web.HttpContext.Current.Server.HtmlEncode(strValue);
 
G

Guest

You are using a instance member of Page, which is Page.Server. Basically, you cannot reference instance members in static methods unless they were passed as parameters.

Luckyily there is a static version of the Server class (System.Web.HttpUtility).

Replace your Server.HtmlEncode with System.Web.HttpUtility.HtmlEncode.

Joe
 
M

Michael Earls

+1 on this.

Joe Agster said:
You are using a instance member of Page, which is Page.Server. Basically,
you cannot reference instance members in static methods unless they were
passed as parameters.
 

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

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top