Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Search-Engine Style DataGridPager works fine only in Page_Load

I tried to use the cutom pager as described in the following article

http://www.dev-archive.com/dotnet/Article/26823

The pager navigates thru the records correctly only if the DataGrid is binded on Page_Load.When i bind the datagrid on the Click event of a link button,the follwing problem occurs.When i click the navigation links (First,Next..) i need to click the link again to get the DataGrid rebinded with new data.Is it a problem of postback?if so please advice me how to handle postback for navigation links.

Thanks in advance
misserene
[574 byte] By [misserene] at [2007-11-11 7:26:39]
# 1 Re: Search-Engine Style DataGridPager works fine only in Page_Load
Here is a beter link to paging

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vbCode/html/vbtskCodePagingInDataGridWebControlVisualBasic.asp
The reason it only work once is because you have it wrapped in a postback I believe. THe following example worked for me then I was able alter the layout better once I had paging working.
sremiger at 2007-11-11 23:13:48 >
# 2 Re: Search-Engine Style DataGridPager works fine only in Page_Load
Hi,

Here is a sample code for ASP.NET DataGrid paging

The DataGrid control has built-in support for paging through the records of a data source.
For example, there may be a large number of records in the AccountsTable. If we divide the records
into multiple pages, it is convenient for the user to see the data.
We enable paging for a DataGrid by enabling the AllowPaging property and
creating a subroutine to change the current page.

Write the following code in the HTML window

<HTML>
<HEAD>
<title>DatagridPaging</title>
</HEAD>
<body>
<form id=Form1" runat=server>

<asp:DataGrid ID=dgrdAccounts runat=server
AllowPaging=True PageSize=5"
OnPageIndexChanged=drdaccounts_pageIndexChanged
cellpadding=3">
</asp:DataGrid>
</form>
</body>
</HTML>

----

Write the following code in the Code behind window

Imports System.Data.SqlClient

Dim myConnection As SqlConnection = New SqlConnection(Data Source=SYS1;Integrated Security=SSPI;Initial Catalog=FinAccounting)
Const strSQL As String = SELECT AccountCode,Accountname,AccountDescription FROM AccountsTable
Dim myDataAdapter As SqlDataAdapter = New SqlDataAdapter(strSQL, myConnection)
Dim dstaccounts As New DataSet()

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack() Then
BindDataGrid()
End If
End Sub

Sub BindDataGrid()
myConnection.Open()
myDataAdapter.Fill(dstaccounts, AccountsTable)
dgrdAccounts.DataSource = dstaccounts
dgrdAccounts.DataBind()
End Sub

Sub drdaccounts_pageIndexChanged(ByVal s As Object, ByVal e As DataGridPageChangedEventArgs)
dgrdAccounts.CurrentPageIndex = e.NewPageIndex
BindDataGrid()
End Sub

This example displays five records at a time from the AccountsTable. By clicking the
< and > links displayed at the bottom of the DataGrid,
we can navigate forward or backward. A new page is selected
with the drdaccounts_pageIndexChanged subroutine.
This subroutine assigns the value of the NewPageIndex to the DataGrid controls CurrentPageIndex.
The subroutine then rebinds the DataGrid to the data source, displaying the new page of records.

If you find this code useful, you like the book Titled: "Develop your own Web Accounting Application using ASP.Net"

Regards
bhar
Bharati at 2007-11-11 23:14:42 >