如何将查询字符串变量填充到其中包含 &、\ 和 $ 的文本框
我有一个变量,比如 A= drug &医疗保险 $12/$15
。
我需要将其分配给文本框,但只有“药物”发布到服务器。其余数据被截断。
this.textbox.text= request.querystring["A"].tostring();
I have a variable like say A= drug & medicare $12/$15
.
I need to assign it to a text box, but only 'drug' is posted the server. The rest of the data gets truncated.
this.textbox.text= request.querystring["A"].tostring();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
以下内容对于 a="foo&bar$12" 无效
:符号是保留字符,它分隔查询字符串变量。在将值发送到该页面之前,您需要对值进行百分比编码。
还有&是 HTML/XML 中的保留字符。我建议阅读百分比编码和 html 编码。
The following is not valid for a="foo&bar$12":
The & symbol is a reserved character, it seperates query string variables. You will need to percent encode a value before sending them to that page.
Also & is a reserved character in HTML/XML. I suggest reading up on percent encoding and html encoding.
我相信您对 HTML 实体有疑问。您需要在您选择的工具中阅读有关 HTML 转义的内容。
&
不能在 HTML 中存在,因为它开始一个实体序列 - 它需要用&
替换。如果不至少指定您正在使用哪个工具链(根据 @Richard 的评论),我们无法真正建议最好的方法。编辑:现在我重读了你的问题,看来 A 不是一个变量而是一个查询参数:) 阅读理解失败。无论如何,在这种情况下,存在类似的问题: &不是查询参数的有效字符,需要进行 URL 转义。同样,具体如何操作在您的工具链文档中,但本质上是这样的。需要替换为
%26
。加号也是不允许的(或者更确切地说它有其他含义);其他的是可以容忍的(但是有更好的方法来编写它们)。I believe you have problems with HTML entities. You need to read up on HTML escaping in your tool of choice.
&
cannot stand in HTML, since it begins an entity sequence - it needs to be replaced with&
. Without specifying at least which toolchain you're using (as per @Richard's comment), we can't really suggest the best way to do it.EDIT: Now that I reread your question, it seems A is not a variable but a query parameter :) Reading comprehension fail. Anyway, in this case a similar problem exists: & is not a valid character for a query parameter, and it needs URL escaping. Again, how exactly to do it is in the documentation for your toolchain, but in essence & will need to be replaced by
%26
. Plus sign is also not permitted (or rather it has another meaning); others are tolerated (but there are nicer ways to write them).这看起来或多或少像 ASP.NET 伪代码,因此我将诊断您的问题,因为查询字符串需要进行 URL 编码。查询字符串中的键/值对由与号 (&) 分隔,ASP.NET(以及其他 Web 平台)会自动为您解析出键值对。
在这种情况下,& 符号终止“A=...”键/值对的值。如果您可以对将用户带入您的页面的链接进行 URL 编码,那么问题就会得到解决。如果实际使用 ASP.NET,可以使用 HttpUtility.UrlEncode() 方法 为此:
您最终会得到以下查询字符串:A=drug%20%26%20medicare%20%2412%2F%2415
That looks more or less like ASP.NET pseudocode, so I'm going to diagnose your problem as the query string needing to be URL encoded. Key/value pairs in the query string are separated by an ampersand (&), and ASP.NET (along with other web platforms) automatically parse out the key value pairs for you.
In this case, the ampersand terminates the value of the "A=..." key/value pair. The problem will be solved if you can URL encode the link that brings the user into your page. If actually using ASP.NET, you can use the HttpUtility.UrlEncode() method for that:
You'll end up with this querystring instead: A=drug%20%26%20medicare%20%2412%2F%2415