【发布时间】:2014-06-26 09:41:02
【问题描述】:
我使用 vs2010。 C# 我需要将参数从一个页面传递到另一个页面,看起来很容易。 但是我有一个问题,新页面无法获取参数值。 因为“#”
这里是示例
这是第 1 页
<body>
<form id="form1" runat="server">
<div>
<a href="WebForm2.aspx?item='#004'">go to webForm2</a>
</div>
</form>
页面2
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string item = Request.QueryString["item"];
//here item value is empty.
}
}
如何传递带有特殊字符的参数?
我发现 encodeURIComponent 可以解决问题,但似乎不需要在 c# 代码中解码。 这是示例
第 1 页:
<script type="text/javascript">
function goto() {
var id = '#004@$%^&?';
var url = 'WebForm2.aspx?item=' + encodeURIComponent(id);
window.location.href = url;
}
</script>
第2页
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string item = Request.QueryString["item"];
Label1.Text = "pass value:" + item;
//below the result is the same as above
string value = Server.UrlDecode(item);
Label2.Text = value;
}
}
这里不需要解码。
正确吗?
【问题讨论】:
标签: c# asp.net parameters