从GridView中获取JSON数据的ASP.NET? [英] Get JSON data from GridView in ASP.NET?

查看:178
本文介绍了从GridView中获取JSON数据的ASP.NET?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用ASP.NEt做一个简单的数据库查询并返回JSON格式化数据IM。
现在,我有以下的code从数据库显示结果:

 < ASP:GridView控件ID =GridView1=服务器的AutoGenerateColumns =FALSE
        的DataSourceID =SqlDataSource1
        EmptyDataText =有没有数据记录显示。>
    <柱体和GT;
        < ASP:BoundField的数据字段=ScanId的HeaderText =ScanId只读=真
            SORTEX pression =ScanId/>
        < ASP:BoundField的数据字段=用户ID的HeaderText =用户ID
            SORTEX pression =用户ID/>
        < ASP:BoundField的数据字段=酒吧code的HeaderText =酒吧code
            SORTEX pression =酒吧code/>
        < ASP:BoundField的数据字段=纬度的HeaderText =纬度
            SORTEX pression =纬度/>
        < ASP:BoundField的数据字段=经度的HeaderText =经度
            SORTEX pression =经度/>
        < ASP:BoundField的数据字段=DATE_TIME的HeaderText =DATE_TIME
            SORTEX pression =DATE_TIME/>
        < ASP:BoundField的数据字段=locatio_name的HeaderText =locatio_name
            SORTEX pression =locatio_name/>
        < ASP:BoundField的数据字段=pos_accuracy的HeaderText =pos_accuracy
            SORTEX pression =pos_accuracy/>
        < ASP:BoundField的数据字段=pos_country的HeaderText =pos_country
            SORTEX pression =pos_country/>
        < ASP:BoundField的数据字段=pos_territory的HeaderText =pos_territory
            SORTEX pression =pos_territory/>
        < ASP:BoundField的数据字段=pos_city的HeaderText =pos_city
            SORTEX pression =pos_city/>
        < ASP:BoundField的数据字段=pos_street的HeaderText =pos_street
            SORTEX pression =pos_street/>
        < ASP:BoundField的数据字段=速度的HeaderText =速度SORTEX pression =速度/>
        < ASP:BoundField的数据字段=路线的HeaderText =当然
            SORTEX pression =当然/>
    < /专栏>
    < / ASP:GridView的>
    < ASP:SqlDataSource的ID =SqlDataSource1=服务器
        的ConnectionString =下;%$的ConnectionStrings:dbConnectionString1%>中
        的ProviderName =下;%$的ConnectionStrings:dbConnectionString1.ProviderName%>中
        的SelectCommand =SELECT [ScanId],[用户ID],[巴code],[纬度],[经度],[DATE_TIME],[locatio_name],[pos_accuracy],[pos_country],[pos_territory],[pos_city ],[pos_street],[速],[当然] FROM [ScanDetails],其中[用户ID] =&GT'1';
    < / ASP:SqlDataSource的>

我可以用它来获取数据,JSON格式化?你能指出我朝着正确的方向吗?

修改

我有一个加载返回JSON数据到应用程序在.aspx移动应用。所以基本上,我需要某种形式的Response.Write(json_data)的;
我不知道如何实现,虽然我整天寻找可能的解决方案。
我设法使用ADO.NET实体数据模型,并进行查询是这样的:

在控制器我所做的:

公共类ReadController:控制器
    {

  dbEntities1 _db =新dbEntities1();    //
    // GET:/读/
    公众的ActionResult指数()
    {
        ViewBag.myData =从C在_db.users选择C;
        返回查看();
    }}

针对我所做的:

 <%的foreach(scan_bar code2sql_com.Models.user℃的(IEnumerable的)ViewBag.myData)
{%GT;
   &所述;%= c.username%GT;
<%}%GT;


解决方案

只是为了完整性...

