Tags


The Aspx page Code:

I am defining the images as Image button in the GridView pagerTemplate

<%@ Page Language=”C#” AutoEventWireup=”true” CodeBehind=”Default.aspx.cs” Inherits=”GridViewServerSidePaging._Default”  EnableEventValidation=”false” %> 

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”&gt;
<html xmlns=”http://www.w3.org/1999/xhtml”&gt;
<head runat=”server”> <title></title></head>

<body>

<form id=”form1″  runat=”server”>
<div>

<asp:GridView ID=”gvUser” runat=”server” AllowPaging=”True” OnDataBound=”gvUser_DataBound”
PageSize=”2″ BorderStyle=”None”

 BackColor=”White” BorderColor=”#CC9966″ BorderWidth=”1px”   CellPadding=”4″ Height=”16px” Width=”16px”>
<RowStyle BackColor=”White” ForeColor=”#330099″ />
<FooterStyle BackColor=”#FFFFCC” ForeColor=”#330099″ />

<PagerTemplate>

<asp:ImageButton ID=”imgBtnFirst” runat=”server” ImageUrl=”~/Images/first.gif” CommandArgument=”First”

CommandName=”Page” OnCommand=”Paginate” />

<asp:ImageButton ID=”imgBtnPrevious” runat=”server” ImageUrl=”~/Images/previous.gif”
CommandArgument=”Prev”
CommandName=”Page” OnCommand=”Paginate” /> 

<asp:ImageButton ID=”imgBtnNext” runat=”server” ImageUrl=”~/Images/next.gif” CommandArgument=”Next”

CommandName=”Page” OnCommand=”Paginate” />

<asp:ImageButton ID=”imgBtnLast” runat=”server” ImageUrl=”~/Images/last.gif” CommandArgument=”Last”

CommandName=”Page” OnCommand=”Paginate” />

</PagerTemplate>

<PagerStyle BackColor=”#FFFFCC” ForeColor=”#330099″ HorizontalAlign=”Center” />

<SelectedRowStyle BackColor=”#FFCC66″ Font-Bold=”True” ForeColor=”#663399″ />

<HeaderStyle BackColor=”#990000″ Font-Bold=”True” ForeColor=”#FFFFCC” />

</asp:GridView>
</div>


</form>

</body>

</html>

Code Behind:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using GridViewServerSidePaging.DAO;

namespace GridViewServerSidePaging

{


public
partial
class
_Default : System.Web.UI.Page

{


protected
void Page_Load(object sender, EventArgs e)

{


// gvUser.Visible = true;

PopulateGridView(gvUser.PageIndex);

}


protected
void gvUser_DataBound(object sender, EventArgs e)

{


GridViewRow gvPager = gvUser.BottomPagerRow;


if (gvPager == null)

{


return;

}


else

gvPager.Visible = true;


DropDownList ddlPages = (DropDownList)gvPager.Cells[0].FindControl(“ddlPages”);


Label lblPageCount = (Label)gvPager.Cells[0].FindControl(“lblPageCount”);


if (ddlPages != null)

PopulatePager(ddlPages);


if (lblPageCount != null)

lblPageCount.Text = gvUser.PageCount.ToString();

}


private
void PopulateGridView(int pageIndex)

{

gvUser.DataSource = GridDAO.GetAllUser();

gvUser.DataBind();

}


protected
void PopulatePager(DropDownList ddlPages)

{


for (int i = 0; i < gvUser.PageCount; i++)

{


ListItem lstItem = new
ListItem((i + 1).ToString());


if (i == gvUser.PageIndex)

lstItem.Selected = true;

ddlPages.Items.Add(lstItem);

}

}


protected
void Paginate(object sender, CommandEventArgs e)

{


// get the current page selected


int intCurIndex = gvUser.PageIndex;


switch (e.CommandArgument.ToString().ToLower())

{


case
“first”:

gvUser.PageIndex = 0;


break;


case
“prev”:


if (gvUser.PageIndex > 0)

gvUser.PageIndex = intCurIndex – 1;


break;


case
“next”:

gvUser.PageIndex = intCurIndex + 1;


break;


case
“last”:

gvUser.PageIndex = gvUser.PageCount – 1;


break;

}

Response.Write(“” + gvUser.PageIndex);


// popultate the gridview control

PopulateGridView(gvUser.PageIndex);

}

}

}

GridViewBD.dbml

GridViewDAO.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

namespace GridViewServerSidePaging.DAO

{


public
class
GridDAO

{


public
static
GridDBDataContext GetDBContex()

{


GridDBDataContext gridDBContext = new
GridDBDataContext();


return gridDBContext;

}


public
static
List<User> GetAllUser()

{


List<User> userList = (from
User
in GetDBContex().Users select User).ToList<User>();


return userList;

}


public
static
List<User> GetPagedUser(int pageIndex)

{


IEnumerable<User> enumUser;


int skip = pageIndex * 2;

enumUser = GetAllUser().Skip(skip).Take(2);


List<User> userPageList = enumUser.ToList<User>();


return userPageList;

}

}

}

Solution Structure:


Output Image:

Advertisements