-1

I use vs2010. c# I need pass parameter from one page to another, it seems it's easy. however I have a issue ,the new page cannot get the parameter value. because of "#"

here is the sample

this is page 1

<body>
<form id="form1" runat="server">
<div>
<a href="WebForm2.aspx?item='#004'">go to webForm2</a>
</div>
</form>

the page2

 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.
    }
}

How to pass parameter with special character?

I find the encodeURIComponent can sovle the problem, but it seems don't need to decode in c# code. here is the sample

Page1:

 <script type="text/javascript">
    function goto() {
        var id = '#004@$%^&?';
        var url = 'WebForm2.aspx?item=' + encodeURIComponent(id);
        window.location.href = url;
    }
</script>

Page2

    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;
    }
}

Here no need to decode.

Is that corrent?

Jack.li
  • 55
  • 2
  • 10

1 Answers1

1

You will need to encode the parameters which you are passing. You can use HttpUtility.UrlEncode(value); method for encoding parameters.

You can execute the c# code on .aspx page as follows

<a href="WebForm2.aspx?item=<%=HttpUtility.UrlEncode('#004')%>">go to webForm2</a>
Vaibhav Vidhate
  • 186
  • 1
  • 5