2010-06-28 99 views
0

我有我的的.aspx的代碼隱藏以下屬性:字符串是未定義

protected string CurrentProductView 
    { 
     get 
     { 
      string viewName = string.Empty; 
      viewName = Enum.GetName(typeof(ProductView), currentProdView); 
      return viewName; 
     } 
    } 

在我的.aspx我有一些JavaScript試圖引用這個字符串:

$(document).ready(function() 
{ 
    var action = <%=CurrentProductView %> 

      $("#recommendations").load("recommendationsHandler.ashx?action=" + action + "item&csid=" + csid + "&productID=" + productID, function() 
     { 
      $("#recommendationsView").show(); 
     }); 
}); 

但由於某種原因,我得到「項目未定義」。

當我調試這個,我肯定看到一個字符串回來viewName。那麼爲什麼它會抱怨如果一個字符串回來?!?!

+0

你能分享與Javascript交互的代碼? – 2010-06-28 15:08:55

+1

你不應該在字符串周圍有引號嗎? 'var action =「<%= CurrentProductView%>」;' – 2010-06-28 15:09:16

+0

不,它引用了服務器端屬性。 – PositiveGuy 2010-06-28 15:10:04

回答

5

更改此:

var action = <%=CurrentProductView %> 

這樣:

var action = "<%=CurrentProductView %>" 

既然你打印出一個字符串值的頁面到一個變量,你需要周圍的引號,因爲該值觀看作爲頁面上JavaScript的字面值。你不需要圍繞整數引號,因爲在JavaScript中這是合法的:

var my_number = 4; 

如果做不到

var my_string = this is a string; 

,它需要是這樣的:

var my_string = "this is a string"; 
+0

更新我想我不明白爲什麼......因爲它是jQuery?因爲我們已經定義了類型爲int的其他變量,如下所示:var productID = <%= ProductID%>; – PositiveGuy 2010-06-28 15:12:11

+0

由於產品ID是一個i​​nt,並且在JavaScript中,整數不需要定義圍繞它們的引號。 – kemiller2002 2010-06-28 15:12:39

+0

不是我在想什麼,但我對aspx不夠熟悉,無法確定。 (例如,字符串值可能是自動引用的,例如。) – 2010-06-28 15:12:47

相關問題