PageIndexChange in datagrid

P

pietaster

I have a datagrid that I need to allow paging. The datagrid only gets
loaded when !IsPostBack. If this is the case paging does not work. If
I load the datagrid IsPostBack then the paging works. Then the problem
would be that when paged the data will get loaded twice. Once from
Page_Load and once from datagrid_PageIndexChange. What should I
do?????

My paging event gets loaded in OnInt. AllowPaging and PageSize and
other things needed for paging are also loaded in OnInt.

Thanks.
pietaster
 
R

Renato Aloi

The default behavior of the datagrid's pagin' is:

Not IsPostBack (first time load)
- On Page load: DataBind the datagrid

IsPostBack (every other load)
- On Page load: Skips binding
- On Page Index change: Set the new page index and databind the datagrid

The best way to do this is combining not is postback with a routine to
databind the datagrid, like this:

' 1. {
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

If Not IsPostBack Then
Me.loadDataGrid()
End If

End Sub
' }

' 2. {
Private Sub DataGrid1_PageIndexChanged(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles
DataGrid1.PageIndexChanged

Me.DataGrid1.CurrentPageIndex = e.NewPageIndex
Me.loadDataGrid()

End Sub
' }

' 3. {
Private Sub loadDataGrid()

Me.DataGrid1.DataSource = whateverDataSource
Me.DataGrid1.DataBind()

End Sub
' }

[]s
Graccs
 

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,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top