C# asp.net网页将参数传递到另一个页面包括#&引用;
我使用vs2010。c# 我需要将参数从一个页面传递到另一个页面,看起来很简单。 但是我有一个问题,新页面无法获取参数值。 因为“#” 这是样品 这是第1页C# asp.net网页将参数传递到另一个页面包括#&引用;,c#,asp.net,parameters,C#,Asp.net,Parameters,我使用vs2010。c# 我需要将参数从一个页面传递到另一个页面,看起来很简单。 但是我有一个问题,新页面无法获取参数值。 因为“#” 这是样品 这是第1页 <body> <form id="form1" runat="server"> <div> <a href="WebForm2.aspx?item='#004'">go to webForm2</a> </div> </form> 如何传递具有特殊字符的参
<body>
<form id="form1" runat="server">
<div>
<a href="WebForm2.aspx?item='#004'">go to webForm2</a>
</div>
</form>
如何传递具有特殊字符的参数
我发现encodeURIComponent可以解决这个问题,但它似乎不需要用c代码进行解码。 这是样品 第1页:
<script type="text/javascript">
function goto() {
var id = '#004@$%^&?';
var url = 'WebForm2.aspx?item=' + encodeURIComponent(id);
window.location.href = url;
}
</script>
这里不需要解码
这是否正确?您需要对正在传递的参数进行编码。您可以使用
HttpUtility.UrlEncode(值)代码>编码参数的方法
您可以在.aspx页面上执行c#代码,如下所示
<a href="WebForm2.aspx?item=<%=HttpUtility.UrlEncode('#004')%>">go to webForm2</a>
我使用javascript而不是c来传递,您可以在.aspx页面上执行c代码。我已经更新了答案,请检查。我发现编码器组件可以解决问题。
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;
}
}
<a href="WebForm2.aspx?item=<%=HttpUtility.UrlEncode('#004')%>">go to webForm2</a>