encoding of "&" vs "&" in url

D

DC Gringo

I am having a bear of a time with setting a URL query string as a text value
in a dropdownlist and Server.URLEncode does not seem to do its job.

theFullLink = theLinkPrefix & theImageryTypeTrimmed &
Server.URLEncode("&f=")
ddlMyDropDownList.Items.Add(New ListItem("MyTextValue", theFullLink & "af"))

Which puts out the following HTML:

<option value="~/folder/page.aspx?param1=value1%26f%3daf">-
Afghanistan</option>



If I leave off the Server.URLEncode() i get:

<option value="~/folder/page.aspx?param1=value1&amp;f=af">-
Afghanistan</option>

Help!

_____
DC G
 
O

Oliver Wong

DC Gringo said:
If I leave off the Server.URLEncode() i get:

<option value="~/folder/page.aspx?param1=value1&amp;f=af">-
Afghanistan</option>

Help!

This output seems correct to me. What were you expecting?

- Oliver
 
D

DC Gringo

I was expecting:

<option value="~/folder/page.aspx?param1=value1&f=af">- Afghanistan</option>

This dropdownlist is being posted back and it does not work as is...


____
DC G
 
O

Oliver Wong

DC Gringo said:
I was expecting:

<option value="~/folder/page.aspx?param1=value1&f=af">-
Afghanistan</option>

This dropdownlist is being posted back and it does not work as is...

Unfortunately, I don't think the above is legal XML (and therefor not
valid XHTML either). The ampersign means that the next few characters will
refer to an entity reference, but entity references are always terminsated
by a semicolon, and there is no semicolon after the ampersign. I'm assuming
you want the "value" attribute to have the following value:
"~/folder/page.aspx?param1=value1&f=af". To express that in XML, you'd have
to escape the ampersign so that what actually appears in the XML file is
"~/folder/page.aspx?param1=value1&amp;f=af".

This is similar to the concept that to represent the string which only
contains one instance of the character \, you have to type it in as "\\" or
@"\" in C#.

- Oliver
 
D

DC Gringo

Clinton,

Thanks for the suggestion, but it didn't work...the result was:

<option value="~/centers/innerSections.aspx?pageID=ndvi&amp;#38;f=af">-
Afghanistan</option>

_____
DC G
 
D

DC Gringo

Oliver,

Thank you for the explanation...that seems to make good sense.

However, I still have to implement this code somehow. How else can I do
this?

theFullLink = theLinkPrefix & theImageryTypeTrimmed & "&f="
ddlGotoRegionCountryCenter.Items.Add(New ListItem("- countryName",
theFullLink & "af"))

To get this output:

<option value="~/folder/page.aspx?param1=value1&f=af">- countryName</option>

???
_____
DC G
 
O

Oliver Wong

DC Gringo said:
Oliver,

Thank you for the explanation...that seems to make good sense.

However, I still have to implement this code somehow. How else can I do
this?

The point I'm getting at is that the text you're trying to output isn't
valid XML, so assuming you want to output XML (e.g. HTML), you should never
need to put an ampersign like that.

However, if for some reasons, you have clients or something which demand
this kind of output, then try emitting the data as plain text instead of
HTML; that way, you won't have any sort of escaping mechanism interfering.

- Oliver
 
Joined
Dec 25, 2009
Messages
3
Reaction score
0
Hi,
For some of you reading this post and wondering how to url encode a character, try this url encoder
stringfunction.com/url-encode.html
Pretty useful
David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top