在GridView呈现出一个HTML表,同时它的jQuery / JavaScript的访问收获它的数据,很容易简单地创建你的ASP.NET应用程序中的端点,可以返回JSON数据。这里有一些方法,我个人最喜欢的是jQuery来ASP.NET MVC,你可以创建一个控制器来处理和渲染HTML输出和JSON数据相同的模型。


  1. ASP.NET MVC:端到端Jquery的,再如使用 JsonResult 对象和更广例如

  2. 创建一个WCF服务。在这里,您可以创建可从您的网站independantly运行一个Web服务,但是,可以验证相比,ASP.NET MVC时要绑定。例子来自 MSDN ,使用的一个简单的DataContract (国米$ .NET对象应该是什么夹住的p $点输出(查找数据传输对象[DTO]进一步阅读)下面是一个的演练为好。

  3. 您$​​ C $ C范围内使用的WebMethod后面。也许少影响力的老同学ASP.NET开发人员在那里你可以定义与WEBMETHOD属性的方法,将作为一个Javascript端点。它可以是一个有点繁琐(特别是相对于MVC),但可能需要较少的时间执行。例子包括:<一href=\"http://www.seoasp.net/post/2008/07/16/jQuery-To-Call-ASPNET-Page-Methods-and-Web-Services.aspx\"相对=nofollow>简单的调用和和东西有点<一个href=\"http://weblogs.asp.net/craigshoemaker/archive/2008/11/07/using-jquery-to-call-asp-net-ajax-page-methods-by-example.aspx\"相对=nofollow>更复杂。有一件事我会提的是,你没有得到任何自动的Json连载(我认为)这个技术,你可能必须自己执行序列如本<一个href=\"http://stackoverflow.com/questions/7178839/how-to-pass-a-timespan-object-to-a-wcf-method-using-json\">post.

心连心

im using ASP.NEt to make a simple database query and return JSON formated data. Right now I have the following code which displays the results from database:

<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>

Can I use this to get data JSON formated? Can you point me in the right direction?

EDIT

I have a mobile app that loads the .aspx which returns JSON data to the app. So I basically need some sort of Response.Write(json_data); I dont know how to implement that, although I'm searching for possible solutions all day long. I managed to use ADO.NET Entity Data Model and to make queries this way:

in controller i did:

public class ReadController : Controller {

    dbEntities1 _db = new dbEntities1();

    //
    // GET: /Read/
    public ActionResult Index()
    {
        ViewBag.myData = from c in _db.users select c;
        return View();
    }

}

in view i did:

<% foreach (scan_barcode2sql_com.Models.user c in (IEnumerable)ViewBag.myData)
{ %>
   <%= c.username %>
<% } %>

解决方案

Just for completeness...

The Gridview renders out a HTML table and while it is accessible by Jquery / Javascript to harvest its data, it is easier to simply create an endpoint within your ASP.NET application that can return JSON data. Here are some approaches, my personal favourite would be jQuery to ASP.NET MVC as you can create a controller to handle and render HTML output and JSON data to same model.

  1. ASP.NET MVC : End to end Jquery, Another example using JsonResult object and a broader example
  2. Create a WCF Service. Here you can create a webservice that can run independantly from your website, however, authentication can be a bind when compared to ASP.NET MVC. Examples are from MSDN, using a simple DataContract (that interprets between .NET objects and what should be outputted (lookup Data Transfer Objects [DTO] for further reading). Here's a walkthrough as well.
  3. Using WebMethod within your code behind. Probably the less impactful for old school ASP.NET developers where you can define methods with the 'Webmethod' attribute that will act as a Javascript Endpoint. It can be a bit fiddly (especially compared to MVC) but may take less time on the implementation. Examples are include simple calls and this and something a bit more complex. One thing I would mention is that you do not get any automatic Json serialising (I think) with this technique and you may have to perform the serialisation yourself as shown in this post.

HTH

这篇关于从GridView中获取JSON数据的ASP.NET?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