.Net WebService + ExtJs填充网格

时间:2012-07-03 04:36:18

标签: c# extjs extjs4.1

我已经使用ExtJs和.NET webservice方法创建了一个小程序来填充gridPanel,但它没有填充..我的代码在下面

//这是我的网络服务方法

    [WebMethod(EnableSession = true)]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = true, XmlSerializeString = false)]
        public List<Student> GetStudentList()
        {
            List<Student> obList = new List<Student>();
            obList.Add(new Student(1, "John", "Doe"));
            obList.Add(new Student(2, "Michael", "Crowd"));
            obList.Add(new Student(3, "Gunnar", "Rasmundsson"));
            obList.Add(new Student(4, "Alicia", "Mankind"));
            return obList;
        }

    public class Student
    {
        private int _stid;
        private string _stname;
        private string _stservice;

        public Student(){}
        public Student(int stid, string stname, string stservice)
        {
            this.StId = stid;
            this.StName = stname;
            this.StService = stservice;
        }

        public int StId {
            get { return this._stid; }
            set { this._stid = value; } 
        }
        public string StName { 
            get{return this._stname;}
            set { this._stname = value; }
        }
        public string StService { get { return this._stservice; } set { this._stservice = value; } }
    }

//这是我的ExtJs网格填充代码

        Ext.onReady(function () {
            var myStore = new Ext.data.JsonStore({
                // Load data at once
                autoLoad: true,
                // Override default http proxy settings
                proxy: new Ext.data.HttpProxy({
                    // Call web service method using GET syntax
                    url: 'GetStudent.asmx/GetStudentList',
                    // Ask for Json response
                    headers: { 'Content-type': 'application/json' }
                }),
                // Root variable 
                root: 'd',
                // Record identifier
                id: 'StId',
                //reader:Jreader,
                // Fields declaration
                fields: ['StId', 'StName', 'StService'],
            });


            var grid = new Ext.grid.GridPanel({
                // Set store
                store: myStore,
                // Columns definition
                columns: [
                    { dataIndex: 'StId', header: 'St Id' },
                    { dataIndex: 'StName', header: 'St Name' },
                    { dataIndex: 'StService', header: 'St Service' }
                ],
                // Render grid to dom element with id set to panel
                renderTo: 'divGrid',
                width: 422,
                height: 300
            });
        });                

我还包括

    <body>
        <form id="form1" runat="server">
            <asp:ScriptManager ID="ScriptManager1" runat="server">
                <Services>
                    <asp:ServiceReference Path="~/GetStudent.asmx" />
                </Services>
            </asp:ScriptManager>
            <div id="divGrid"></div>
        </form>
    </body>

请让我知道我哪里错了,谢谢你的帮助!!!!

2 个答案:

答案 0 :(得分:2)

您的代码似乎很好。你有没有登上firefox-firebug?只需查看流量并查看您获得的错误。

编辑:添加代码。

以下是基于您给出的示例的完整工作示例:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>ExtJS ASP.NET WebService</title>

    <script type="text/javascript" src="js/ext/adapter/ext/ext-base.js"></script>
    <script type="text/javascript" src="js/ext/ext-all.js"></script>
    <link rel="stylesheet" href="js/ext/resources/css/ext-all.css"/>
    <script type="text/javascript">

        Ext.onReady(function () {
            var myStore = new Ext.data.JsonStore({
                // Load data at once
                autoLoad: true,
                // Override default http proxy settings
                proxy: new Ext.data.HttpProxy({
                    // Call web service method using GET syntax
                    url: 'Service.asmx/GetStudentList',
                    // Ask for Json response
                    headers: { 'Content-type': 'application/json' }
                }),
                // Root variable 
                root: 'd',
                // Record identifier
                id: 'StId',
                //reader:Jreader,
                // Fields declaration
                fields: ['StId', 'StName', 'StService'],
            });


            var grid = new Ext.grid.GridPanel({
                // Set store
                store: myStore,
                // Columns definition
                columns: [
                    { dataIndex: 'StId', header: 'St Id' },
                    { dataIndex: 'StName', header: 'St Name' },
                    { dataIndex: 'StService', header: 'St Service' }
                ],
                // Render grid to dom element with id set to panel
                renderTo: 'divGrid',
                width: 422,
                height: 300
            });
        });                
    </script>
</head>
<body>
  <div id="divGrid"></div>
</body>
</html>

Service.asmx代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;

namespace WebService4ExtJS
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [ScriptService]
    public class Service : System.Web.Services.WebService
    {
        [WebMethod(EnableSession = true)]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = true, XmlSerializeString = false)]
        public List<Student> GetStudentList()
        {
            List<Student> obList = new List<Student>();
            obList.Add(new Student(1, "John", "Doe"));
            obList.Add(new Student(2, "Michael", "Crowd"));
            obList.Add(new Student(3, "Gunnar", "Rasmundsson"));
            obList.Add(new Student(4, "Alicia", "Mankind"));
            return obList;
        }
    }

    public class Student
    {
        private int _stid;
        private string _stname;
        private string _stservice;

        public Student() { }
        public Student(int stid, string stname, string stservice)
        {
            this.StId = stid;
            this.StName = stname;
            this.StService = stservice;
        }

        public int StId
        {
            get { return this._stid; }
            set { this._stid = value; }
        }
        public string StName
        {
            get { return this._stname; }
            set { this._stname = value; }
        }
        public string StService { get { return this._stservice; } set { this._stservice = value; } }
    }
}

区别是html代码的部分。

编辑:添加解决方案(由Nitin Soni在评论中提供,添加到此处,以便有人搜索时不要混淆)

你可以看到Nitin在评论中说这个问题是ExtJS 4.1版特有的:

  

我检查了你的代码并做了同样的事情,是的,它也适用于我..我   进一步分析了这个问题与ExtJs的版本有关。你的代码   (代码项目代码)使用Extjs 2.2库文件,而我有   使用了Extjs 4.1最新的库文件。似乎问题在于   特定的ext-all.js文件版本。我用旧的替换了这个文件   版本,它的工作原理。

他还提供解决方案:(再次发表评论)

  

我找到了使用Extjs 4.1的解决方案。我也必须使用   Store方法阅读器中的以下代码行:{type:&#39; json&#39;,root:   &#39; d&#39; }

答案 1 :(得分:0)

以下是我的测试。出于测试目的,我将数据目录设置为与其他目录(例如,app)相同的级别。然后,我将Web服务文件移至该目录中。 我没有触摸index.html中的任何内容。

我还在WebMethod中添加了两行JSON格式的代码。

JavaScriptSerializer js = new JavaScriptSerializer();
Context.Response.Write(js.Serialize(obList));

对于此更改,我还将WebMethod中Student的返回类型更改为void。

以下是我编写的app.js代码。

Ext.onReady(function () {
Ext.create("Ext.panel.Panel", {
    width: 800,
    height: 500,
    renderTo: Ext.getBody(),
    layout: 'fit',
    items: [{
        xtype: 'grid',            
        columns: [{
            text: 'Id',
            dataIndex: 'StId',
            flex: 1
        }, {
            text: 'Name',
            dataIndex: 'StName',
            flex: 1
        }, {
            text: 'Service',
            dataIndex: 'StService',
            flex: 1
        }],
        store: {
            autoLoad: true,
            fields: ['StId', 'StName', 'StService'],               
            proxy: {
                type: 'ajax',
                url: './data/GetStudent.asmx/GetStudentList',
                reader: {
                    type: 'json',
                    rootProperty: 'd'
                }
            }
        }
    }]
})

});

对我有用。