We will connect to Northwind Database
First create an aspx page and add SqlDataSourse control that connect to Customers table in the database and add Gridview control and set DataSourse to SqlDataSourse and from its property set allow paging to true and AutoGenerateColumns to False
and in Pager Template of the Gridview add four ImageButton (First,Next,Prev,Last) and Dropdownlist (ID=DDLPage , AutoPostPack= true) and two Lable(ID= lblCurrent and lblPages)
In Code Behind we will write the following code:
using System;
using System.Data;
using System.Configuration;
using System.Collections;using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class Default7 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void GridView1_DataBound(object sender, EventArgs e)
{
GridViewRow row = GridView1.BottomPagerRow;
if (row == null)
{
return;
}
DropDownList DDLPage = (DropDownList)row.Cells[0].FindControl("DDLPage");
Label lblPages = (Label)row.Cells[0].FindControl("lblPages");
Label lblCurrent = (Label)row.Cells[0].FindControl("lblCurrent");
lblPages.Text = GridView1.PageCount.ToString();
int currentPage = GridView1.PageIndex + 1;
lblCurrent.Text = currentPage.ToString();
if (DDLPage != null)
{
for (int i = 0; i < GridView1.PageCount; i++)
{
int pageNumber = i + 1;
ListItem item = new ListItem(pageNumber.ToString());
if (i == GridView1.PageIndex)
{
item.Selected = true;
}
DDLPage.Items.Add(item);
}
}
//-- For First and Previous ImageButton
if (GridView1.PageIndex == 0)
{
//((ImageButton)GridView1.BottomPagerRow.FindControl("btnFirst")).Enabled = false;
//((ImageButton)GridView1.BottomPagerRow.FindControl("btnFirst")).Visible = false;
//((ImageButton)GridView1.BottomPagerRow.FindControl("btnPrev")).Enabled = false;
//((ImageButton)GridView1.BottomPagerRow.FindControl("btnPrev")).Visible = false;
//--- OR ---\\
ImageButton btnFirst = (ImageButton)row.Cells[0].FindControl("btnFirst");
ImageButton btnPrev = (ImageButton)row.Cells[0].FindControl("btnPrev");
btnFirst.Visible = false;
btnPrev.Visible = false;
}
//-- For Last and Next ImageButton
if (GridView1.PageIndex+1 == GridView1.PageCount)
{
//((ImageButton)GridView1.BottomPagerRow.FindControl("btnLast")).Enabled = false;
//((ImageButton)GridView1.BottomPagerRow.FindControl("btnLast")).Visible = false;
//((ImageButton)GridView1.BottomPagerRow.FindControl("btnNext")).Enabled = false;
//((ImageButton)GridView1.BottomPagerRow.FindControl("btnNext")).Visible = false;
//--- OR ---\\
ImageButton btnLast = (ImageButton)row.Cells[0].FindControl("btnLast");
ImageButton btnNext = (ImageButton)row.Cells[0].FindControl("btnNext");
btnLast.Visible = false;
btnNext.Visible = false;
}
}
protected void DDLPage_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row=GridView1.BottomPagerRow;
DropDownList DDLPage = (DropDownList)row.Cells[0].FindControl("DDLPage");
GridView1.PageIndex = DDLPage.SelectedIndex;
GridView1.DataBind();
}
}
Sunday, January 20, 2008
Subscribe to:
Post Comments (Atom)


No comments:
Post a Comment