Results 1 to 2 of 2

Thread: [RESOLVED] htmlencode and htmldecode

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2014
    Posts
    16

    Resolved [RESOLVED] htmlencode and htmldecode

    say I have this in my database:
    <script> Hello world </script>
    <br>
    <h1> Johny apple seed </h1>
    a;sldfjaslkjdflkasdjf

    when I try this:
    Code:
    itemData ="<script> Hello world </script><br><h1> Johny apple seed </h1>a;sldfjaslkjdflkasdjf"
    dItem.InnerHtml = Server.HtmlDecode(itemData)
    I get this:

    Johny apple seed
    a;sldfjaslkjdflkasdjf

    which is what I'm looking for. However, I also get a javascript error because of the <script> hello world </script>
    Is there anyway to get what I want without getting a javascript error?

    I tried this:
    Code:
    dItem.InnerText = Server.HtmlDecode(itemData)
    Then I'll get
    <script> Hello world </script> <br> <h1> Johny apple seed </h1> a;sldfjaslkjdflkasdjf

    which is fine, but not exactly what I want...
    Any ideas?
    thanks.

  2. #2
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,020

    Re: htmlencode and htmldecode

    Since Hello World is inside the script tags which is javascript, you can't set it as innertext of the script element. Instead,
    you show it to the browser using document.write();
    Code:
    string itemData = String.Format("&lt;script&gt; document.write(\"{0}\") &lt;/script&gt;&lt;br&gt;&lt;h1&gt; Johny apple seed &lt;/h1&gt;a;sldfjaslkjdflkasdjf", "Hello world");
    
    dItem.InnerHtml = Server.HtmlDecode(itemData);
    KGC
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width