Web Studyaspnet.blogspot.com Babyneed.blogspot.com

ASP.Net Web Developer Guide

Power By: eXTReMe Tracker Powered by Blogger  
 
 
 
 

 

 
Important Links
 
   
     
 
Sitemap
 
   
   
 
Reference
 
   

Wednesday, March 29, 2006

Navigating to a Selected Page

In our previous example, we could only move to the previous or next page.We can sure do better than that! We can display a list of page numbers, and the user can click any one of these page numbers to move to the desired page. In this example we will illustrate how to accomplish this objective.The run-time view of the application is shown in Figure 3.84.The code for the application is shown in Figure 3.85. There is actually nothing much new in the code,except that we have set several paging related properties as follows:
AllowPaging="true" PageSize="5" PagerStyle-Mode="NumericPages"
PagerStyle-HorizontalAlign="Center" OnPageIndexChanged="doPaging"
Figure 3.84 Paging in a DataGrid Control

Figure 3.85 DataGrid5.aspx
<!— Chapter3/DataGrid5.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)
myOleDbAdapter.Fill(myDataSet,"dtProducts")
dataGrid1.DataSource=myDataSet.Tables("dtProducts")
dataGrid1.DataBind()
myConn.Close()
End Sub
Sub doPaging(s As Object, e As DataGridPageChangedEventArgs)
dataGrid1.CurrentPageIndex=e.NewPageIndex
bindDataGrid
End Sub
</script>
<html><head></head><form runat="server">
<asp:DataGrid runat="server" id="dataGrid1" AutoGenerateColumns="true"
AllowPaging="true" PageSize="5" PagerStyle-Mode="NumericPages"
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>

0 Comments:

Post a Comment

<< Home