Adding Page Headers and Page Footers to a ReportViewer Report
A report can contain a header and footer that run along the top and bottom of each page, respectively. Headers and footers can contain static text, images, lines, rectangles, borders, background color, and background images. You cannot add data-bound fields or images directly to a header or footer. However, you can write an expression that indirectly references a data-bound field or image that you want to use in a header or footer.
Report headers and footers are not the same as the headers and footers in a table or group. For information about table headers and footers, see Adding Table Data Regions. For more information about how to headers and footers to your report, see How to: Add, Hide, and Remove Page Headers and Footers (ReportViewer Controls).
Each report output format processes pages differently. For more information, see Defining Page Size and Page Breaks in a ReportViewer Report.
Suppressing Headers and Footers on the First Page
Page headers and footers are displayed on the first and last page of a report by default. You can suppress page headers and footers on the first and last page by changing the PrintOnFirstPage or PrintOnLastPage property for the header or footer.
Displaying Variable Data in a Page Header or Footer
Page headers and footers can contain static content, but they are more commonly used to display varying content like page numbers or information about the contents of a page. To display variable data that is different on each page, you must write an expression. To put variable data in a header or footer:
Add a text box to the header or footer.
In the text box, write an expression that produces the variable data that you want to appear.
In the expression, include references to report items on the page (for example, you can reference a text box that contains data from a particular field). Do not include a direct reference to fields in a data set.
You cannot refer directly to a field from a text box in a page header or footer. (For example, you cannot use the expression =Fields!LastName.Value.)
To display field information in a page header or footer, place the field expression in a text box in the body of the report, and then refer to that text box in the page header or footer. The following expression displays the contents of the first instance of a text box named TXT_LastName:
You cannot use aggregate functions on fields in the page header or footer. You can only use an aggregate function on a report item (like the First() function described previously). For common expressions in page headers and footers, see Common Expressions for ReportViewer Reports.
The following sections in this topic show ready-to-use expressions that get variable data commonly used in headers and footers. It also explains how the Excel rendering extension processes headers and footers. For more information about expressions, see Using Expressions in a ReportViewer Report.
Adding a Page Number to a Header or Footer
Footers commonly display a page number. To display a page number in the header or footer of a report, create a text box in the footer and add the following expression:
=Globals.PageNumber & " of " & Globals.TotalPages
Adding the Report Name, Path, and Report Execution time to a Header or Footer
Use the following expressions to show the report title or the report name as it is stored in the report server database with timestamp information about when the report was generated:
=Globals.ReportName & ", dated " & Format(Globals.ExecutionTime, "d")
Adding a Databound Image to a Header or Footer
You can use image data stored in a database in a header or footer. However, you cannot reference database fields from the Image control directly. Instead, you must add a text box in the body of the report and then set the text box to the data field that contains the image (note that the value must be base64 encoded). You can hide the text box in the body of the report to avoid showing the base64 encoded image. Then, you can reference the value of the hidden text box from the Image control in the page header or footer.
For example, suppose you have a report that consists of product information pages. In the header of each page, you want to display a photograph of the product. To print a stored image in the report header, define a hidden text box named TXT_Photo in the body of the report that retrieves the image from the database and use an expression to give it a value:
In the header, add an Image control which uses the TXT_Photo text box, decoded to show the image:
Adding Calculated Page Totals to a Header or Footer
For some reports, it is useful to include a calculated value in the header or footer of each report (for example, a per-page sum total if the page includes numeric values). Because you cannot reference the fields directly, the expression that you put in the header or footer must reference the name of the report item (for example, a text box) rather than the data field:
If the text box is in a table or list that contains repeated rows of data, the value that appears in the header or footer at run time is a sum of all values of all TextBox1 instance data in the table or list for the current page.
When calculating page totals, you can expect to see differences in the totals when you use different rendering extensions to view the report. Paginated output is calculated differently for each rendering extension. The same page that you view in HTML might show different totals when viewed in PDF if the amount of data on the PDF page is different.
Using Headers and Footers to Position Text
You can use headers and footers to position text on a page. For example, suppose you are creating a report that you want to mail out to customers. You can use a header or footer to position the customer address so that it appears in an envelope window when folded.
If you are only using the text box to populate a header or footer, you can hide the text box in the report body. Placement of the text box in the report body can have an affect on whether the value appears on the header or footer of the first or last page of a report. For example, if you have tables, matrices, or lists that cause the report to span multiple pages, the hidden text box value appears on the last page. If you want to appear on the first page, place the hidden text box at the top of the report body.
Working with Page Headers and Footers in Excel
When defining page headers and footers for reports that target the Excel rendering extension, follow these guidelines to achieve best results:
Use page footers to display page numbers.
Use page headers to display images, titles, or other text. Do not put page numbers in the header.
In Excel, page footers have a limited layout. If you define a report that includes complex report items in the page footer, the page footer won't process as you expect when the report is viewed in Excel.
The Excel rendering extension can accommodate images and absolute positioning of simple or complex report items in the page header. A side-effect of supporting a richer page header layout is reduced support for calculating page numbers in the header. In the Excel rendering extension, default settings cause page numbers to be calculated based on the number of worksheets. Depending on how you define the report, this might produce erroneous page numbers. For example, suppose you have a report that renders as a single large worksheet that prints on four pages. If you include page number information in the header, each printed page will show "Page 1 of 1" in the header.
A more accurate page count is based on logical pages that correlate to the dimensions of a printed page. In Excel, the page footer uses logical page numbers automatically. To put the logical page count in the page header, you must configure the device information settings to use simple headers. Be aware that when you use simple headers, you remove the capability of handling complex report layout in the header region.