2010-08-05 76 views
1

我有一個列表,我循環填充我的表。我想將一行數據傳遞給我的JavaScript代碼。如果沒有,我想通過列表和ID號在列表中搜索該行。我怎樣才能做到這一點?如何將aspx中的列表傳遞給javascript?

<%foreach(var item in Model.NewList) { %> 
<tr> 
    <td><%=item.EntryDate.ToShortDateString() %></td> 
    <td onmouseover="showDetailsHover(<%=item %>,<%=item.idNumber%>);" 
     onmouseout="hideDetailsHover();"><%=Html.ActionLink(item.idNumber,"SummaryRedirect/" + item.idNumber) %></td> 
</tr> 
<% } %> 

回答

0

感謝您的想法。我終於挖得更深一些,您的建議,我用一個在名單上圈我通過那麼我的數據添加到基於環行...

success: function(data) { 
     var loopList = data.message.NewList; 
     for (var i = 0; i < loopList.length; i++) { 
      addRecentData(loopList[i]); 
     } 
    }, 
}); 
function addRecentData(data) { 
    .... 
} 

感謝微調!

1

的「從ASPX傳遞一個列表的javascript」的概念是有點困難的,因爲你的ASP.NET代碼來理解在服務器上運行,JavaScript代碼在瀏覽器中運行。因爲它們存在於不同的域中,所以不能簡單地將列表從一個域「傳遞」到另一個域。

但是,你們有幾個選項:

  • 揭露,你可以使用JavaScript訪問Web服務。 Web服務可以負責提供數據行,以便JavaScript可以理解它。
  • 當您的頁面加載時,將靜態格式的JSON數據直接放入您的javascript函數中。 JSON是JavaScript可以理解的格式。雖然從技術上講,這不是將變量「傳遞」到ASP.NET的JavaScript函數中,但它會說「這是我在javascript函數中運行的數據,當它運行在客戶端上時」。
1

我能想到的最快捷的方法是這樣的:

  1. 使用Json.Net連載列表爲網頁的JSON字符串。
  2. 包括jQueryjQuery-json插件。
  3. 在javascript函數中定義一個javascript列表。

像這樣的事情你的aspx頁面上:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> 
<script type="text/javascript" src="http://jquery-json.googlecode.com/files/jquery.json-2.2.js"></script> 
<script type="text/javascript"> 
    function foo() { 
     // This is where we use the Json.Net library 
     var rawJsonString = '<%= Newtonsoft.Json.JsonConvert.SerializeObject(Model.NewList) %>'; 

     // This is where we use the jQuery and jQuery-json plugin 
     var list = $.evalJSON(rawJsonString); 

     // Do stuff with your list here 
    } 
</script> 
相關問題