473,406 Members | 2,769 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,406 software developers and data experts.

Problem with C# Class and Server.HTMLEncode

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
Nov 18 '05 #1
4 10631
Try using this syntax instead:
System.Web.HttpContext.Current.Server.HtmlEncode(s trValue);

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net

"Andrea Williams" <an*******@hotmail.IHATESpam.com> wrote in message
news:Om**************@TK2MSFTNGP11.phx.gbl...
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

Nov 18 '05 #2
THANKS!

I knew there had to be a way... :-)

Andrea
"Steve C. Orr [MVP, MCSD]" <St***@Orr.net> wrote in message
news:O4**************@TK2MSFTNGP11.phx.gbl...
Try using this syntax instead:
System.Web.HttpContext.Current.Server.HtmlEncode(s trValue);

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net

"Andrea Williams" <an*******@hotmail.IHATESpam.com> wrote in message
news:Om**************@TK2MSFTNGP11.phx.gbl...
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


Nov 18 '05 #3
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

Nov 18 '05 #4
+1 on this.

"Joe Agster" <an*******@discussions.microsoft.com> wrote in message
news:83**********************************@microsof t.com...
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

Nov 18 '05 #5

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

Similar topics

4
by: abcd | last post by:
Simple but not working at this moment I have to fill the HTML select control.... my values could be for example "test number 1" sample code, dim x Data = "test ...
3
by: Michael Gaillez | last post by:
Hi, I'm dynamically loading an assembly into an ASP.NET application. In the control that I load from that dynamic assembly I've tried to use Server.HTMLEncode but this results in a serious...
1
by: bidllc | last post by:
I'm working on a minor bug from an open source bug tracking system (bugtracket.net). It's a great app, but I don't want to bother the creator any more than I have to, so I thought I'd pose the...
1
by: Marco Miltenburg | last post by:
While working on some multilingual code I found a rather strange thing happening with Server.HTMLEncode. While loading different languages I change the Codepage and Charset in ASP to reflect the...
6
by: Greg | last post by:
Hello, I have a GridView bound to a custom object. I set the DataKeyNames property along with the column DataField properties at design time, and bind the GridView to my object at run-time. In...
3
by: hugo | last post by:
Hi, I have a function that I call from form fields using the OnKeyUp function to replace special caracters. Once this function has been called, it does not set the focus on the form field where...
0
by: webaccess | last post by:
Hi Friends ..!! I want to use datagrid/dataview control to data in tablular format,also I want to add paging and format the data of table column. Problem is data is coming from API Dom in as...
5
by: =?Utf-8?B?R1ROMTcwNzc3?= | last post by:
Hi Guys, It's been a while, I've got a small problem that I could do with your expertise on. As you know (I Think) I build websites for recruitment agencies, part of the website includes a job...
2
by: yogarajan | last post by:
hello friend i need urgent help this is my aspx page <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Reguserdetailstest.aspx.cs" Inherits="Reguserdetailstest" %> <!DOCTYPE html...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.