【问题标题】:asp.net web page pass parameter to another page include "#" [duplicate]asp.net网页将参数传递给另一个页面包括“#”[重复]
【发布时间】: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


    【解决方案1】:

    您需要对要传递的参数进行编码。参数编码可以使用HttpUtility.UrlEncode(value);方法。

    您可以在.aspx页面上执行c#代码如下

    <a href="WebForm2.aspx?item=<%=HttpUtility.UrlEncode('#004')%>">go to webForm2</a>
    

    【讨论】:

    • 我使用javascript而不是c#来传递
    • 您可以在.aspx页面上执行c#代码。我已经更新了答案,请检查。
    • 我发现encodeURIComponent可以解决问题。
    猜你喜欢
    • 2011-01-02
    • 1970-01-01
    • 2015-02-17
    • 2016-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多