oJM86o
oJM86o

Reputation: 2118

Passing characters such as # and ++ in query string?

I create a hyperlink that when clicks generates a url like so:

http://somesite?MyTag=C++

And I read it into a text box like so:

Me.txtTags.Text = CType(Request.QueryString("Tag"), String)

But the result of this is that the textbox txtTags will only contain C and doesnt have the ++. I tried http://somesite?MyTag=C# and the # is missing as well. But if I look at the address bar these values are there....

Upvotes: 0

Views: 4206

Answers (6)

Mahdi Ghiasi
Mahdi Ghiasi

Reputation: 15301

You should Encode the string when you are generating that link using Server.UrlEncode, here is an example:

MyHyperLink.NavigateUrl = "http://www.mysite.com/default.aspx?mytag=" & Server.UrlEncode("C++")

And when you are trying to process it in default.aspx, you should decode it using Server.URLDecode, here is an example:

Me.txtTags.Text = Server.UrlDecode(Request.QueryString("mytag")) 'This will show "C++" without quotes in txtTags textbox.

You may want to read more about Server.UrlEncode here and about Server.UrlDecode here.

Upvotes: 1

Samich
Samich

Reputation: 30125

You need to use UrlEncode when you building url and UrlDecode when you are trying to read url params

MyURL = "http://www.contoso.com/articles.aspx?title=" & Server.UrlEncode("C#")

Upvotes: 2

NicoTek
NicoTek

Reputation: 1167

Here is a list of Special Characters for Query String

http://permadi.com/tutorial/urlEncoding/

Upvotes: 0

Alexander Corwin
Alexander Corwin

Reputation: 1157

# is used as the anchor tag, so it's not a legal query param. It represents the end of the query string and segues into the beginning of the anchor string. I just ran into this problem myself yesterday :)

+ is generally used to encode spaces in URLs, so it won't show up in a query string either.

Upvotes: 4

Arxae
Arxae

Reputation: 102

'#' can't be used since it is used for HTML anchors. Don't know the exact answer for the + though

Upvotes: 1

PraveenVenu
PraveenVenu

Reputation: 8337

try

Me.txtTags.Text = Server.UrlDecode(Request.QueryString("MyTag"))

Upvotes: 5

Related Questions