Click here to Skip to main content
Licence 
First Posted 20 May 2005
Views 31,767
Bookmarked 28 times

A convenient DataGrid Pager Control

By | 20 May 2005 | Article
It's very easy to use and the most conventient DataGrid pager control.
 
Part of The SQL Zone sponsored by
See Also

Introduction

Last time, I submitted a web control to configure the data connection in web.config for a web site, it was easy and convenient. This time let's us go for a most convenient DataGrid Pager control. The steps involved are as follows:

  1. Drag and drop a Pager and a PagerExt web control into a web page.
  2. Set the property ControlID to DataGrid's ID or set it in the code as shown below:

    override protected void OnInit(EventArgs e)
    {
        InitializeComponent();
        base.OnInit(e);
    
        this.Pager1.ControlID = this.DataGrid1.ID;
        this.PagerExt1.ControlID = this.DataGrid1.ID;
    }
  3. Write a data bind function:
    private void BindGrid()
    {
        try
        {
            this.DataGrid1.DataSource = 
                        obj.GetDataSet(this.TextBox1.Text);
            this.DataGrid1.DataBind();
            
            // this is important for the show current 
            // page number correctly.
            this.PagerExt1.Refresh(); 
        }
        catch
        {
            this.Page.RegisterStartupScript
            ("","<script>alert('Please check the sql " + 
                           "cmd or wbe.config');</script>");
        }
    }
  4. Events coding:
    private void Pager1_PagerClick(object sender, 
                                     System.EventArgs e)
    {
        this.BindGrid();
    }
    private void PagerExt1_PageGoClick(object sender, 
                                     System.EventArgs e)
    {
        this.BindGrid();
    }

Testing your DataGrid pager control

How to do

private void Pager_Click(object sender, EventArgs e)
{
    if (this.Page.FindControl(this.ControlID) == null)
        return;

    String arg = ((LinkButton)sender).CommandArgument;

    try
    {
      switch(arg)
      {
        case "First":
           ((System.Web.UI.WebControls.DataGrid)this.
                Page.FindControl(this.ControlID)).CurrentPageIndex = 0;
            this.OnPagerCmd(e);
            break;
        case "Prev":
            if (((System.Web.UI.WebControls.DataGrid)this.
                      Page.FindControl(this.ControlID)).CurrentPageIndex > 0)
               ((System.Web.UI.WebControls.DataGrid)this.Page.
                               FindControl(this.ControlID)).CurrentPageIndex --;
            this.OnPagerCmd(e);
            break;
        case "Next":
            if (((System.Web.UI.WebControls.DataGrid)this.
                         Page.FindControl(this.ControlID)).CurrentPageIndex < 
                (((System.Web.UI.WebControls.DataGrid)this.
                               Page.FindControl(this.ControlID)).PageCount - 1))
                ((System.Web.UI.WebControls.DataGrid)this.
                          Page.FindControl(this.ControlID)).CurrentPageIndex ++;
            this.OnPagerCmd(e);
            break;
        case "Last":
            ((System.Web.UI.WebControls.DataGrid)this.
               Page.FindControl(this.ControlID)).CurrentPageIndex = 
                              ((System.Web.UI.WebControls.DataGrid)this.
                                Page.FindControl(this.ControlID)).PageCount - 1;
            this.OnPagerCmd(e);
            break;
      }
    }
    catch
    {
        return;
    }
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

zhengdong jin

Web Developer

China China

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionPager and PagerExt? Pinsusseanderson4:25 25 May '05  
AnswerRe: Pager and PagerExt? Pinmemberzhengdong jin18:02 5 Jun '05  

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.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 21 May 2005
Article Copyright 2005 by zhengdong jin
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid