ListView Paging
ListView Paging
Setting the pageIndex dynamically at runtime in ListView
Recently I had a requirement in my project where I have to set the start row index of ListView
dynamically at runtime. We had huge number of records in the database table and hence I
implemented paging using DataPager control for ListView. For the Datapager, PageSize is set to 5
and whenever user clicks on Previous button, the previous 5 records are displayed and whenever
user clicks on Next button, Next 5 records are displayed. Like this it goes on. In this below
code snippet I am going to show the easiest way to display the data in listview starting with a
particular index.
Below code shows the aspx page containing the DataPager with NextPreviousPagerField which
displays the Next and Previous buttons for navigation and a ListView which is associated with the
DataPager control using the PagedControlID property of DataPager. I have used SqlDataSource to
bind the data from Emp table to the ListView control.
ListView displays EmpId column value as hyperlink and notice that the NavigateUrl is a url to
Details page, which is appended with the querystring parameter startIndex which is the current
startrowIndex (startrowIndex is the variable declared in the code behind file.) of the page
displayed.
Note: startrowIndex is the variable declared in the code-behind file and it is used to
keep track of the ListView current page startrowindex.
<div>
<asp:DataPager ID="DataPager1" PagedControlID="ListView1" PageSize="3" runat="server">
<Fields >
<asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True"
ShowLastPageButton="True" />
</asp:DataPager>
<asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1"
onpagepropertieschanged="ListView1_PagePropertiesChanged">
<LayoutTemplate>
<table>
<tr runat="server" id="itemPlaceholder">
<td>
<asp:Label ID="Label1" runat="server" Text="Id"></asp:Label>
</td>
<td>
<asp:Label ID="Label2" runat="server" Text="Name"></asp:Label>
</td>
</tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<table>
<tr>
<td>
<asp:Hyperlink ID="hlnkDetails" runat="server" NavigateUrl='<%#
"~/Details.aspx?startIndex="+Server.UrlEncode( startRowIndex.ToString() ) %>'
Text='<%#Eval("EmpId") %>'></asp:HyperLink>
</td>
<td>
<asp:Label ID="Label3" runat="server" Text='<%#Eval("EmpName")
%>'></asp:Label>
</td>
</tr>
</table>
</ItemTemplate>
</asp:ListView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$
ConnectionStrings:MSNETDBConnectionString %>"
SelectCommand="SELECT [EmpId], [EmpName] FROM [Emp]"></asp:SqlDataSource>
</div>
Now when the user clicks on the above hyperlink, User is redirected to Details page with
querystring parameter as startIndex. Now in the Details page we have a back button. When the user
clicks on this back button user is redirected to previous page. In our case it is
TestingPage.aspx along with the same querystring parameter startIndex.
Below is the code in the Back Button click event handler of the Details.aspx page.
protected void btnBack_Click(object sender, EventArgs e)
{
Response.Redirect("~/TestingPage.aspx?startIndex=" + Request.QueryString["startIndex"]);
}
When the user is redirected to TestingPage, We have to display the same records in the listview
which were displayed when the user clicked on the hyperlink in the TestingPage.
public int startRowIndex = 0;//Track the current page start row Index
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (!string.IsNullOrEmpty(Request.QueryString["startIndex"]))//check if querystring
"startIndex" has a value.
{
//Set the page related properties for the ListView in the DataPager control
DataPager1.SetPageProperties(Convert.ToInt32(Request.QueryString["startIndex"]),
DataPager1.MaximumRows, true);
}
}
}
In the above method: DataPager1.SetPageProperties, there are three parameters to the method.
a.startRowIndex: The index of the first record on the page of data.
b.Maximum number of rows: The maximum number of records to be displayed on a single page
of ListView.
c.Rebind ListView: True indicates that the ListView wil be rebinded after the page
properties are set.
Note: Here we have not considered the situation, if the records are deleted or inserted in
the database in which case the startrowIndex might vary.