Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Gridview- Issue when navigating to page #X where PageButtonCount < 'X' 2

Status
Not open for further replies.

jaikanthn

Programmer
Sep 1, 2006
8
US
Hi,

I have a gridview (using VS 2005) whose PageMode is "Numeric", PageButtonsCount is set to 10 and the PageSize (NumberOfRecordsPerPage) is 2. I'm planning to navigate to page # 25 programatically by setting grd.PageIndex = 25;. Even though the data populated in the grid correctly corresponds to the data that is supposed to be in the page # 25, the page numbers displayed in the Pager are 1 to 10... with page #6 selected which is wierd. How can I have the pager to display 21 to 30 and have page #25 selected? Thanks in advance.

Jai
 
At what point in the code are you setting the page index and why would you want to do it programatically?
 
hi jbenson,

Thanks for the reply.

The grid lists dates as one of the columns and I have to set the page which has today's date as the start-up page (current page) when the grid is rendered. This page need not necessarily be the 1st page. This could be the 8th page or 25th page, for instance.

If the page number happens to be say, 4, it works fine. 4 is set as teh current page in the pager. But if it is say, 25, it has no way of highlighting it since the page numbers shown in the pager are only 1..10 (for some reason #6 is shown as current page!). I'm not able to get 21..30 displayed & 25 highlighted.

I set the PageIndex in the Page_Load event.
Any help is appreciated.

Thanks
Jai
 
Setting the pageindex has not worked for me in my test page. Please post your page_load event code.
 
Hi,

grd.DataSource is set to an ObjectDataSource. The data displayed itself is fine and corresponds to the data that is supposed to be displayed on page #25. It's just the page numbers on the pager, which is causing the issue.

Here is the code:-

protected void Page_Load(object sender, EventArgs e)
{
if (! IsPostBack)
{
SetTotalRecords();//to display in a label
ComputeCurrentPage();();//to display in a label
ComputeTotalNumberOfPages();();//to display in a label

CurrentPageIndex = GetCurrentDatesPageIndex();

grd.PageIndex = CurrentPageIndex; //I get 25.
grd.DataBind();
}
grd.PageSize = 4; //constant
/*
Set at design-time
grd -> PagerSettings -> PageButtonCount = 10.
*/
}

Thanks for your help.
Jai
 
I finally got my test page to work, and it work exactly as you want yours to work. I think your problem may be that you are using one of the datasource controls to bind to the grid. Although the are easy to use to make a quick page, they normally cause issues when doing anything even a little more complex than what they are designed for. I sugges you bind your grid to a datatable instead and see if it works for you.
 
hi jbenson,

Thanks for the reply. This is an useful tip.
I'll try to explain the reason behind going for an ObjectDatasource instead of a of a regualr Dataset(Datatable). This is not a regular pagination. Even though I show pages 1..10, I get the data corresponding to one particular page at any point of time. I calculate the number of pages required for the total number of records. and display the page number accordingly on the pager. But the data displayed (in fact the data in hand) pertains only to a single page. when the user clicks on a different page number, I capture the PageIndexChanging event, get the data corresponding to that page and rebind the grid. So at any point of time i have data worth one page only. If I try to do this the conventional way, i may need to set grd.PageCount. But it is read-only ! Hence the idea of using an ObjectDatasource for binding the gird. the class (TypeName) behind the ObjectDatasource is my own class. In this case, I can set the resultset, and the totalrowsCount of the ObjectDatasource and it seems to take care of everthing else. (except what I'm looking for). May be I have to tweak the code for the ObjectDatasource's class.

Here is the code for the ObjectDatasource's Class:-

public class ItemListingDataSource
{
List<ItemListingItem> ItemList;
int totalCount = 0;

public ItemListingDataSource()
{

}

public ItemListingDataSource(List<ItemListingItem> ItemList, int totalCount)
{
this.ItemList = ItemList;
this.totalCount = totalCount;
}

public List<ItemListingItem> GetItems()
{
return ItemList;
}

public List<ItemListingItem> GetItems(int startRowIndex, int pageSize)
{
return ItemList;
}

public int TotalCount()
{
return totalCount;
}
}


and here is how I load the ObjectDatasource:-

protected void ods_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
{
//calling a "parametered" constructor of datasource and passing it to the ObjectInstance property
e.ObjectInstance = new ItemListingDataSource(ListingItems, TotalPages * ItemsPerPage);
}


and here is the HTML for the ObjectDatasource:-

<asp:ObjectDataSource ID="ods" runat="server" EnablePaging="true" MaximumRowsParameterName="pageSize"
OnObjectCreating="ods_ObjectCreating" SelectCountMethod="TotalCount"
SelectMethod="GetItems" TypeName=".....ItemListingDataSource">
</asp:ObjectDataSource>


Please see if you can help me in any way.

I'm glad you found where the problem is. Appreciate your help so far.

Jai
 
Hi jbenson,

I got it to work, finally.
All I had to do was, put the line of code "grd.PageSize = 4;" before the statement grd.DataBind();
DataBind triggers the databind of ObjectDataSource which calculates the Page numbers based on TotalNumberOfRecords and TotalNumberOfRecords/Page (PageSize). In my code "grd.PageSize = 4;" was after Databind() which was the cause for that confusing behavior. I put it before Databind and it works like a charm now. I need to have one more instance of "grd.PageSize = 4;" statement outside of if (!Postback) too since this has to be set everytime, the page is posted back.

Here is the final code:-

protected void Page_Load(object sender, EventArgs e)
{
int PageSize = ComputeTotalNumberOfPages();

if (! IsPostBack)
{
SetTotalRecords();//to display in a label
ComputeCurrentPage();();//to display in a label

grd.PageSize = PageSize;

CurrentPageIndex = GetCurrentDatesPageIndex();

grd.PageIndex = CurrentPageIndex; //I get 25.
grd.DataBind();
}

grd.PageSize = PageSize;
}

I wouldn't have gotten this to work without your help. Thanks a lot.

Jai
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top