-3

I need to retrieve the value of a query string for a department using .net c# but as some of the department names include an '&' as soon as it is read it stops after the &

www.example.co.uk/default.aspx?dept=IM&T

Label1.Text = "The Department is - " + Request.QueryString["dept"];

The result is...

The Department is - IM

How can I get this to read the query string as IM&T?

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
KlydeMonroe
  • 197
  • 2
  • 4
  • 18
  • @PatrickHofman because is asp.net the duplicate is not the one you set. The encoding is done using the `Server.URLEncode` and the decode is automatic done on `QueryString[]` call – Aristos May 27 '16 at 09:21
  • hi @PatrickHofman - I have tried what you have suggested - Label3.Text = Server.UrlEncode(Request.QueryString["Dept"]); but it is still having the same issue and stopping as soon as it hits the &. The question which you have linked through didnt seem to help with my issue... As I am linking from one page with a gridview of items and passing the id with a querystring to another page showing a break down of the depts for the selected item i am unable to just change the & in the query string to %26. – KlydeMonroe May 27 '16 at 10:50
  • I have managed to resolve the issue - where I am picking up the department set as a query string I have used the replace method to replace & with %26 this then allows the gridview to read the query string parameter. – KlydeMonroe May 27 '16 at 11:04

2 Answers2

2

escaping ampersand in url

To use "&" as URL query string, simply changes it to %26 percent-encoding. & is used for HTML encoding.

Community
  • 1
  • 1
Tetsuya Yamamoto
  • 24,297
  • 8
  • 39
  • 61
0

You need to encode an & (ampersand) in a query sting to %26

Here is a helpful tool to help: http://meyerweb.com/eric/tools/dencoder/

And a good thread on the rules on when to escape:

What characters must be escaped in an HTTP query string?

Community
  • 1
  • 1
Murray Foxcroft
  • 12,785
  • 7
  • 58
  • 86