Tuesday, November 9, 2010

Paging ListView With DataPager

<asp:ListView ID="EmpListView" runat="server">
<LayoutTemplate>

<tr>
<td align="center">Employee Data</td>
</tr>

</LayoutTemplate>

<ItemTemplate>

<tr>
<td>
<br />
Employee Id:&nbsp;<b><%# Eval("employeeId") %>'></b>
<br /><br />
Employee Name::&nbsp;<b><%# Eval("FirstName") %>'>&nbsp;<%# Eval("LastName") %>'></b>
<br />
</td>
</tr>

</ItemTemplate>

</asp:ListView>
</table>
<br />
<asp:DataPager ID="PagerRow" runat="server" PagedControlID="EmpListView"
OnPreRender="EmpList_PreRender">
<Fields>
<asp:TemplatePagerField>
<PagerTemplate>
Page
<asp:Label runat="server" ID="lbCurrentPage"
Text="<%# Container.TotalRowCount>0 ? (Container.StartRowIndex / Container.PageSize) + 1 : 0 %>" />
of
<asp:Label runat="server" ID="lbTotalPages"
Text="<%# Math.Ceiling ((double)Container.TotalRowCount / Container.PageSize) %>" />
(Total:
<asp:Label runat="server" ID="lbTotalItems"
Text="<%# Container.TotalRowCount%>" />
records)
<br /><br />
</PagerTemplate>
</asp:TemplatePagerField>


<asp:NextPreviousPagerField ButtonType ="Button" ShowFirstPageButton="True" ShowPreviousPageButton="True" ShowNextPageButton="True" ShowLastPageButton="True" />

<asp:TemplatePagerField OnPagerCommand="PagerNextPrevious_OnPagerCommand">
<PagerTemplate>
<br /><br />

<asp:LinkButton ID="lbtnFirst" runat="server" CommandName="First"
Text="First" Visible='<%# Container.StartRowIndex > 0 %>' />

<asp:LinkButton ID="lbtnPrevious" runat="server" CommandName="Previous"
Text='<%# (Container.StartRowIndex - Container.PageSize + 1) + " - " + (Container.StartRowIndex) %>'
Visible='<%# Container.StartRowIndex > 0 %>' />

<asp:Label ID="lbtnCurrent" runat="server"
Text='<%# (Container.StartRowIndex + 1) + "-" + (Container.StartRowIndex + Container.PageSize > Container.TotalRowCount ? Container.TotalRowCount : Container.StartRowIndex + Container.PageSize) %>' />

<asp:LinkButton ID="lbtnNext" runat="server" CommandName="Next"
Text='<%# (Container.StartRowIndex + Container.PageSize + 1) + " - " + (Container.StartRowIndex + Container.PageSize*2 > Container.TotalRowCount ? Container.TotalRowCount : Container.StartRowIndex + Container.PageSize*2) %>'
Visible='<%# (Container.StartRowIndex + Container.PageSize) < Container.TotalRowCount %>' />

<asp:LinkButton ID="lbtnLast" runat="server" CommandName="Last"
Text="Last" Visible='<%# (Container.StartRowIndex + Container.PageSize) < Container.TotalRowCount %>' />
</PagerTemplate>
</asp:TemplatePagerField>

<asp:TemplatePagerField OnPagerCommand = "PagerNo_OnPagerCommand">
<PagerTemplate>
<br /><br />
<asp:TextBox ID="txtPageNo" runat="server" Width="30px" onKeyUp = "value = value.replace(/[^0-9]/g,'')"
Text="<%# Container.TotalRowCount>0 ? (Container.StartRowIndex / Container.PageSize) + 1 : 0 %>" ></asp:TextBox>
<asp:Button ID="btnsubmit" runat="server" Text="Go" />
</PagerTemplate>
</asp:TemplatePagerField>
</Fields>
</asp:DataPager>
private void DataBindList()
{
connectionlayer dl = new connectionlayer();
DataSet emp = new DataSet();
dl.LoadDataSet(CommandType.Text, "SELECT employeeId,LastName,FirstName FROM Employees", ref emp, "emp");
EmpListView.DataSource = emp;
EmpListView.DataBind();

}


protected void PagerNextPrevious_OnPagerCommand(object sender, DataPagerCommandEventArgs e)
{
switch (e.CommandName)
{

case "First":
e.NewStartRowIndex = 0;
e.NewMaximumRows = e.Item.Pager.MaximumRows;
break;
case "Previous":
e.NewStartRowIndex = e.Item.Pager.StartRowIndex - e.Item.Pager.PageSize;
e.NewMaximumRows = e.Item.Pager.MaximumRows;
break;
case "Next":
int NewIndexNo = e.Item.Pager.StartRowIndex + e.Item.Pager.PageSize;
if (NewIndexNo <= e.TotalRowCount)
{
e.NewStartRowIndex = NewIndexNo;
e.NewMaximumRows = e.Item.Pager.MaximumRows;
}
break;

case "Last":
e.NewStartRowIndex = e.TotalRowCount - e.TotalRowCount % e.Item.Pager.PageSize;
e.NewMaximumRows = e.Item.Pager.MaximumRows;
break;
}
}

protected void PagerNo_OnPagerCommand(object sender, DataPagerCommandEventArgs e)
{
int NewPageNo = Convert.ToInt32((e.Item.FindControl("txtPageNo") as TextBox).Text);
int NewIndexNo = e.Item.Pager.PageSize * (NewPageNo - 1);
if (NewIndexNo <= e.TotalRowCount)
{
e.NewStartRowIndex = NewIndexNo;
e.NewMaximumRows = e.Item.Pager.MaximumRows;
}
}
protected void EmpList_PreRender(object sender, EventArgs e)
{
DataBindList();
PagerRow.PageSize = 5;
}

2 comments :

  1. Very impressive. I loved this article!
    One question - have you tied a custom validator to content displayed inside the listview control?

    ReplyDelete

AJAX, asp, Asp.net, asp.net and sql server security, Asp.net IntemIndex, C#, Css, DataBinder.Eval, DataKeyNames, Datalist, Datapager, DataSet, DataTable, DropDownList, FindControl, gridview, JavaScript, jquery, Listview, Paging, Regex, RegularExpression, Repeater, Server side validation, Sql Server, timer, timercallback, Validation, XML, xmlnode, XPath