ASP.Net - DataPager

The Data Pager control is used inside a listview object to setup a paging mechanism.

Here’s a sample of the code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<asp:DataPager 
ID = "DataPager_VendorAccount"
runat = "server"
PagedControlID="lvAccounts">
<Fields>
<asp:NextPreviousPagerField
ButtonType = "Button"
ShowFirstPageButton = "True"
ShowNextPageButton="False" />
<asp:NumericPagerField />
<asp:NextPreviousPagerField
ButtonType = "Button"
ShowLastPageButton = "True"
ShowPreviousPageButton="False" />
</Fields>
</asp:DataPager>

Typically, I place the datapager control inside the layout template of the ListVIew control. This helps the format of the data pager to match the layout of the ListView.

Usually, I have a second control - a simple textbox called “txtPageSize”. I will initialize this with a value (like 10), and when it’s changed I update the pagesize parameter of the listview control.

Here’s an example of the text box

1
2
3
4
5
6
7
<asp:TextBox
ID = "txtPageSize"
runat = "server"
Columns = "3"
Text = "20"
OnTextChanged="txtPageSize__TextChanged"
AutoPostBack="True" />

When it is changed, it calls a routine ‘TxtPageSize__TextChanged”, here’s a sample of that routine.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Public Sub txtPageSize__TextChanged( _
ByVal sender As Object, _
ByVal e As System.EventArgs) 'Handles txtPageSize.TextChanged

Dim txtPageSize As TextBox _
= lvAccounts.FindControl("txtPageSize")

Dim DataPager_VendorAccount As DataPager _
= lvAccounts.FindControl("DataPager_VendorAccount")

If IsNumeric(txtPageSize.Text) Then
DataPager_VendorAccount.PageSize = txtPageSize.Text
End If

If IsNumeric(txtPageSize.Text) Then
DataPager_VendorAccount.PageSize = txtPageSize.Text
SI.sys.ParmSetValue("VendorAccountList_Page_Size", txtPageSize.Text.ToString, BillPay.BO_Staff.Get_ID_byNtUserName(SI.sys.ntUserNameNoWG), 5)
End If

End Sub

I adjust the control, but also I save this value into a systems parameter table, and reload it when the form is reconstructed. I do that with a few lines in the ListView PreRender event. Here’s a sample

1
2
3
4
5
6
7
8
9
Protected Sub lvAccounts_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles lvAccounts.PreRender
'
' page size logic
Dim txtPageSize As TextBox _
= lvAccounts.FindControl("txtPageSize")
Dim DataPager_VendorAccount As DataPager _
= lvAccounts.FindControl("DataPager_VendorAccount")
txtPageSize.Text = SI.sys.ParmGetValue("VendorAccountList_Page_Size", "10", BillPay.BO_Staff.Get_ID_byNtUserName(SI.sys.ntUserNameNoWG), 5)
DataPager_VendorAccount.PageSize = txtPageSize.Text

In theory when someone set’s the page size textbox, it is repopulated when they navigate back to the form.