Datagrid paging wierd behaviour

B

Brian Lowe

I have a DataGrid which uses a dataset as its source. I have it set to allow
paging and page length set at 9 rows, and page style set to numeric. My
dataset has 25 rows, so just as expected I see rows 1 to 9 and paging links
1 to 3 with "1" as a static label and "2" and "3" as linkbuttons.

OK - here's where the fun starts.

I click "2" and get a postback, but the grid still shows rows 1 to 9 and
the paging controls still show 1 as static and 2 and 3 as active links.

I click "2" a second time. The grid still shows rows 1 to 9 but the paging
controls change so that 1 is active, 2 is static and 3 is active.

But the wierdness doesn't stop there.

I click "3". The grid still shows rows 1 to 9 but the paging controls change
so that 1 is still active, 2 is still static and 3 is still active.

I click "3" again. The grid now shows rows 1 to 7 and the paging controls
change so that 1 is still active, 2 is now active and 3 is now static.

OK - so I figure that the grid has somehow clocked the fact that it should
only be showing 7 rows because it's on page 3, but how come it's showing
rows belonging to page 1?

Now, "1" is an active link, so I click it, and then the grid content changes
to show rows 1 to 8 and some columns of row #9.

I've looked at the code and it seems to match exactly the examples (aside
from the structure of the test dataset) on MSDN and various other coding
support sites.

I've set Trace="True" and watched the output and can see nothing different
in my page to what's in a (really over simplistic) example.

I've stepped through the code line by line in the Visual Studio IDE. Nothing
raises any errors. Everything seems to happen just when it should.

I've searched Google, MSDN et al for anything that might discuss symptoms
similar to these, and I'ev drawn a blank.

I just know I'm going to look stupid when the cause is revealed to be
something obvious and trivial, but what the heck. Nothing's idiot proof to a
really talented idiot!

Brian
 
S

Scott M.

The problem is most likely where and when you are calling DataBind in your
code.

Your Page_Load event should only do a databind in a NON postback situation
(although the data source should be set in either case). The DataBind call
should also be placed in your PageIndexChanged event handler after setting
the new page index.

Let's see your code.
 
B

Brian Lowe

I was following an example in C# that just had
void dataGrid1_PageIndexChanged(object source,
System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
dataGrid1.CurrentPageIndex = e.NewPageIndex;
}
in its code, and which seemed to work just fine.

I hadn't noticed that elsewhere in the code file were
void Page_Load()
{
dataGrid1.DataSource = DataView1;
}
void Page_PreRender()
{
DataBind();
}

I've changed my code so that the DataSource is set and the DataBind gets
triggered, and now all is well.

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top