Click here to Skip to main content
15,921,837 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
We're emitting html through a custom server control in C#.

So this looks on the code behind as (a rough, trimmed down version to keep it relevant):
string.Format("<input id='{0}' value='{1}' name ='{0}>", idHere, valHere);

The problem is that valHere accepts free text like single quotes, double quotes, other special characters as well.

Escaping by \ doesn't work effectively.

So what's an effective solution for this?

Thanks for any pointers!
Posted
Comments
Sergey Alexandrovich Kryukov 31-Aug-11 23:00pm    
What do you mean be "doesn't work"? It does work.
--SA

Try one of the approaches on this page[^].
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 1-Sep-11 10:40am    
Of course, this is the answer, my 5. I removed my solution; thank you for pointing out my mistake.
--SA
I'm not sure if I conveyed my question incorrectly, because your solution is incorrect with respect to what I'm trying to ask. In general, I think people ask a question with respect to their requirements.

When you say escaping with \ in a string literal. Yes that's fine. But as I indicated, this text is going to be emitted out through a server control. This control toggles readonly and edit modes. The input data is inputted/set on page 1(let's call it pageSource.aspx). PageResults.aspx has the custom server control that injects html as I indicated. We display all values entered by user on pageSource on this custom server control. Now the user can "toggle" the control to edit what he had entered on pageSource. I hope this gives you a better picture.

In other words, what will get rendered out will be:

HTML
<input id="anExampleId" value="someValueExample"></input>


Now let's say , the user inputs
HTML
ab\\\'"b
. See what I'm saying? Now, by escaping etc, it's not going to work. That's what I meant by it doesn't work. Try jsFiddle or another tool try to emit out an input tag with value that contains both single quotes, double quotes etc.

You can't manipulate this:

string input =
   string.Format("<input id="{0}" value="{1}" name="{0}">", idHere, valHere);</input>



I think I have a partial solution (this is the direction we have to take):

It's not that input string above that should be manipulated, it's the actual value (
valHere
) that needs to be manipulated.

valHere= valHere.Replace("\'", "\\'");


I'll add my actual solution shortly.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900