Click here to Skip to main content
Click here to Skip to main content
Go to top

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

, 27 Aug 2008
Rate this:
Please Sign up or sign in to vote.
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.

/// <span class="code-SummaryComment"><summary></span>
///Person 
/// <span class="code-SummaryComment"></summary></span>
[ActiveRecord("Customers")]
public class Person: ActiveRecordBase<person />
{
    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.

/// <span class="code-SummaryComment"><summary></span>
/// Query condition
/// <span class="code-SummaryComment"></summary></span>
public class PersonCond : BaseCond 
{

    public PersonCond(Type targetType)
            : base(targetType)
        {
        }
         .
         .
         .
    /// <span class="code-SummaryComment"><summary></span>
    /// return the query HQL
    /// <span class="code-SummaryComment"></summary></span>
    /// <span class="code-SummaryComment"><returns></returns></span>
    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

        /// <span class="code-SummaryComment"><summary></span>
        /// PagerCustom
        /// <span class="code-SummaryComment"></summary></span>
        [
        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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author


Comments and Discussions

 
Question[Message Deleted] PinmemberJoker_jfxia3-Sep-08 16:38 
AnswerRe: Error Pinmemberclarkapp3-Sep-08 22:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140926.1 | Last Updated 27 Aug 2008
Article Copyright 2008 by clarkapp
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid