Click to See Complete Forum and Search --> : DataGrid paging "previous" event failure


tomhartland
04-22-2004, 05:29 AM
Greetings :-)

I'm having trouble getting the paging to work on one of my DataGrid web controls. The following C# code lives behind a simple ASPX page containing a single PlaceHolder control...private void Page_Load(object sender, System.EventArgs e)
{
// Create a table, column and 25 rows
DataTable obDataTable = new DataTable( "testtable" );
obDataTable.Columns.Add( new DataColumn( "testcol1" ) );
for ( int iLoop=0; iLoop<25; iLoop++ )
obDataTable.Rows.Add( new object[] { "a" + iLoop.ToString() } );

// Create datagrid and set the paging to 10
DataGrid obDataGrid = new DataGrid();
obDataGrid.AllowPaging = true;
obDataGrid.PageSize = 10;
obDataGrid.PageIndexChanged += new
DataGridPageChangedEventHandler( PageChanged );

// Set the Data source, bind and add to the placeholder
obDataGrid.DataSource = obDataTable.DefaultView;
obDataGrid.DataBind();
PlaceHolder1.Controls.Add( obDataGrid );
}

protected void PageChanged( object objSender,
DataGridPageChangedEventArgs eArgs )
{
// Cast the datagrid and set the page index
DataGrid obDataGrid = (DataGrid)objSender;
obDataGrid.CurrentPageIndex = eArgs.NewPageIndex;
obDataGrid.DataBind();
}Moving forward through the pages works correctly, and I can breakpoint in the PageChanged handler. However the Previous button does not fire the event handler (but does fire Page_Load) and the data on screen becomes corrupt.
Using the above code, the 3 pages show correctly, but clicking Previous on the 3rd page (which has the last 5 entries) produces a page showing the same 5 entries, plus the last 5 entries from the 1st page.
This can be also be reproduced with the PagerStyle.Mode set to Numbered.

Does anybody have any ideas? Why is the event handler not being called for the Previous button? Any help would be greatly appreciated.

Cheers,
Tom

PeOfEo
04-22-2004, 07:00 PM
I do not use c# enough and have not experienced this problem before in vb.net so I will not be able help you figure out this specific problem, but just in general I would say write your own pageing code, custum pageing, instead of relying on the built in asp.net pageing because if you do your own code you will have more control over it and it will be valid, you do not have to worrie about how asp.net outputs js without a type attribute, and so on and so fourth. I am not doing anypaging right now but a blog I am working on will only show results from the past most and put the rest into the archive, which I actuall probably will end up pageing when this is all done, but when I used the built in pageing back in the day I did not like it because it gave me very limited control over the look and feel of it. Just out of curiosity, why are you using the place holder control, doesn't that output a table? Why wouldn't you use a <div runat="server">? This is just out of my curiosity, because I find myself running a bunch of html elements at the server to get more source control.

tomhartland
04-23-2004, 02:39 AM
Thanks for your reply :-)

I have to agree with you about the DataGrids seemingly appauling control over the appearance of things like the paging.
Also, unless I'm being stupid - which is highly possible - there doesn't seem to be a way to set a CssClass property on any of the controls within the DataGrid (in particular the <A> tag that is created for the title in sortable columns).

But your suggestion of writing my own custom paging looks like it is going to be the answer - unfortunate, as we're looking at a pretty hefty deadline.

The PlaceHolder control does not produce a table - it is used as more of a marker for the positioning of dynamically created ASP.NET HTML code.
From MSDN "Use the PlaceHolder control as a container to store dynamically added server controls to the Web page. The PlaceHolder control does not produce any visible output and is only used as a container for other controls on the Web page."
Do a quick test - make a simple "<html><body><asp:PlaceHolder ID="phTest"/></body></html>" page, and add a single Label object to the controls collection. The result is just a <span> tag (and associated attributes), nothing else.

PeOfEo
04-23-2004, 04:54 PM
well does it have to infact be pageing? I wqould say count the rows from the gird like before, then divide it by 10. Then show the first 10 results, then show the next 10 when the user clicks on the next button (multiply the page# (which will incriment when the user clicks it) by 10 then add another 10 and you have your beginning and end numbers, page# * 10 = your start, page# * 10 + 10 = your end), so on and so fourth until he is on the last page where you would make the button disabled or no longer visible. A button would be good because it just resends the data, but you could also use a query string and redirect that user to the same page he was on but with the query string for the page number and it will run that page load select event and get the number of entries again too, which would look better but be slightly less efficient (but probably not noticable). You could also use the linkbutton element, to avoid this, but that element will create a java script even though it will resend the page. I try to avoid asp.net outputting js at all costs.

mkiew
08-16-2005, 05:12 PM
I met the same problem. Just wonder if it has been solved.

Thanks!