Problem with C# Class and Server.HTMLEncode

  • Thread starter Thread starter Andrea Williams
  • Start date Start date
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
 
Try using this syntax instead:
System.Web.HttpContext.Current.Server.HtmlEncode(strValue);
 
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
 
+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.
 
Back
Top