RSS

Custom Paging of GridView,DataListView and Repeater Control of Asp.net With C#

25 Jun

Custom Paging Img

Custom Paging of GridView or DataListview or Repeater

Code-Behind Code

//Page Load Event

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

BindMngRegistration();

}

}

//Load or Bind the Data to Dataset with gridview or Datalist

public void BindMngRegistration()

{

DataSet ds = BLLRegisrationOnline.ViewManageRegistration();

_PageDataSource.DataSource = ds.Tables[0].DefaultView;

_PageDataSource.AllowPaging = true;

_PageDataSource.PageSize = Convert.ToInt32(GVRegView.PageSize);

_PageDataSource.CurrentPageIndex = CurrentPage;

ViewState[“TotalPages”] = _PageDataSource.PageCount;

this.lblPageInfo.Text = “Page ” + (CurrentPage + 1) + ” of ” + _PageDataSource.PageCount;

this.lbtnPrevious.Enabled = !_PageDataSource.IsFirstPage;

this.lbtnNext.Enabled = !_PageDataSource.IsLastPage;

this.lbtnFirst.Enabled = !_PageDataSource.IsFirstPage;

this.lbtnLast.Enabled = !_PageDataSource.IsLastPage;

//Bind With Control

this.GVRegView.DataSource = _PageDataSource;

this.GVRegView.DataBind();

this.doPaging();

}

//Write This Code For the Number of Record to display at Lable

protected void dlPaging_ItemDataBound(object sender, DataListItemEventArgs e)

{

if (e.Item.ItemType != ListItemType.Separator)

{

LinkButton lnkbtnPage = (LinkButton)e.Item.FindControl(“lnkbtnPaging”);

if (lnkbtnPage.CommandArgument.ToString() == CurrentPage.ToString())

{

lnkbtnPage.Enabled = false;

lnkbtnPage.Style.Add(“fone-size”, “14px”);

lnkbtnPage.Font.Bold = true;

}

}

}

//display the current page of the number of page like (5 of 15)

protected void dlPaging_ItemCommand(object source, DataListCommandEventArgs e)

{

if (e.CommandName.Equals(“Paging”))

{

CurrentPage = Convert.ToInt16(e.CommandArgument.ToString());

this.BindMngRegistration();

}

}

// Page Datasource Object for pagging

#region PagedDataSource

PagedDataSource _PageDataSource = new PagedDataSource();

#endregion

//Properties of Paging for next,first ,last,previous

#region Private Properties

private int CurrentPage

{

get

{

object objPage = ViewState[“_CurrentPage”];

int _CurrentPage = 0;

if (objPage == null)

{

_CurrentPage = 0;

}

else

{

_CurrentPage = (int)objPage;

}

return _CurrentPage;

}

set { ViewState[“_CurrentPage”] = value; }

}

private int fistIndex

{

get

{

int _FirstIndex = 0;

if (ViewState[“_FirstIndex”] == null)

{

_FirstIndex = 0;

}

else

{

_FirstIndex = Convert.ToInt32(ViewState[“_FirstIndex”]);

}

return _FirstIndex;

}

set { ViewState[“_FirstIndex”] = value; }

}

private int lastIndex

{

get

{

int _LastIndex = 0;

if (ViewState[“_LastIndex”] == null)

{

_LastIndex = 0;

}

else

{

_LastIndex = Convert.ToInt32(ViewState[“_LastIndex”]);

}

return _LastIndex;

}

set { ViewState[“_LastIndex”] = value; }

}

#endregion

//Events of Paging

#region “Custom Paging”

protected void lbtnFirst_Click(object sender, EventArgs e)

{

CurrentPage = 0;

this.BindMngRegistration();

}

protected void lbtnPrevious_Click(object sender, EventArgs e)

{

CurrentPage -= 1;

this.BindMngRegistration();

}

protected void lbtnNext_Click(object sender, EventArgs e)

{

CurrentPage += 1;

this.BindMngRegistration();

}

protected void lbtnLast_Click(object sender, EventArgs e)

{

CurrentPage = (Convert.ToInt32(ViewState[“TotalPages”]) – 1);

this.BindMngRegistration();

}

#endregion

//total Pages

private void doPaging()

{

DataTable dt = new DataTable();

dt.Columns.Add(“PageIndex”);

dt.Columns.Add(“PageText”);

fistIndex = CurrentPage – 5;

if (CurrentPage > 5)

{

lastIndex = CurrentPage + 5;

}

else

{

lastIndex = 10;

}

if (lastIndex > Convert.ToInt32(ViewState[“TotalPages”]))

{

lastIndex = Convert.ToInt32(ViewState[“TotalPages”]);

fistIndex = lastIndex – 10;

}

if (fistIndex < 0)

{

fistIndex = 0;

}

for (int i = fistIndex; i < lastIndex; i++)

{

DataRow dr = dt.NewRow();

dr[0] = i;

dr[1] = i + 1;

dt.Rows.Add(dr);

}

this.dlPaging.DataSource = dt;

this.dlPaging.DataBind();

}

Client- Side Code

<div style=”float:right;”>

<table cellpadding=”0″ border=”0″>

<tr><td>

<asp:Label ID=”lblPageInfo” runat=”server”></asp:Label>

</td><td>&nbsp;&nbsp;</td>

<td align=”right”>

<asp:LinkButton ID=”lbtnFirst” runat=”server”

CausesValidation=”false” OnClick=”lbtnFirst_Click” CssClass=”paging”>First</asp:LinkButton>

&nbsp;</td>

<td align=”right”>

<asp:LinkButton ID=”lbtnPrevious” CssClass=”paging” runat=”server”

CausesValidation=”false” OnClick=”lbtnPrevious_Click”>Previous</asp:LinkButton>&nbsp;&nbsp;</td>

<td align=”center” valign=”middle”>

<asp:DataList ID=”dlPaging” runat=”server” RepeatDirection=”Horizontal” OnItemCommand=”dlPaging_ItemCommand”

OnItemDataBound=”dlPaging_ItemDataBound”>

<ItemTemplate>

<asp:LinkButton CssClass=”paging” ID=”lnkbtnPaging” runat=”server” CommandArgument='<%# Eval(“PageIndex”) %>’

CommandName=”Paging” Text='<%# Eval(“PageText”) %>’></asp:LinkButton>&nbsp;

</ItemTemplate>

<SeparatorTemplate>&nbsp;|&nbsp;</SeparatorTemplate>

</asp:DataList>

</td>

<td align=”left”>

&nbsp;&nbsp;<asp:LinkButton CssClass=”paging” ID=”lbtnNext” runat=”server” CausesValidation=”false”

OnClick=”lbtnNext_Click”>Next</asp:LinkButton></td>

<td align=”left”>

&nbsp;

<asp:LinkButton ID=”lbtnLast” runat=”server” CausesValidation=”false” OnClick=”lbtnLast_Click” CssClass=”paging”>Last</asp:LinkButton></td>

<td style=”padding-left:5px;”>&nbsp;</td>

</tr>

<tr>

<td colspan=”7″ align=”center” style=”display:none;” valign=”middle”>

&nbsp;</td>

</tr>

</table></div>

Advertisements
 
2 Comments

Posted by on June 25, 2010 in ASP Dot Net C#, Uncategorized

 

2 responses to “Custom Paging of GridView,DataListView and Repeater Control of Asp.net With C#

  1. bhavik

    June 12, 2012 at 4:03 pm

    Thax was usefull

     

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: