ID #6387

ExtJs+WCF+LINQ实现分页Grid(1)

上篇文章《用ExtJs+Linq+Wcf打造简单grid 》,这个网格控件不带分页,本 文在上文的基础上添加分页功能,文中会着重介绍如何在用LINQ返回分页数据, 如何使ExtJs与WCF进行Restful交互,如何在页面中添加一个带有分页功能的 ExtJS的Grid控件。

废话也不多说了,本文仍然是实战学习系列,没有太 多理论讲解,只是作者学习中的操作流程记录而已,如果日后有机会,会逐渐的 深入一些。

第一步:在vs2008中创建一个支持.Net Framework 3.5,名 称为:ExtJs_Wcf_Linq_PageGrid的Asp.Net网站,

完成后删除项目模板中的default.aspx页面。此步骤完成之后的效果 图如下:

第二步:创建网站之后,将ExtJs相关资源文件添加到项目中,这些文 件主要来源是extjs的官方示例项目,完成后项目效果图为:

第三步:在本文的示例中,我们使用SQL2005自带的示例数据库 AdventureWorks中的数据表Product,默认情况下该示例数据库可能未安装,要 安装此数据库,可以查阅SQL2005中文档与教程中的SQL2005联机文档。在这个步 骤中,主要目的是在项目创建一个Lint to Xml类,这个类用于Linq与数据表 Product进行交互。使用Linq To Sql可以产生一个实体类和一个数据访问类。添 加该类的操作如下:

创建好Products.dbml之后,打开vs2008的服务器资源管理器,在服务 器资源管理器中添加对数据库AdventureWorks的数据连接,然后将该库中数据表 Product拖到Products.dbml设计器的左面的对象关系设计器面板中,操作如下图 所示:

拖动成功之后,便有vs2008的IDE自动生成了有关Product的实体类和 linq操作数据表Product的操作类:ProductsDataContext,在可视化界面中也能 有如下的显示:

自动生成的实体类Product并不支持作为WCF的数据类,必须手动对其 添加DataContract和DataMember,添加后的代码如下:

linq to sql类生成的代码并添加了wcf支持
#pragma warning disable 1591
//----------------------------------------------------------------- -------------
// 
// 此代码由工具生成。
// 运行库版 本:2.0.50727.1433
//
// 对此文件的更改可能会导致不正确的行为 ,并且如果
// 重新生成代码,这些更改将会丢失。
// 
// --------------------------------------------------------------------- ---------

