Pagination in asp.net


In this article I am explaining How to implement paging in ASP.Net GridView control in a simple and easy way. This properties of the GridView come into play when Paging needs to be added. 1. PageSize - Sets the Size of the Page for GridView control 2. PageIndexChanging – The event that will be triggered when the page link is clicked by the user.

Here is the code for add pagination,in dynamically generated gridview.
Here you can count your various paging variables like,

totalpagelabel,
pageindexlabel,
pagerowcount
from code.

just simply follow this code.
Put this code into .aspx page.


[PagerSettings FirstPageText="First" LastPageText="Last" Mode="NextPreviousFirstLast"
NextPageText="Next" PreviousPageText="Prev" PageButtonCount="3" /> ]
[asp:DropDownList ID="drpPageSize" runat="server" AutoPostBack="true" OnSelectedIndexChanged="drpPageSize_SelectedIndexChanged">

[asp:ListItem Selected="True"]5[/asp:ListItem]
[tasp:ListItem]10[/asp:ListItem]....
[/asp:DropDownList]

[td]
Showing
[asp:Label ID="PageIdxLabel" runat="server" /]

[/td]



code in aspx.cs file,

protected void drpPageSize_SelectedIndexChanged(object sender, EventArgs e)
{

grid.PageSize = int.Parse(drpPageSize.SelectedItem.Text);
grdBind();

}

protected void grid_PageIndexChanging(object sender, GridViewPageEventArgs e)
{

grid.PageIndex = e.NewPageIndex;

grdBind();
}
protected void grid_RowDataBound(object sender, GridViewRowEventArgs e)
{

EventManagerDataContext db = new EventManagerDataContext();

var q = from a in db.EMR_NEWs

select new
{
News_ID = a.News_ID,
Title = a.Title,
Text = a.Text,
Creation_Date = a.Creation_Date,
Publication_Date = a.Publication_Date,
Expiration_Date = a.Expiration_Date
};
int pagetotal = q.Count();

int pagerowcount = (grid.PageIndex * grid.PageSize) + grid.Rows.Count;
PageIdxLabel.Text = ((grid.PageIndex * grid.PageSize) + 1).ToString() + "to " + Convert.ToString(pagerowcount) + "of" + (pagetotal).ToString();

}


Pagination in asp.net


Attachments

Comments

Author: naveensanagasetti18 Oct 2013 Member Level: Gold   Points : 2

Hi Ketan,

I hope you are post is not completed just try to edit your post and resubmit again. When you wrote code parts you must place your code inbetween CODE blocks.
Instead of "lessthan symbol" you must use " & lt" and instead of "greaterthan symbol" you must use " & gt" and styles you try to give effectively then only user can understand the post clearly. Here your total content is in Bold format, but that's not correct you can highlight necessary points only in bold.

Hope you edit the content and resubmit the Resource again..

Author: ketan Italiya18 Oct 2013 Member Level: Gold   Points : 0

hey,Naveen

Thanks,for your suggestion,I make changes.in my article.I hope this is now able to understand.

Thanks,
ketan

Author: naveensanagasetti18 Oct 2013 Member Level: Gold   Points : 0


Compare to previous this is OK but what is "[" this in Source code for each and everyline it's showing that. Can you please decorate it properly for getting more points..



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: