The & character needs to be URL encoded because & is used to divide the query string into name=value pairs. When creating the URL, use:
HttpUtility.UrlEncode(url);
Question
I'm using QueryStrings for users to auto-select values from a drop down list. Some of the values in this drop down list contain "&" in them.
E.g. "Acquisitions & Development"
When the user passes through a value with an "&", the string value gets cut off before the second half.
string functionalDepartment = Request.QueryString["FunctionalDepartment"];
//returns "Acquisitions "
Is there a way that I can include the second half of the value when getting the value from the QueryString?
Solution
The & character needs to be URL encoded because & is used to divide the query string into name=value pairs. When creating the URL, use:
HttpUtility.UrlEncode(url);
OTHER TIPS
Before passing it to query string just replace:
.Replace("&","%26");
or use Server.URLEncode()
You could user System.Web.HttpUtility.UrlEncode
while setting the values for the drop down.
This will ensure that &
will not break the values.
Use HttpServerUtility.UrlEncode:
URL encoding ensures that all browsers will correctly transmit text in URL strings. Characters such as a ampersand (&) might be truncated or corrupted by some browsers.
More Details see this Query strings with special characters
I also refer that
Server.UrlEncode("url")
will solve ur issue.
Example:
MyURL = "http://www.ursite.com/urpage.aspx?ReqStr=" + Server.UrlEncode("ASP.NET Examples");