【问题标题】:警报对话框从不显示 asp.net ajax 中的转义字符
【发布时间】:2022-01-23 12:20:26
【问题描述】:

我正在开发 ASP.NET AJAX Web 应用程序。

作为要求的一部分,我需要向最终用户显示带有上传文件位置的消息。一切都很好,但警告消息永远不会在路径中显示“/”符号。

对于路径:\\shrestasoft\intranet\CorrectionReports\ReportsWithAccount\CorrectionReportWithAccount-Dec-22-2021-12-31-36-PM.xlsx

以下是我的警报对话框的显示方式:

\shrestasoftintranetCorrectionReportsReportsWithAccountCorrectionReportWithAccount-Dec-22-2021-12-31-36-PM.xlsx

我写了以下代码:

    public static void ShowAlertWithFileLocation(object sender, string message)
    {
        message = "alert('" + message + "');";
        ScriptManager.RegisterClientScriptBlock((sender as Control), typeof(ScriptManager), "alert", message, true);
    }

我尝试使用HtmlUtility.HtmlEncode() 方法,但这对我不起作用。有人可以建议我如何使用路径获取适当的文件名吗?

【问题讨论】:

    标签: javascript asp.net asp.net-ajax


    【解决方案1】:

    \ 被认为是逃生车。但是代替'使用`和String.raw

    例如:

     message = "alert(String.raw`" + message + "`);";
    

    注意 - String.raw 没有 () - 你使用 ` 而不是 '

    因此,您在大多数键盘上使用左到 1 键的 `。

    例如:

     var s = String.raw`power\weight`
     alert(s)
    

    【讨论】:

      【解决方案2】:

      答案很简单,但我花了更多时间才得出这个结论。我们只需要使用new JavaScriptSerializer().Serialize(message); 方法即可。

      以下是我修改后的代码:

          public static void ShowAlertWithFileLocation(object sender, string message)
          {
              message = new JavaScriptSerializer().Serialize(message);
              message = "alert('" + message + "');";
              ScriptManager.RegisterClientScriptBlock((sender as Control), typeof(ScriptManager), "alert", message, true);
          }
      

      【讨论】:

        猜你喜欢
        • 2020-01-14
        • 1970-01-01
        • 1970-01-01
        • 2011-06-17
        • 2023-03-13
        • 1970-01-01
        • 1970-01-01
        • 2020-11-04
        相关资源
        最近更新 更多