Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.1
|
ASP.NET 1.1 As of 10/6/2005, this forum is locked as part of the reorganization described here: http://p2p.wrox.com/topic.asp?TOPIC_ID=35394. No posts have been deleted. Open ongoing discussions from the last week have been moved to either ASP.NET 1.0 and 1.1 Beginners http://p2p.wrox.com/asp-net-1-0-1-1-basics-60/ or ASP.NET 1.0 and 1.1 Professional. http://p2p.wrox.com/forum.asp?FORUM_ID=50. See my sticky post inside for more.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.1 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 5th, 2004, 09:47 AM
Friend of Wrox
 
Join Date: Jun 2004
Posts: 245
Thanks: 0
Thanked 0 Times in 0 Posts
Default Server.HtmlEncode -- Easy Ques

Hello
This is suppose to be simple but I can't get it to work:

Imports System.Web.HttpServerUtility

s ="This is a <Test String>"
lblError.Text = Server.HtmlEncode(s)



I am suppose to get "This+is+a+%3cTest+String%3e" but I get "This is a <Test String>". What am I overlooking??

 
Old October 5th, 2004, 02:46 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

The string gets encoded as "This is a &lt;Test String&gt;", if you "view source" you will see it. But you will not see that as the label text because when the page renders, it interprets the characters and shows you the original text.

 
Old October 5th, 2004, 03:54 PM
Friend of Wrox
 
Join Date: Jun 2004
Posts: 245
Thanks: 0
Thanked 0 Times in 0 Posts
Default


jbenson001

According to MSDN when I use Server.HtmlEncode I am suppose to get "This+is+a+%3cTest+String%3e" not "This is a &lt;Test String&gt;". How can I get "This+is+a+%3cTest+String%3e"???

Below is the link that tells me that "This is a <Test String>" suppose to get "This+is+a+%3cTest+String%3e".

http://msdn.microsoft.com/library/de...ncodeTopic.asp

What am I doing wrong???

 
Old October 5th, 2004, 04:01 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

I am not sure why it doesn't encode that way. The example MSDN gives won't even work, you can't Dim writer As New StringWriter, this gives an error.

Why is this so important to you to get this particular result?


 
Old October 5th, 2004, 10:05 PM
Friend of Wrox
 
Join Date: Jun 2004
Posts: 245
Thanks: 0
Thanked 0 Times in 0 Posts
Default

jbenson001,
I see Server.HtmlEncode used to encode URL Strings on most all major websites like Yahoo.com.

Somebody Please help me.

 
Old October 5th, 2004, 10:11 PM
Friend of Wrox
 
Join Date: Jun 2004
Posts: 245
Thanks: 0
Thanked 0 Times in 0 Posts
Default


Imports System.Web.HttpServerUtility

Dim TestString As String = "This is a <Test String>."
Dim writer As New StringWriter
Server.HtmlEncode(TestString, writer)
Dim EncodedString As String = writer.ToString()


suppose to yield:This+is+a+%3cTest+String%3e
but I get:This is a <Test String>.

Help!!!!!

 
Old October 6th, 2004, 04:21 AM
Registered User
 
Join Date: Oct 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi ,

Use Urlencode, try this.

string str = "This is a <Test String>.";

Response.Write(str);

string encodedstr = System.Web .HttpUtility .UrlEncode (str.ToString());

Response.Write(encodedstr);

 
Old October 6th, 2004, 06:02 AM
Friend of Wrox
 
Join Date: Jun 2004
Posts: 245
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello butterfly

I think your C# code translates to this in VB.NET ( I don't know C# at all):
------------------------------------------------------------
 Dim str As String = "This is a <Test String>."

        Response.Write(str)

        Dim encodedstr As System.Web.HttpUtility

        encodedstr.UrlEncode(str)
        Response.Write(encodedstr)
------------------------------------------------------------

When I try this I only get "This is a ."
I don't even see the encoded string.

Please Help

 
Old October 6th, 2004, 06:10 AM
Friend of Wrox
 
Join Date: Jun 2004
Posts: 245
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks butterfly it worked.

I have another question.
Originally I was trying to use Server.HtmlEncode for textboxes so I can make the html tag content safe to store in SQL Database.

I was told if I use Server.HtmlEncode that I can avoid SQL Script injections. But I couldn't get Server.HtmlEncode to work.

Is it common practice to use UrlEncode as a substitute for Server.HtmlEncode to avoid SQL Script Injections??

What is the difference between UrlEncode and HtmlEncode???

I am very new to ASP.NET.

 
Old October 6th, 2004, 07:00 AM
Friend of Wrox
 
Join Date: Jun 2004
Posts: 245
Thanks: 0
Thanked 0 Times in 0 Posts
Default

One final question for this topic.

How come when I use UrlDecode I don't get the correct answer?
Instead of getting "This is a <Test String>." I get "This is a ."

Dim encodedstr As System.Web.HttpUtility
        Dim encoded As String
        Dim decode As String


        encoded = encodedstr.UrlEncode("This is a <Test String>.")
        Label2.Text = encoded.ToString

        decode = encodedstr.UrlDecode(encoded)
        Label3.Text = decode

-------------------------------------------------
What am I overlooking???






Similar Threads
Thread Thread Starter Forum Replies Last Post
Easy hyperlink ques with iframe bekim Javascript How-To 4 September 21st, 2005 02:45 AM
Easy Ques -- Datetime validation for Access bekim Access 2 June 16th, 2005 01:52 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.