1

In start page: I want to pass a directory path to a popup window page when client click on LinkButton lbtnEditText

<asp:LinkButton  ID="lbtnEditText" runat="server" Text="Edit Text" CommandArgument='<%# Eval("Path") + "," + Eval("Name")%>' OnCommand="Button1_Click"></asp:LinkButton>

And the code behind:

protected void Button1_Click(object sender, CommandEventArgs e)
    {
        string[] values = e.CommandArgument.ToString().Split(',');

        string queryString =

            "editpage.aspx?path="

            + values[0];

        string newWin =

            "window.open('" + queryString + "');";

        ClientScript.RegisterStartupScript

            (this.GetType(), "pop", newWin, true);

    }

The queryString exactly is = "editpage.aspx?path=D:\\C#Projects\\website\\Lecturer\\giangvien\\profile.xml" (I check it when I debug)

But In destination page (popup window): editpage.aspx

    string path = Request.QueryString["path"];
    string content = File.ReadAllText(path);
    if(content!=null)
      textarea.Value = content;

It has an error: Could not find file 'D:\C#Projects\website\C Try to debug, the path I recieved is just only : "D:C"

And in the address bar of editpage.aspx display:

http://localhost:41148/website/editpage.aspx?path=D:C#ProjectswebsiteLecturergiangvienprofile.xml

Help!!! Why is the path changed when I pass it to the editpage???

vyclarks
  • 854
  • 2
  • 15
  • 39

3 Answers3

2

The reason behind happening so is :: you are passing query string data which is having unexpected characters which are '\,#'. The solution to this is escape and encode this values before setting as query string values

Hiren Desai
  • 941
  • 1
  • 9
  • 33
2

Encoding Urls correctly is unfortunately required skill for anyone doing web development...

Everything after # is "hash" portion of Url and browser don't need to send it to server. More formal name is fragment identifier.

What you need to do is encode value of path query parameter correctly (i.e. with encodeURIComponent funcition in JavaScript).

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
1

To give you the actual solution for C#:

string queryString = "editpage.aspx?path=" + System.Web.HttpUtility.UrlEncode(values[0]);

see reference for encoding http://msdn.microsoft.com/en-us/library/system.web.httputility.urlencode.aspx

and decoding http://msdn.microsoft.com/en-us/library/system.web.httputility.urldecode%28v=vs.110%29.aspx

Filburt
  • 17,626
  • 12
  • 64
  • 115