Introduction
I found that page navigation of Crystal report viewer is kind of broken. There are different solutions to this problem on the internet but sometimes, it's not possible to use certain solutions because of their limitation or compatibilities. So I will show you how to create custom paging for Crystal report viewer.
Background
It's going to help you if you have any kind of knowledge of Crystal report viewer and C#.
Using the Code
I would guess that viewer knows how to set up a Crystal report viewer in ASP.NET.
Step 1: First Define a Crystal Report Viewer in Your Page Like This
<cr:crystalreportviewer autodatabind="true"
bestfitpage="False" id="CrystalReportViewer1"
runat="server">DisplayToolbar="true"
ToolPanelView="None" SeparatePages="true"
Width="100%" HasPageNavigationButtons="False" />
Make sure to keep DisplayToolbar
property as true
and HasPageNavigationButtons
as false
. We want to show toolbar but to hide the default page index change buttons.
Step 2: Put a Hidden Field Just Below the Crystal Report Viewer
<asp:HiddenField runat="server" ID="hid_current_page" Value="1" />
Step 3: Now We Need Four Buttons for the Navigation
<table>
<tr>
<td>
<asp:ImageButton ID="btn_first" runat="server"
ImageUrl="~/Images/First.png" OnClick="btn_first_Click" />
</td>
<td>
<asp:ImageButton ID="btn_prev" runat="server"
ImageUrl="~/Images/previous.png" OnClick="btn_prev_Click" />
</td>
<td>
<asp:ImageButton ID="btn_next" runat="server"
ImageUrl="~/Images/next.png" OnClick="btn_next_Click" /></td>
<td>
<asp:ImageButton ID="btn_last" runat="server"
ImageUrl="~/Images/last.png" OnClick="btn_last_Click" /></td>
</tr>
</table>
So let me tell you how things will work. We have taken a hidden field to store the current page number of the Crystal report viewer. By default, its value will be 1,
which will indicate the first page the Crystal report viewer. Following are some Crystal report predefined functions that will help us to set the page of the report.
ShowFirstPage()
- This will set the first page of Crystal report viewer RefreshReport()
- Use to refresh the value of Crystal report ShowNthPage(n)
- To set nth page of Crystal report viewer
Following is the code for the navigation button click:
1. First Page Button is Clicked Event
protected void btn_first_Click(object sender, ImageClickEventArgs e)
{
CrystalReportViewer1.ShowFirstPage();
CrystalReportViewer1.RefreshReport();
hid_current_page.Value = "1";
}
2. Previous Page Button Clicked Event
protected void btn_prev_Click(object sender, ImageClickEventArgs e)
{
int current_page = Convert.ToInt32(hid_current_page.Value);
if ((current_page - 1) < 1)
{
hid_current_page.Value = "1";
CrystalReportViewer1.ShowNthPage(1);
}
else
{
hid_current_page.Value = Convert.ToString(current_page - 1);
CrystalReportViewer1.ShowNthPage(current_page - 1);
}
CrystalReportViewer1.RefreshReport();
}
3. When Next Page Button is Clicked
But before setting the next page, we need to find the last page of the Crystal report.
private int getLastPageNumber(CrystalReportViewer crview)
{
crview.ShowLastPage();
CrystalDecisions.Web.ViewInfo vi = crview.ViewInfo;
return vi.PageNumber;
}
3.1 Next Page Button click event
protected void btn_next_Click(object sender, ImageClickEventArgs e)
{
int current_page = Convert.ToInt32(hid_current_page.Value);
int last_page = getLastPageNumber(CrystalReportViewer1);
if ((current_page + 1) > last_page)
{
hid_current_page.Value = Convert.ToString(last_page);
CrystalReportViewer1.ShowNthPage(last_page);
}
else
{
hid_current_page.Value = Convert.ToString(current_page + 1);
CrystalReportViewer1.ShowNthPage(current_page + 1);
}
CrystalReportViewer1.RefreshReport();
}
4. Last Page Button Clicked Event
protected void btn_last_Click(object sender, ImageClickEventArgs e)
{
int last_page = getLastPageNumber(CrystalReportViewer1);
hid_current_page.Value = Convert.ToString(last_page);
CrystalReportViewer1.ShowNthPage(last_page);
CrystalReportViewer1.RefreshReport();
}