Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Get HTML string from code behind

Posted on 2010-11-23
8
Medium Priority
?
1,003 Views
Last Modified: 2013-12-17
Hi,

I generate some HTML code from code behind I like to use at client side.
The strings are very complex (full HTML sides) and if I return as raw I get errors and my JavaScript don't work any more (destroyed by the return value).
So I need something like HttpUtility.HtmlEncode before I return it to client.
This works fine but now I need to convert it back as the HTML tags now destroyed and also the layout.
Is there a other function that may work than HttpUtility.HtmlEncode JavaScript can handle native?

Thanks

Andre
0
Comment
Question by:andre72
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 4

Expert Comment

by:DigitalTyrant
ID: 34201367
You can use the javascript functions escape() and unescape() in most cases.
0
 

Author Comment

by:andre72
ID: 34201485
A sample for <html>
After HTMLEncode:
&lt;html&gt;

Client side:
unescape() returns &lt;html&gt;
escape() returns %26lt%3Bhtml%26gt%3B
0
 
LVL 4

Accepted Solution

by:
DigitalTyrant earned 1000 total points
ID: 34201655
The escape() and unescape() functions are alternatives to what you're attempting.  If you include the line
<script language="javascript" type="text/javascript" src="http://code.jquery.com/jquery-1.4.4.js"></script>
in the header of the page, you can use the following line in your javascript.

$("<div>").html("&lt;html&gt;").text()

that line uses jQuery to create a temporary div to convert the string to HTML. For example,

var htmlCode = $("<div>").html("&lt;html&gt;").text();

There are plenty of other custom functions that make the conversion but jQuery is easy to implement and well maintained.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:andre72
ID: 34201726
Thanks this looks much better ...
I still can't understand why special chars are destroyed - do you have an idea for?
0
 
LVL 1

Assisted Solution

by:warhero
warhero earned 1000 total points
ID: 34201778
I see, that's unusual - Javascript function;

escape - for encoding
unescape - for decoding

in your sample, it doesn't seem like decoding function works.

at the moment i can't be sure why but i can suggest some trails and you see how these affect the string value you're trying to encode and decode.

1. make sure the string being decoded in client side is a string
2. try trimming the white space in both sides
3. try using alternative representation for string like use of double and single quotes and see if something happens
4. as for a dirty trick try double decode to see if that does something
unescape(unescape("&lt;html&gt;");

Open in new window

5. i found a custom made encoding class in javascript, try fiddling with your html class to see it this helps, also you would learn how encoding works Custom Class - Html Encoder
 

Reference:
0
 
LVL 4

Expert Comment

by:DigitalTyrant
ID: 34201853
What do you mean destroyed?  
0
 

Author Comment

by:andre72
ID: 34204071
I'm sorry the special char problem was a StreamReader issue and not a de-/encoding problem
0
 

Author Closing Comment

by:andre72
ID: 34204081
Thanks for your support the JQuery and the HTMLDecoder Custom Class both works fine!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question