2010-08-05 50 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> 
相关问题