namespace ExtJs_Wcf_Linq_PageGrid
{
   using System.Data.Linq;
  using System.Data.Linq.Mapping;
  using System.Data;
  using System.Collections.Generic;
  using System.Reflection;
  using System.Linq;
   using System.Linq.Expressions;
  using System.ComponentModel;
  using System;
using System.Runtime.Serialization;
   [System.Data.Linq.Mapping.DatabaseAttribute (Name="AdventureWorks")]
  public partial class ProductsDataContext : System.Data.Linq.DataContext
  {
     private static System.Data.Linq.Mapping.MappingSource mappingSource = new AttributeMappingSource();
Extensibility Method Definitions#region Extensibility Method Definitions
partial void OnCreated();
partial void InsertProduct(Product instance);
partial void UpdateProduct(Product instance);
partial void DeleteProduct(Product instance);
#endregion
    public ProductsDataContext() :
        base (global::System.Configuration.ConfigurationManager.ConnectionStrings ["AdventureWorksConnectionString"].ConnectionString, mappingSource)
    {
      OnCreated();
     }
    public ProductsDataContext(string connection) :
        base(connection, mappingSource)
    {
      OnCreated();
    }
    public ProductsDataContext(System.Data.IDbConnection connection) :
         base(connection, mappingSource)
    {
       OnCreated();
    }
    public ProductsDataContext(string connection, System.Data.Linq.Mapping.MappingSource mappingSource) :
         base(connection, mappingSource)
    {
       OnCreated();
    }
    public ProductsDataContext (System.Data.IDbConnection connection, System.Data.Linq.Mapping.MappingSource mappingSource) :
         base(connection, mappingSource)
    {
       OnCreated();
    }
    public System.Data.Linq.Table<Product> Product
    {
       get
      {
        return this.GetTable<Product>();
      }
    }
  }

[Table(Name="Production.Product")]

[DataContract]
  public partial class Product : INotifyPropertyChanging, INotifyPropertyChanged
  {
     private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);
    private int _ProductID;
    private string _Name;
    private string _ProductNumber;
    private bool _MakeFlag;
     private bool _FinishedGoodsFlag;
    private string _Color;
    private short _SafetyStockLevel;
     private short _ReorderPoint;
    private decimal _StandardCost;
    private decimal _ListPrice;
     private string _Size;
    private string _SizeUnitMeasureCode;
    private string _WeightUnitMeasureCode;
    private System.Nullable<decimal> _Weight;
    private int _DaysToManufacture;
    private string _ProductLine;
     private string _Class;
    private string _Style;
    private System.Nullable<int> _ProductSubcategoryID;
    private System.Nullable<int> _ProductModelID;
     private System.DateTime _SellStartDate;
    private System.Nullable<System.DateTime> _SellEndDate;
     private System.Nullable<System.DateTime> _DiscontinuedDate;
    private System.Guid _rowguid;
    private System.DateTime _ModifiedDate;
Extensibility Method Definitions#region Extensibility Method Definitions
partial void OnLoaded();
partial void OnValidate(System.Data.Linq.ChangeAction action);
partial void OnCreated();
partial void OnProductIDChanging(int value);
partial void OnProductIDChanged ();
partial void OnNameChanging(string value);
partial void OnNameChanged();
partial void OnProductNumberChanging(string value);
partial void OnProductNumberChanged();
partial void OnMakeFlagChanging(bool value);
partial void OnMakeFlagChanged ();
partial void OnFinishedGoodsFlagChanging(bool value);
partial void OnFinishedGoodsFlagChanged();
partial void OnColorChanging(string value);
partial void OnColorChanged();
partial void OnSafetyStockLevelChanging(short value);
partial void OnSafetyStockLevelChanged();
partial void OnReorderPointChanging(short value);
partial void OnReorderPointChanged();
partial void OnStandardCostChanging (decimal value);
partial void OnStandardCostChanged();
partial void OnListPriceChanging(decimal value);
partial void OnListPriceChanged();
partial void OnSizeChanging(string value);
partial void OnSizeChanged();
partial void OnSizeUnitMeasureCodeChanging(string value);
partial void OnSizeUnitMeasureCodeChanged();
partial void OnWeightUnitMeasureCodeChanging(string value);
partial void OnWeightUnitMeasureCodeChanged();
partial void OnWeightChanging (System.Nullable<decimal> value);
partial void OnWeightChanged();
partial void OnDaysToManufactureChanging(int value);
partial void OnDaysToManufactureChanged();
partial void OnProductLineChanging(string value);
partial void OnProductLineChanged();
partial void OnClassChanging(string value);
partial void OnClassChanged();
partial void OnStyleChanging(string value);
partial void OnStyleChanged();
partial void OnProductSubcategoryIDChanging (System.Nullable<int> value);
partial void OnProductSubcategoryIDChanged();
partial void OnProductModelIDChanging(System.Nullable<int> value);
partial void OnProductModelIDChanged();
partial void OnSellStartDateChanging(System.DateTime value);
partial void OnSellStartDateChanged();
partial void OnSellEndDateChanging (System.Nullable<System.DateTime> value);
partial void OnSellEndDateChanged();
partial void OnDiscontinuedDateChanging (System.Nullable<System.DateTime> value);
partial void OnDiscontinuedDateChanged();
partial void OnrowguidChanging (System.Guid value);
partial void OnrowguidChanged();
partial void OnModifiedDateChanging(System.DateTime value);
partial void OnModifiedDateChanged();
#endregion
     public Product()
    {
      OnCreated();
     }
    [DataMember]
    [Column (Storage="_ProductID", AutoSync=AutoSync.OnInsert, DbType="Int NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)]
    public int ProductID
     {
      get
      {
        return this._ProductID;
      }
      set
       {
        if ((this._ProductID != value))
         {
          this.OnProductIDChanging (value);
          this.SendPropertyChanging();
           this._ProductID = value;
           this.SendPropertyChanged("ProductID");
           this.OnProductIDChanged();
        }
       }
    }

[DataMember]

[Column (Storage="_Name", DbType="NVarChar(50) NOT NULL", CanBeNull=false)]
    public string Name
    {
      get
      {
        return this._Name;
      }
      set
       {
        if ((this._Name != value))
         {
          this.OnNameChanging(value);
           this.SendPropertyChanging();
           this._Name = value;
          this.SendPropertyChanged ("Name");
          this.OnNameChanged();
        }
      }
    }

[DataMember]

[Column(Storage="_ProductNumber", DbType="NVarChar(25) NOT NULL", CanBeNull=false)]
     public string ProductNumber
    {
      get
      {
        return this._ProductNumber;
      }
      set
      {
         if ((this._ProductNumber != value))
        {
          this.OnProductNumberChanging(value);
           this.SendPropertyChanging();
           this._ProductNumber = value;
           this.SendPropertyChanged("ProductNumber");
           this.OnProductNumberChanged();
        }
       }
    }

[DataMember]

[Column(Storage="_MakeFlag", DbType="Bit NOT NULL")]
    public bool MakeFlag
    {
       get
      {
        return this._MakeFlag;
      }
      set
       {
        if ((this._MakeFlag != value))
         {
          this.OnMakeFlagChanging (value);
          this.SendPropertyChanging();
           this._MakeFlag = value;
           this.SendPropertyChanged("MakeFlag");
           this.OnMakeFlagChanged();
        }
       }
    }

[DataMember]

[Column (Storage="_FinishedGoodsFlag", DbType="Bit NOT NULL")]
    public bool FinishedGoodsFlag
     {
      get
      {
        return this._FinishedGoodsFlag;
      }
      set
      {
        if ((this._FinishedGoodsFlag != value))
        {
           this.OnFinishedGoodsFlagChanging(value);
           this.SendPropertyChanging();
           this._FinishedGoodsFlag = value;
           this.SendPropertyChanged("FinishedGoodsFlag");
           this.OnFinishedGoodsFlagChanged();
        }
      }
    }

[DataMember]

[Column(Storage="_Color", DbType="NVarChar(15)")]
    public string Color
    {
      get
      {
         return this._Color;
      }
      set
      {
        if ((this._Color != value))
         {
          this.OnColorChanging (value);
          this.SendPropertyChanging();
           this._Color = value;
           this.SendPropertyChanged("Color");
           this.OnColorChanged();
        }
      }
    }

[DataMember]

[Column (Storage="_SafetyStockLevel", DbType="SmallInt NOT NULL")]
    public short SafetyStockLevel
     {
      get
      {
        return this._SafetyStockLevel;
      }
      set
      {
        if ((this._SafetyStockLevel != value))
        {
           this.OnSafetyStockLevelChanging(value);
           this.SendPropertyChanging();
           this._SafetyStockLevel = value;
           this.SendPropertyChanged("SafetyStockLevel");
           this.OnSafetyStockLevelChanged();
        }
      }
    }

[DataMember]

[Column(Storage="_ReorderPoint", DbType="SmallInt NOT NULL")]
    public short ReorderPoint
    {
      get
      {
        return this._ReorderPoint;
      }
      set
      {
        if ((this._ReorderPoint != value))
        {
           this.OnReorderPointChanging(value);
           this.SendPropertyChanging();
           this._ReorderPoint = value;
           this.SendPropertyChanged("ReorderPoint");
           this.OnReorderPointChanged();
        }
       }
    }

[DataMember]

[Column(Storage="_StandardCost", DbType="Money NOT NULL")]
    public decimal StandardCost
     {
      get
      {
        return this._StandardCost;
      }
      set
       {
        if ((this._StandardCost != value))
        {
           this.OnStandardCostChanging(value);
           this.SendPropertyChanging();
           this._StandardCost = value;
           this.SendPropertyChanged("StandardCost");
           this.OnStandardCostChanged();
        }
       }
    }

[DataMember]

[Column(Storage="_ListPrice", DbType="Money NOT NULL")]
    public decimal ListPrice
    {
      get
      {
        return this._ListPrice;
      }
      set
       {
        if ((this._ListPrice != value))
         {
          this.OnListPriceChanging (value);
          this.SendPropertyChanging();
           this._ListPrice = value;
           this.SendPropertyChanged("ListPrice");
           this.OnListPriceChanged();
        }
       }
    }

[DataMember]

[Column (Storage="_Size", DbType="NVarChar(5)")]
     public string Size
    {
      get
       {
        return this._Size;
      }
      set
      {
        if ((this._Size != value))
        {
           this.OnSizeChanging(value);
           this.SendPropertyChanging();
          this._Size = value;
          this.SendPropertyChanged ("Size");
          this.OnSizeChanged();
        }
      }
    }

[DataMember]

[Column (Storage="_SizeUnitMeasureCode", DbType="NChar(3) ")]
    public string SizeUnitMeasureCode
     {
      get
      {
        return this._SizeUnitMeasureCode;
      }
      set
      {
        if ((this._SizeUnitMeasureCode != value))
        {
           this.OnSizeUnitMeasureCodeChanging(value);
           this.SendPropertyChanging();
           this._SizeUnitMeasureCode = value;
           this.SendPropertyChanged("SizeUnitMeasureCode");
           this.OnSizeUnitMeasureCodeChanged();
         }
      }
    }


2011-07-01 18:48
阅读:
I'm VC , Just U know Y
本站部分文章来源于互联网,版权归原作者所有。

延伸阅读:

ExtJS+WCF+LINQ打造全功能Grid

ExtJs学习笔记(6)_可分页的GridPanel

ExtJS与.NET结合开发实例(Grid之数据显示、分页、排

ExtJs学习笔记(22)-XTemplate + WCF 打造无刷新数据分页

ExtJs的.NET控件----YuiGrid(分页/在线编辑)