encoding of "&" vs "&" in url

Discussion in 'ASP .Net' started by DC Gringo, Aug 4, 2005.

  1. DC Gringo

    DC Gringo Guest

    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
    DC Gringo, Aug 4, 2005
    #1
    1. Advertising

  2. DC Gringo

    Oliver Wong Guest

    "DC Gringo" <> wrote in message
    news:...
    > 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
    Oliver Wong, Aug 4, 2005
    #2
    1. Advertising

  3. DC Gringo

    DC Gringo Guest

    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


    "Oliver Wong" <> wrote in message
    news:cuuIe.207802$on1.77081@clgrps13...
    >
    > "DC Gringo" <> wrote in message
    > news:...
    >> 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
    >
    DC Gringo, Aug 4, 2005
    #3
  4. DC Gringo

    clintonG Guest

    Try passing the character entity & instead of the ampersand.

    <%= Clinton Gallagher
    METROmilwaukee (sm) "A Regional Information Service"
    NET csgallagher AT metromilwaukee.com
    URL http://metromilwaukee.com/
    URL http://clintongallagher.metromilwaukee.com/


    "DC Gringo" <> wrote in message
    news:...
    >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
    >
    >
    > "Oliver Wong" <> wrote in message
    > news:cuuIe.207802$on1.77081@clgrps13...
    >>
    >> "DC Gringo" <> wrote in message
    >> news:...
    >>> 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
    >>

    >
    >
    clintonG, Aug 4, 2005
    #4
  5. DC Gringo

    Oliver Wong Guest

    "DC Gringo" <> wrote in message
    news:...
    >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
    Oliver Wong, Aug 5, 2005
    #5
  6. DC Gringo

    DC Gringo Guest

    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

    "clintonG" <> wrote in message
    news:...
    > Try passing the character entity & instead of the ampersand.
    >
    > <%= Clinton Gallagher
    > METROmilwaukee (sm) "A Regional Information Service"
    > NET csgallagher AT metromilwaukee.com
    > URL http://metromilwaukee.com/
    > URL http://clintongallagher.metromilwaukee.com/
    >
    >
    > "DC Gringo" <> wrote in message
    > news:...
    >>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
    >>
    >>
    >> "Oliver Wong" <> wrote in message
    >> news:cuuIe.207802$on1.77081@clgrps13...
    >>>
    >>> "DC Gringo" <> wrote in message
    >>> news:...
    >>>> 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
    >>>

    >>
    >>

    >
    >
    DC Gringo, Aug 5, 2005
    #6
  7. DC Gringo

    DC Gringo Guest

    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

    "Oliver Wong" <> wrote in message
    news:x_KIe.158904$HI.81946@edtnps84...
    >
    > "DC Gringo" <> wrote in message
    > news:...
    >>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
    >
    DC Gringo, Aug 5, 2005
    #7
  8. DC Gringo

    Oliver Wong Guest

    "DC Gringo" <> wrote in message
    news:OBxul%...
    > 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
    Oliver Wong, Aug 8, 2005
    #8
  9. DC Gringo

    davitz38

    Joined:
    Dec 25, 2009
    Messages:
    3
    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
    davitz38, Feb 5, 2010
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. DC Gringo
    Replies:
    3
    Views:
    6,129
    mikeb
    Aug 19, 2004
  2. Replies:
    1
    Views:
    682
    Rosanne
    Oct 11, 2005
  3. FoundThisOnline

    &amp; problem with hyperlink control

    FoundThisOnline, Oct 11, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    2,879
    FoundThisOnline
    Oct 11, 2005
  4. Nathan Sokalski
    Replies:
    8
    Views:
    4,608
    Raymond
    Mar 1, 2006
  5. T.J.

    &amp, in URL's

    T.J., Jan 28, 2005, in forum: HTML
    Replies:
    31
    Views:
    7,708
    Duende
    Feb 15, 2005
Loading...

Share This Page