2012-03-06 108 views
0

即時通訊使用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 %> 
<% } %> 
+0

你打算如何處理那JSON數據,它呈現爲一個表?如果是這樣,Gridview將爲您提供該功能。如果你的意圖是操縱數據集客戶端,那麼你可能是最好的創建一個Javascript端點連接... – SeanCocteau 2012-03-06 13:55:15

+0

@SeanCocteau謝謝你的答覆。我編輯了我的問題上面的更多細節。 – 2012-03-06 14:26:33

回答

1

只是爲了完整性...

Gridview呈現出一張HTML表格,雖然JQuery/Javascript可以獲取它的數據,但在ASP.NET應用程序中簡單地創建一個可以返回JSON數據的端點會更容易。這裏有一些方法,我個人最喜歡的是jQuery到ASP.NET MVC,因爲你可以創建一個控制器來處理和呈現HTML輸出和JSON數據到相同的模型。

  1. ASP.NET MVC:End to end Jquery,使用JsonResult對象和broader example
  2. 創建WCF服務又如。在這裏,您可以創建一個可以從您的網站獨立運行的web服務,但是,與ASP.NET MVC相比,身份驗證可以是綁定。例子是從MSDN,使用a simple DataContract(解釋.NET對象之間和應該輸出什麼(查找數據傳輸對象[DTO]進一步閱讀),這也是一個walkthrough以及
  3. 在你的代碼後面使用WebMethod。對老派的ASP.NET開發人員來說影響不大,在那裏你可以使用'Webmethod'屬性來定義方法,這個屬性可以作爲一個Javascript端點,它可能有點複雜(特別是與MVC相比),但是在實現上可能會花費更少的時間。包括simple callsthis以及more complex有一點我會提到的是,你沒有得到任何自動的Json序列化(我認爲)這種技術,你可能必須自己執行序列化,如post所示

HTH

+0

謝謝。今天我遇到了HTTP Handlers並使用了.ASHX,這看起來正是我所需要的。 – 2012-03-08 00:17:36