65.9K
CodeProject is changing. Read more.
Home

GridViewExpand(To be with ORMPPING ActiveReord,sorting,paging)

starIconstarIconstarIconemptyStarIconemptyStarIcon

3.00/5 (3 votes)

Aug 27, 2008

CPOL

3 min read

viewsIcon

19482

downloadIcon

184

GridViewExpand(To be with ORMPPING ActiveReord,sorting,paging)

Introduction

The GridViewExpand integrated ORMMAPING (ActiveRecord,Nhibernate), sorting and paging function.

Background

ASP.NET used code-behind style to develop.The design pattern seems could be between MODEL1 servlet AND Model 2 Struts (http://www.javaworld.com/javaworld/jw-12-1999/jw-12-ssj-jspmvc.html).

When we add paging and sorting to GridView, We must to write a lot of similar codes which are too difficult to do auto-test to every GridView. Fatally, GridView of .NET framework do not implement good paging and sorting because that Gridview select all of the data before paging or sorting the data. That influrence the capability when the data is huge. Though we use ORM instead of dataset to build DAO of project clear, we endure the weakless of it still.

I rewrote the GridView Control to make it more efficient and easier to coding when do paging and sorting with ORMapping.

Using the Code

The sample solution is made up by three projects.

GridViewExpandCastleBase build base query by ActiveRecord

GridViewExpand expand the sorting and paging

WebTest shows the usage

The WebTest of the solution base on Person What maps to northWind on MS SqlServer.You may change the connect string in Web.Config to run WebTest.

Write Person.cs map to table Customers in NorthWind.

/// <summary>
///Person 
/// </summary>
[ActiveRecord("Customers")]
public class Person: ActiveRecordBase
{
    private string contactName;
    [PrimaryKey(PrimaryKeyType.UuidString)]
       public string ContactName
    {
        get { return contactName; }
        set { contactName = value; }
    }
      .
      .
      .
}

Then PersonCond is the query condition object for Person Class inherts from BaseCond<T>. PersonCond must override the abstract method GetHQL() to return a HQL string which help to do ActiveRecordCriteriaQuery.

/// <summary>
/// Query condition
/// </summary>
public class PersonCond : BaseCond 
{

    public PersonCond(Type targetType)
            : base(targetType)
        {
        }
         .
         .
         .
    /// <summary>
    /// return the query HQL
    /// </summary>
    /// <returns></returns>
    public override string getHQL()
    {

        StringBuilder s = new StringBuilder();
        s.Append("from Person as p  where 1=1 ");

        if (id!=null&&id!="")
        {
            s.Append("and  p.Id like '" + id + "%' ");
        }

     .
     .
     .
        return s.ToString();
      
    }
}

Then you need just to add GridViewExpand to your toolbox then use it as usual in aspx page. in *.cs file,when you want to full the data to it,you write

   protected void Button1_Click(object sender, EventArgs e)
    {
        //Below to fill the query condition
        PersonCond p = new PersonCond(typeof(Person));
        p.Address = TextBoxAddress.Text;
        p.City = TextBoxCity.Text;
        p.ContactName = TextBoxName.Text;
        p.PostalCode = TextBoxPostalCode.Text;
        //Push query condition to the GridViewExpand
        GridView1.CustomPagerSettings.QueryBaseCond = p;
        GridView1.ShowData();
    }

and it will make paging and sorting function automaticaly.

Architect

  1. BaseCastle project
    It break up the query to two steps if BaseCond.IsCount is set true. First,query the total count according with the query. Second, query data according with query(query conditon,page index,page size) .
  2. Main Architect of GridViewExpand

    GridViewExpand integrated sorting and paging by hold up OnCommand event.

    Abstract class:ExtendFunction.cs. Paging and Sorting function implement the abstract class with its function on Excute() Event class:event.cs. Both functions add their own event in it to link to the delegate when the GridViewExpand initialize. These class is partitial of GridViewExpand. Property Class. Properties for each function.These class is partitial of GridViewExpand. GridViewExpand.cs.Override some event and methord to add your own to it.

  3. 3.Other

(1) Embed-resource write

[assembly: System.Web.UI.WebResource("GridViewExpand.Resources.Asc.gif", "image/gif")]

tag to add your BMP/GIF to the class as embed-resource.

Write

this._sgv.Page.ClientScript.GetWebResourceUrl
                                (
                                    this.GetType(),
                                    "GridViewExpand.Resources.item_amount_10.gif"
                                );

to use it.

(2)Use below in front of Property

        /// <summary>
        /// PagerCustom
        /// </summary>
        [
        Description("PagerCustom"),
        Category("Extension"),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
        PersistenceMode(PersistenceMode.InnerProperty)
        ]

So it will be show at visual studio the property window.

Points of Interest

See more for help: Expand architect for GridView:http://www.xieker.com/txt/37104.htm

Creating Custom ASP.NET Controls Roadmap: http://msdn.microsoft.com/en-ca/library/bb386519.aspx

History

Initial release.

FAQ

What is PersonCond and how do we use it?

Answer: Then PersonCond is the query condition object for Person Class inherts from BaseCond. PersonCond must override the abstract method GetHQL() to return a HQL string which help to do ActiveRecordCriteriaQuery.

BaseCond<T> is an encapsulation of ActiveRecordCriteriaQuery. We can set page index, pageSize and other parameters to query. The class is initial with Generic Class