Custom paging in ASP Gridview using stored procedures [duplicate]
-
13-07-2021 - |
Pregunta
I'm trying to create a GridView with custom paging. I have very large data so In order to Increase ten Speed I'm trying to do Custom Paging.
I wrote a SQL Server stored procedure that takes input parameters SelectedPageNumber
, SelectedPageSize
and gives the DataTable
filled with the resulting rows belonging to the particular page. The stored procedure also has a MaxPageNumber
as output parameter.
I'm able to bind the data of the page to the GridView but I'm not able to figure out how to show the page numbers up to the MaxPageNumber
value taken as a stored procedure.
I'm using .Net Framework 4.0
It might be a duplicate but I could not find the solution.
Can you help me out ???
Solución
You could create a Pager using Repeater
control, Something like this
// These are your outputs from that SP
int MaxPageNumber = 10,
CurrentPageNumber = 4;
void BindPager()
{
DataTable PagerData = new DataTable();
PagerData.Columns.Add("pageNo");
for (int i = 1; i < MaxPageNumber; i++)
PagerData.Rows.Add(i);
pager.DataSource = PagerData;
pager.DataBind();
}
<asp:Repeater runat="server" ID="pager" onitemcommand="pager_ItemCommand"
onitemdatabound="pager_ItemDataBound">
<ItemTemplate>
<asp:Button runat="server" ID="pageNo"
Text='<%# Eval("pageNo") %>'
CommandArgument='<%# Eval("pageNo") %>'
CommandName="DoPaging" />
</ItemTemplate>
</asp:Repeater>
protected void pager_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
// Code to highlight current page
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
Button pageNo = e.Item.FindControl("pageNo") as Button;
if (pageNo == null) return;
if (pageNo.Text == CurrentPageNumber.ToString())
pageNo.BackColor = Color.Blue;
else
pageNo.BackColor = Color.Gray;
}
}
protected void pager_ItemCommand(object source, RepeaterCommandEventArgs e)
{
// perform your paging here according to page number
}
Otros consejos
To show the page numbers you can create a List of integers from MinPageNumber to MaxPageNumber, bind the list to a Repeater-Control as the DataSource and let the Repeater display each page number. Depending on the current page number you might have to recreate that list.
If you are using custom paging then you have to create a custom page control to display the page numbers. You can place the custom pager below the gridview and on click event on the page number rebind the grid.