Below is an aspx page defining a DataGrid. I've loaded it up just for grins so if you see something new in there - good. It should all be pretty self-explanatory. The only thing to notice is that just above the DataGrid we have added a TextBox control where the user can type in the number of rows he/she wants to display in the grid. We have initially set the value to 10. The code-behind file will read the value from the textbox and dynamically set the DataGrid's PageSize property with that same value.
|
<%@ Page Language="vb" Src="SpecifyPaging.aspx.vb" Inherits="SpecifyPaging" %> <html> <head> <title>User Specified Paging in a DataGrid</title> </head> <body> <form runat="server"> Show <asp:TextBox id="txtNumRows" runat="server" columns="1" text="10" /> rows at a time <asp:DataGrid runat="server" id="dataGrid" BorderWidth="1px" BackColor="White" BorderStyle="None" BorderColor="#999999" Cellpadding="3" GridLines="Vertical" ItemStyle-ForeColor="Black" ItemStyle-BackColor="#CCCCCC" AlternatingItemStyle-BackColor="#DCDCDC" ShowHeader="True" CssClass="DataGrid" HeaderStyle-BackColor="#000084" HeaderStyle-ForeColor="White" HeaderStyle-Font-Bold="True" AllowPaging="True" OnPageIndexChanged="PageIndexChanged_OnClick" PageSize="10" PagerStyle-Mode="NumericPages" PagerStyle-HorizontalAlign="Center" PagerStyle-ForeColor="Black" PagerStyle-BackColor="#999999"> </asp:DataGrid> </form> </body> </html> |
|
Imports System Imports System.Data Imports System.Data.SqlClient Imports System.Configuration Imports System.Web.UI Imports System.Web.UI.WebControls Public Class SpecifyPaging : Inherits Page Protected WithEvents dataGrid As System.Web.UI.WebControls.DataGrid Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid Protected WithEvents txtNumRows As System.Web.UI.WebControls.TextBox Sub Page_Load(Source As Object, E As EventArgs) If Not Page.IsPostBack Then BindData() End If End Sub Sub BindData() Dim strQuery As String = "SELECT CompanyName, ContactName, " _ & "ContactTitle, Phone, Fax FROM Customers" Dim objConn As New SqlConnection( _ ConfigurationSettings.AppSettings("ConnectionString")) Dim dataAdapter As New SqlDataAdapter(strQuery, objConn) Dim ds As DataSet ds = New DataSet() dataAdapter.Fill(ds) dataGrid.DataSource = ds dataGrid.DataBind() End Sub Sub PageIndexChanged_OnClick(Source As Object, E As DataGridPageChangedEventArgs) dataGrid.PageSize = txtNumRows.Text dataGrid.CurrentPageIndex = e.NewPageIndex BindData() End Sub End Class |