The data grid automatically generates the previous page and next page icons. When any one of these icons is clicked, the doPaging subprocedure is triggered. The click event passes a DataGridPageChangedEventArgs parameter to the sub-procedure. In the doPaging procedure we have set the currentPageIndex property of the data grid to the newPageIndex property of this parameter. Then we issued a call to the bindDataGrid procedure as shown in the following code excerpt.The complete code for this application is shown in Figure 3.83 and can be found on the CD that accompanies this book in the file named DataGrid4.aspx.

Sub doPaging(s As Object, e As DataGridPageChangedEventArgs) dataGrid1.CurrentPageIndex=e.NewPageIndex bindDataGrid End Sub

Figure 3.83 DataGrid4.aspx

Figure 3.83 DataGrid4.aspx

<!— Chapter3/DataGrid4.aspx —> <%@ Page Language="VB" Debug="true" %> <%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.OleDb" %> <script language="VB" Debug="true" runat="server"> Sub Page_Load(Source As Object, E As EventArgs) If Not IsPostBack Then bindDataGrid

End If End Sub

Sub bindDataGrid

Dim myConn As OleDbConnection Dim myOleDbAdapter As OleDbDataAdapter Dim connStr, sqlStr As String Dim myDataSet As New Dataset connStr="Provider=Microsoft.Jet.OLEDB.4.0; " _

+ "Data Source=D:\Products.mdb" sqlStr="SELECT ProductId, ProductName, Price " _

+ "FROM Products ORDER BY ProductId" myConn= New OleDbConnection(connStr) myConn.Open()

myOleDbAdapter=New OleDbDataAdapter(sqlStr,myConn)






Figure 3.83 Continued

End Sub

Sub doPaging(s As Object, e As DataGridPageChangedEventArgs) dataGrid1.CurrentPageIndex=e.NewPageIndex bindDataGrid End Sub

<html><head></head><form runat="server">

<asp:DataGrid runat="server" id="dataGrid1" AutoGenerateColumns="true" AllowPaging="true" PageSize="5" PagerStyle-HorizontalAlign="Center" OnPageIndexChanged="doPaging" BackColor="White" BorderWidth="1px" BorderStyle="Solid" Width="100%" BorderColor="Salmon"

CellPadding="2" CellSpacing="0" Font-Name="Verdana" Font-Size="8pt"> <HeaderStyle Font-Size="8" Font-Names="Arial" Font-Bold="True"

BackColor="Yellow" HorizontalAlign="center"> </HeaderStyle>

<AlternatingItemStyle BackColor="Beige"/> </asp:DataGrid> </center></form></html>


Every time we navigate to a different page, the entire data table is populated again, even we if are viewing only five records. Thus, for a very large data table, the speed of execution will slow down significantly. In that case, an alternative technique would involve keeping track of the page numbers programmatically. That can be accomplished by operating on the underlying data table's rows in the cache. We may also employ a Parameterized Stored Procedure to alleviate this problem.

0 0

Post a comment