1

在起始页中:当客户端单击 LinkBut​​ton 时,我想将目录路径传递给弹出窗口页面lbtnEditText

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

以及背后的代码:

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

    }

queryString确切地说是= "editpage.aspx?path=D:\\C#Projects\\website\\Lecturer\\giangvien\\profile.xml"(我在调试时检查它)

但在目标页面(弹出窗口):editpage.aspx

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

它有一个错误:Could not find file 'D:\C#Projects\website\C 尝试调试,path我收到的只是:"D:C"

并在editpage.aspx显示的地址栏中:

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

帮助!!!为什么当我将它传递给editpage时路径会改变???

4

3 回答 3

2

发生这种情况的原因是 :: 您正在传递查询字符串数据,其中包含意外字符“\,#”。解决方案是在设置为查询字符串值之前对这些值进行转义和编码

于 2013-10-24T07:22:25.950 回答
2

不幸的是,任何进行 Web 开发的人都需要正确编码 Urls 的技能......

之后的所有内容都是#Url 的“哈希”部分,浏览器不需要将其发送到服务器。更正式的名称是片段标识符

您需要做的是path正确编码查询参数的值(即使用encodeURIComponentJavaScript 中的函数)。

于 2013-10-24T07:23:34.627 回答
1

为您提供 C# 的实际解决方案:

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

请参阅编码参考http://msdn.microsoft.com/en-us/library/system.web.httputility.urlencode.aspx

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

于 2013-10-24T07:34:13.357 回答