即时通讯使用ASP.NEt进行简单的数据库查询并返回JSON格式数据。 现在我已经显示从数据库中的结果下面的代码:从ASP.NET的GridView中获取JSON数据?
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1"
EmptyDataText="There are no data records to display.">
<Columns>
<asp:BoundField DataField="ScanId" HeaderText="ScanId" ReadOnly="True"
SortExpression="ScanId" />
<asp:BoundField DataField="UserId" HeaderText="UserId"
SortExpression="UserId" />
<asp:BoundField DataField="barcode" HeaderText="barcode"
SortExpression="barcode" />
<asp:BoundField DataField="latitude" HeaderText="latitude"
SortExpression="latitude" />
<asp:BoundField DataField="longitude" HeaderText="longitude"
SortExpression="longitude" />
<asp:BoundField DataField="date_time" HeaderText="date_time"
SortExpression="date_time" />
<asp:BoundField DataField="locatio_name" HeaderText="locatio_name"
SortExpression="locatio_name" />
<asp:BoundField DataField="pos_accuracy" HeaderText="pos_accuracy"
SortExpression="pos_accuracy" />
<asp:BoundField DataField="pos_country" HeaderText="pos_country"
SortExpression="pos_country" />
<asp:BoundField DataField="pos_territory" HeaderText="pos_territory"
SortExpression="pos_territory" />
<asp:BoundField DataField="pos_city" HeaderText="pos_city"
SortExpression="pos_city" />
<asp:BoundField DataField="pos_street" HeaderText="pos_street"
SortExpression="pos_street" />
<asp:BoundField DataField="speed" HeaderText="speed" SortExpression="speed" />
<asp:BoundField DataField="course" HeaderText="course"
SortExpression="course" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:dbConnectionString1 %>"
ProviderName="<%$ ConnectionStrings:dbConnectionString1.ProviderName %>"
SelectCommand="SELECT [ScanId], [UserId], [barcode], [latitude], [longitude], [date_time], [locatio_name], [pos_accuracy], [pos_country], [pos_territory], [pos_city], [pos_street], [speed], [course] FROM [ScanDetails] WHERE [UserId] = '1'">
</asp:SqlDataSource>
我可以用它来获取数据JSON格式化?你能为我指出正确的方向吗?
编辑
我有一个加载返回JSON数据到应用程序中的.aspx移动应用。所以我基本上需要某种Response.Write(json_data); 我不知道如何实现,尽管我整天都在寻找可能的解决方案。 我设法使用ADO.NET实体数据模型,并进行查询是这样的:
在控制我所做的:
公共类ReadController:控制器 {
dbEntities1 _db = new dbEntities1();
//
// GET: /Read/
public ActionResult Index()
{
ViewBag.myData = from c in _db.users select c;
return View();
}
}
鉴于我所做的:
<% foreach (scan_barcode2sql_com.Models.user c in (IEnumerable)ViewBag.myData)
{ %>
<%= c.username %>
<% } %>
你打算如何处理那JSON数据,它呈现为一个表?如果是这样,Gridview将为您提供该功能。如果你的意图是操纵数据集客户端,那么你可能是最好的创建一个Javascript端点连接... – SeanCocteau 2012-03-06 13:55:15
@SeanCocteau谢谢你的答复。我编辑了我的问题上面的更多细节。 – 2012-03-06 14:26:33