2010-05-07 91 views
1

我加載一個js變量是這樣的:奇怪的靜態方法的行爲

var message = '<%= CAnunturi.CPLATA_RAMBURS_INFO %>'; 

其中靜態字符串CPLATA_RAMBURS_INFO我把這樣的:

public static string CPLATA_RAMBURS_INFO = "test"; 

我用它相當不錯,在這個方法。

<script type="text/javascript"> 
    var categoryParam = '<%= CQueryStringParameters.CATEGORY %>'; 
    var subcategoryParam = '<%= CQueryStringParameters.SUBCATEGORY1_ID %>'; 
    var message = '<%= CAnunturi.CPLATA_RAMBURS_INFO %>'; 

    function timedInfo(header) { 
     $.jGrowl(message, { header: header }); 
    }; 
</script> 

所以消息出現。

我不記得,爲什麼,iso的「測試」,如果我從靜態方法中獲取值,則使用消息js var不再成功(消息不再出現)。

public static string CPLATA_RAMBURS_INFO = getRambursInfo(); 

public static string getRambursInfo() 
{ 
    return System.IO.File.ReadAllText(PathsUtil.getRambursPlataFilePath()); 
} 

編輯: 源代碼:

<script type="text/javascript"> 
    var categoryParam = 'category'; 
    var subcategoryParam = 'subcategory1Id'; 
    var message = 'Lorem ipsum dolor sit amet, eu curabitur venenatis 

靈貓pellentesque tortor tempor, 南EST suspendisse,aenean前庭, suspendisse在格言法無eget metus aenean。 在luctus,預期值,也porttitor suscipit NIBH,aenean UT,commodo velit LEO volutpat ullamcorper。 ';

​​
+4

究竟是什麼出了問題?是否有例外? – 2010-05-07 19:44:48

+0

您可以在頁面生成後查看源代碼並查看顯示內容嗎?也許你會看到問題所在。 – 2010-05-07 19:49:34

+0

$ .jGrowl應該顯示一條消息。在它顯示了其良好的第一情況下,在第二,我看到消息變種的源代碼中的內容,但不再顯示的消息... – 2010-05-07 19:49:51

回答

2

你的文件中包含特殊字符(可能是一個新行或')引起渲染JavaScript來包含語法錯誤。

您需要使用Anti-XSS Toolkit逃串,就像這樣:

var message = '<%= AntiXss.JavaScriptEncode(CAnunturi.CPLATA_RAMBURS_INFO) %>'; 

編輯:如果AntiXss沒有幫助,請嘗試以下功能:

public static void QuoteString(this string value, StringBuilder b) { 
    if (String.IsNullOrEmpty(value)) 
     return ""; 

    var b = new StringBuilder(); 
    int startIndex = 0; 
    int count = 0; 
    for (int i = 0; i < value.Length; i++) { 
     char c = value[i]; 

     // Append the unhandled characters (that do not require special treament) 
     // to the string builder when special characters are detected. 
     if (c == '\r' || c == '\t' || c == '\"' || c == '\'' || c == '<' || c == '>' || 
      c == '\\' || c == '\n' || c == '\b' || c == '\f' || c < ' ') { 
      if (b == null) { 
       b = new StringBuilder(value.Length + 5); 
      } 

      if (count > 0) { 
       b.Append(value, startIndex, count); 
      } 

      startIndex = i + 1; 
      count = 0; 
     } 

     switch (c) { 
      case '\r': 
       b.Append("\\r"); 
       break; 
      case '\t': 
       b.Append("\\t"); 
       break; 
      case '\"': 
       b.Append("\\\""); 
       break; 
      case '\\': 
       b.Append("\\\\"); 
       break; 
      case '\n': 
       b.Append("\\n"); 
       break; 
      case '\b': 
       b.Append("\\b"); 
       break; 
      case '\f': 
       b.Append("\\f"); 
       break; 
      case '\'': 
      case '>': 
      case '<': 
       AppendCharAsUnicode(b, c); 
       break; 
      default: 
       if (c < ' ') { 
        AppendCharAsUnicode(b, c); 
       } else { 
        count++; 
       } 
       break; 
     } 
    } 

    if (b == null) { 
     b.Append(value); 
    } 

    if (count > 0) { 
     b.Append(value, startIndex, count); 
    } 

    return b.ToString(); 
} 
+0

我從微軟公司獲得了Axss,導入這個:使用Microsoft.Security.Application;但仍然沒有看到它...你知道爲什麼嗎? – 2010-05-07 20:07:54

+0

請向我們展示源代碼中的'message' var。 – SLaks 2010-05-07 20:16:21

+0

完成............編輯 – 2010-05-07 20:25:38

0

嘗試分配值給Page_Load()函數中的變量。我認爲它會起作用。

+0

這根本行不通。 (這是一個Javascript變量) – SLaks 2010-05-07 19:50:29

+0

我的意思是: public static string CPLATA_RAMBURS_INFO; void Page_Load(object sender,EventArgs e) CPLATA_RAMBURS_INFO = getRambursInfo(); } – Artur 2010-05-07 19:57:34

+0

這沒有幫助。 – SLaks 2010-05-07 20:03:11