Skip to content

Commit

Permalink
#19 - Setup a model parameter for "EnablePaging" that can enable / di…
Browse files Browse the repository at this point in the history
…sable the pager as well as showing all pages at once. Also fixed an issue with url encoding that was causing issues with .NetCore.
  • Loading branch information
alanjuden committed Mar 18, 2017
1 parent 4cbda98 commit 7b20393
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 39 deletions.
28 changes: 17 additions & 11 deletions AlanJuden.MvcReportViewer.NetCore/CoreHtmlHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ public static HtmlString RenderReportViewer(this IHtmlHelper helper, ReportViewe
}
}

sb.AppendLine($" <input type='hidden' id='ReportViewerEnablePaging' name='ReportViewerEnablePaging' value='{model.EnablePaging}' />");

sb.AppendLine(" </div>");
}

Expand All @@ -82,17 +84,21 @@ public static HtmlString RenderReportViewer(this IHtmlHelper helper, ReportViewe
sb.AppendLine(" <div class='ReportViewerToolbar row'>");
sb.AppendLine(" <div class='ReportViewerPager'>");
sb.AppendLine(" <div class='btn-toolbar'>");
sb.AppendLine(" <div class='btn-group'>");
sb.AppendLine($" <a href='#' title='First Page' class='btn btn-default FirstPage'{(contentData.TotalPages == 1 ? " disabled='disabled'" : "")}><span class='glyphicon glyphicon-step-backward'></span></a>");
sb.AppendLine($" <a href='#' title='Previous Page' class='btn btn-default PreviousPage'{(contentData.TotalPages == 1 ? " disabled='disabled'" : "")}><span class='glyphicon glyphicon-chevron-left'></span></a>");
sb.AppendLine(" </div>");
sb.AppendLine(" <div class='btn-group'>");
sb.AppendLine($" <span class='PagerNumbers'><input type='text' id='ReportViewerCurrentPage' name='ReportViewerCurrentPage' class='form-control' value='{contentData.CurrentPage}' /> of <span id='ReportViewerTotalPages'>{contentData.TotalPages}</span></span>");
sb.AppendLine(" </div>");
sb.AppendLine(" <div class='btn-group'>");
sb.AppendLine($" <a href='#' title='Next Page' class='btn btn-default NextPage'{(contentData.TotalPages == 1 ? " disabled='disabled'" : "")}><span class='glyphicon glyphicon-chevron-right'></span></a>");
sb.AppendLine($" <a href='#' title='Last Page' class='btn btn-default LastPage'{(contentData.TotalPages == 1 ? " disabled='disabled'" : "")}><span class='glyphicon glyphicon-step-forward'></span></a>");
sb.AppendLine(" </div>");

if (model.EnablePaging)
{
sb.AppendLine(" <div class='btn-group'>");
sb.AppendLine($" <a href='#' title='First Page' class='btn btn-default FirstPage'{(contentData.TotalPages == 1 ? " disabled='disabled'" : "")}><span class='glyphicon glyphicon-step-backward'></span></a>");
sb.AppendLine($" <a href='#' title='Previous Page' class='btn btn-default PreviousPage'{(contentData.TotalPages == 1 ? " disabled='disabled'" : "")}><span class='glyphicon glyphicon-chevron-left'></span></a>");
sb.AppendLine(" </div>");
sb.AppendLine(" <div class='btn-group'>");
sb.AppendLine($" <span class='PagerNumbers'><input type='text' id='ReportViewerCurrentPage' name='ReportViewerCurrentPage' class='form-control' value='{contentData.CurrentPage}' /> of <span id='ReportViewerTotalPages'>{contentData.TotalPages}</span></span>");
sb.AppendLine(" </div>");
sb.AppendLine(" <div class='btn-group'>");
sb.AppendLine($" <a href='#' title='Next Page' class='btn btn-default NextPage'{(contentData.TotalPages == 1 ? " disabled='disabled'" : "")}><span class='glyphicon glyphicon-chevron-right'></span></a>");
sb.AppendLine($" <a href='#' title='Last Page' class='btn btn-default LastPage'{(contentData.TotalPages == 1 ? " disabled='disabled'" : "")}><span class='glyphicon glyphicon-step-forward'></span></a>");
sb.AppendLine(" </div>");
}
sb.AppendLine(" <div class='btn-group'>");
sb.AppendLine(" <span class='SearchText'>");
sb.AppendLine($" <input type='text' id='ReportViewerSearchText' name='ReportViewerSearchText' class='form-control' value='' />");
Expand Down
36 changes: 36 additions & 0 deletions AlanJuden.MvcReportViewer.NetCore/ReportController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,16 @@ protected ReportViewerModel GetReportViewerModel(HttpRequest request)
var model = new ReportViewerModel();
model.AjaxLoadInitialReport = this.AjaxLoadInitialReport;
model.Credentials = this.NetworkCredentials;

var enablePagingResult = _getRequestValue(request, "ReportViewerEnablePaging");
if (enablePagingResult.HasValue())
{
model.EnablePaging = enablePagingResult.ToBoolean();
}
else
{
model.EnablePaging = true;
}
model.Encoding = this.Encoding;
model.ServerUrl = this.ReportServerUrl;
model.ReportImagePath = this.ReportImagePath;
Expand All @@ -211,5 +221,31 @@ protected ReportViewerModel GetReportViewerModel(HttpRequest request)

return model;
}

private string _getRequestValue(HttpRequest request, string key)
{
if (request.Query != null && request.Query.Keys != null && request.Query.Keys.Contains(key))
{
var values = request.Query[key].ToSafeString().Split(',');
if (values != null && values.Count() > 0)
{
return values[0].ToSafeString();
}
}

try
{
if (request.Form != null && request.Form.Keys != null && request.Form.Keys.Contains(key))
{
return request.Form[key].ToSafeString();
}
}
catch
{
//No need to throw errors, just no Form was passed in and it's unhappy about that
}

return String.Empty;
}
}
}
43 changes: 33 additions & 10 deletions AlanJuden.MvcReportViewer.NetCore/ReportServiceHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,14 @@ public static ReportExportResult ExportReportToFormat(ReportViewerModel model, s
var deviceInfo = $"<DeviceInfo>{outputFormat}{encodingFormat}<Toolbar>False</Toolbar>{htmlFragment}</DeviceInfo>";
if (model.ViewMode == ReportViewModes.View && startPage.HasValue && startPage > 0)
{
deviceInfo = $"<DeviceInfo>{outputFormat}<Toolbar>False</Toolbar>{htmlFragment}<Section>{startPage}</Section></DeviceInfo>";
if (model.EnablePaging)
{
deviceInfo = $"<DeviceInfo>{outputFormat}<Toolbar>False</Toolbar>{htmlFragment}<Section>{startPage}</Section></DeviceInfo>";
}
else
{
deviceInfo = $"<DeviceInfo>{outputFormat}<Toolbar>False</Toolbar>{htmlFragment}</DeviceInfo>";
}
}

var reportParameters = new List<ReportServiceExecution.ParameterValue>();
Expand Down Expand Up @@ -124,18 +131,34 @@ public static ReportExportResult ExportReportToFormat(ReportViewerModel model, s

var executionParameterResult = service.SetReportParameters(executionInfo.ExecutionID, reportParameters.ToArray(), "en-us").Result;

var renderRequest = new ReportServiceExecution.Render2Request(format, deviceInfo, ReportServiceExecution.PageCountMode.Actual);
var result = service.Render2(executionInfo.ExecutionID, renderRequest).Result;
if (model.EnablePaging)
{
var renderRequest = new ReportServiceExecution.Render2Request(format, deviceInfo, ReportServiceExecution.PageCountMode.Actual);
var result = service.Render2(executionInfo.ExecutionID, renderRequest).Result;

extension = result.Extension;
mimeType = result.MimeType;
encoding = result.Encoding;
warnings = result.Warnings;
streamIDs = result.StreamIds;
extension = result.Extension;
mimeType = result.MimeType;
encoding = result.Encoding;
warnings = result.Warnings;
streamIDs = result.StreamIds;

executionInfo = service.GetExecutionInfo(executionHeader.ExecutionID).Result;
exportResult.ReportData = result.Result;
}
else
{
var renderRequest = new ReportServiceExecution.RenderRequest(format, deviceInfo);
var result = service.Render(executionInfo.ExecutionID, renderRequest).Result;

extension = result.Extension;
mimeType = result.MimeType;
encoding = result.Encoding;
warnings = result.Warnings;
streamIDs = result.StreamIds;

exportResult.ReportData = result.Result;
exportResult.ReportData = result.Result;
}

executionInfo = service.GetExecutionInfo(executionHeader.ExecutionID).Result;
}
catch (Exception ex)
{
Expand Down
4 changes: 4 additions & 0 deletions AlanJuden.MvcReportViewer.NetCore/ReportViewerModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ public class ReportViewerModel
/// This indicates whether or not the report will be preloaded when the page loads initially or if it will be an ajax request.
/// </summary>
public bool AjaxLoadInitialReport { get; set; }
/// <summary>
/// Setting this to 'true' enables the paging control and renders a single page at a time. Setting this to 'false' removes the paging control and shows all pages at once.
/// </summary>
public bool EnablePaging { get; set; }

public ReportViewerModel()
{
Expand Down
4 changes: 2 additions & 2 deletions AlanJuden.MvcReportViewer.NetCore/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,12 @@ public static List<string> ToStringList(this string data, string[] separators)

public static string HtmlEncode(this string text)
{
return System.Net.WebUtility.HtmlEncode(text.ToSafeString());
return System.Text.Encodings.Web.HtmlEncoder.Default.Encode(text.ToSafeString());
}

public static string UrlEncode(this string text)
{
return System.Net.WebUtility.UrlEncode(text.ToSafeString());
return System.Text.Encodings.Web.UrlEncoder.Default.Encode(text.ToSafeString());
}

public static string HtmlDecode(this string text)
Expand Down
29 changes: 18 additions & 11 deletions AlanJuden.MvcReportViewer/HtmlHelperExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ public static MvcHtmlString RenderReportViewer(this HtmlHelper helper, ReportVie
}
}

sb.AppendLine($" <input type='hidden' id='ReportViewerEnablePaging' name='ReportViewerEnablePaging' value='{model.EnablePaging}' />");

sb.AppendLine(" </div>");
}

Expand All @@ -79,17 +81,22 @@ public static MvcHtmlString RenderReportViewer(this HtmlHelper helper, ReportVie
sb.AppendLine(" <div class='ReportViewerToolbar row'>");
sb.AppendLine(" <div class='ReportViewerPager'>");
sb.AppendLine(" <div class='btn-toolbar'>");
sb.AppendLine(" <div class='btn-group'>");
sb.AppendLine($" <a href='#' title='First Page' class='btn btn-default FirstPage'{(contentData.TotalPages == 1 ? " disabled='disabled'" : "")}><span class='glyphicon glyphicon-step-backward'></span></a>");
sb.AppendLine($" <a href='#' title='Previous Page' class='btn btn-default PreviousPage'{(contentData.TotalPages == 1 ? " disabled='disabled'" : "")}><span class='glyphicon glyphicon-chevron-left'></span></a>");
sb.AppendLine(" </div>");
sb.AppendLine(" <div class='btn-group'>");
sb.AppendLine($" <span class='PagerNumbers'><input type='text' id='ReportViewerCurrentPage' name='ReportViewerCurrentPage' class='form-control' value='{contentData.CurrentPage}' /> of <span id='ReportViewerTotalPages'>{contentData.TotalPages}</span></span>");
sb.AppendLine(" </div>");
sb.AppendLine(" <div class='btn-group'>");
sb.AppendLine($" <a href='#' title='Next Page' class='btn btn-default NextPage'{(contentData.TotalPages == 1 ? " disabled='disabled'" : "")}><span class='glyphicon glyphicon-chevron-right'></span></a>");
sb.AppendLine($" <a href='#' title='Last Page' class='btn btn-default LastPage'{(contentData.TotalPages == 1 ? " disabled='disabled'" : "")}><span class='glyphicon glyphicon-step-forward'></span></a>");
sb.AppendLine(" </div>");

if (model.EnablePaging)
{
sb.AppendLine(" <div class='btn-group'>");
sb.AppendLine($" <a href='#' title='First Page' class='btn btn-default FirstPage'{(contentData.TotalPages == 1 ? " disabled='disabled'" : "")}><span class='glyphicon glyphicon-step-backward'></span></a>");
sb.AppendLine($" <a href='#' title='Previous Page' class='btn btn-default PreviousPage'{(contentData.TotalPages == 1 ? " disabled='disabled'" : "")}><span class='glyphicon glyphicon-chevron-left'></span></a>");
sb.AppendLine(" </div>");
sb.AppendLine(" <div class='btn-group'>");
sb.AppendLine($" <span class='PagerNumbers'><input type='text' id='ReportViewerCurrentPage' name='ReportViewerCurrentPage' class='form-control' value='{contentData.CurrentPage}' /> of <span id='ReportViewerTotalPages'>{contentData.TotalPages}</span></span>");
sb.AppendLine(" </div>");
sb.AppendLine(" <div class='btn-group'>");
sb.AppendLine($" <a href='#' title='Next Page' class='btn btn-default NextPage'{(contentData.TotalPages == 1 ? " disabled='disabled'" : "")}><span class='glyphicon glyphicon-chevron-right'></span></a>");
sb.AppendLine($" <a href='#' title='Last Page' class='btn btn-default LastPage'{(contentData.TotalPages == 1 ? " disabled='disabled'" : "")}><span class='glyphicon glyphicon-step-forward'></span></a>");
sb.AppendLine(" </div>");
}

sb.AppendLine(" <div class='btn-group'>");
sb.AppendLine(" <span class='SearchText'>");
sb.AppendLine($" <input type='text' id='ReportViewerSearchText' name='ReportViewerSearchText' class='form-control' value='' />");
Expand Down
37 changes: 36 additions & 1 deletion AlanJuden.MvcReportViewer/ReportController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Web;
using System;
using System.Web;
using System.Web.Mvc;

namespace AlanJuden.MvcReportViewer
Expand Down Expand Up @@ -192,6 +193,17 @@ protected ReportViewerModel GetReportViewerModel(HttpRequestBase request)
var model = new ReportViewerModel();
model.AjaxLoadInitialReport = this.AjaxLoadInitialReport;
model.Credentials = this.NetworkCredentials;

var enablePagingResult = _getRequestValue(request, "ReportViewerEnablePaging");
if (enablePagingResult.HasValue())
{
model.EnablePaging = enablePagingResult.ToBoolean();
}
else
{
model.EnablePaging = true;
}

model.Encoding = this.Encoding;
model.ServerUrl = this.ReportServerUrl;
model.ReportImagePath = this.ReportImagePath;
Expand All @@ -200,5 +212,28 @@ protected ReportViewerModel GetReportViewerModel(HttpRequestBase request)

return model;
}
private string _getRequestValue(HttpRequestBase request, string key)
{
var values = request.QueryString.GetValues(key);
if (values != null && values.Length > 0)
{
return values[0].ToSafeString();
}

try
{
if (request.Form != null && request.Form.Keys != null && request.Form[key] != null)
{
return request.Form[key].ToSafeString();
}
}
catch
{
//No need to throw errors, just no Form was passed in and it's unhappy about that
}

return String.Empty;
}

}
}
24 changes: 20 additions & 4 deletions AlanJuden.MvcReportViewer/ReportServiceHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,14 @@ public static ReportExportResult ExportReportToFormat(ReportViewerModel model, s
var deviceInfo = $"<DeviceInfo>{outputFormat}<Toolbar>False</Toolbar>{htmlFragment}</DeviceInfo>";
if (model.ViewMode == ReportViewModes.View && startPage.HasValue && startPage > 0)
{
deviceInfo = $"<DeviceInfo>{outputFormat}{encodingFormat}<Toolbar>False</Toolbar>{htmlFragment}<Section>{startPage}</Section></DeviceInfo>";
if (model.EnablePaging)
{
deviceInfo = $"<DeviceInfo>{outputFormat}{encodingFormat}<Toolbar>False</Toolbar>{htmlFragment}<Section>{startPage}</Section></DeviceInfo>";
}
else
{
deviceInfo = $"<DeviceInfo>{outputFormat}{encodingFormat}<Toolbar>False</Toolbar>{htmlFragment}</DeviceInfo>";
}
}

var reportParameters = new List<ReportServiceExecution.ParameterValue>();
Expand Down Expand Up @@ -95,11 +102,20 @@ public static ReportExportResult ExportReportToFormat(ReportViewerModel model, s
executionInfo = service.LoadReport(model.ReportPath, historyID);
service.SetExecutionParameters(reportParameters.ToArray(), "en-us");

var result = service.Render2(format, deviceInfo, ReportServiceExecution.PageCountMode.Actual, out extension, out mimeType, out encoding, out warnings, out streamIDs);
if (model.EnablePaging)
{
var result = service.Render2(format, deviceInfo, ReportServiceExecution.PageCountMode.Actual, out extension, out mimeType, out encoding, out warnings, out streamIDs);

executionInfo = service.GetExecutionInfo();
exportResult.ReportData = result;
}
else
{
var result = service.Render(format, deviceInfo, out extension, out mimeType, out encoding, out warnings, out streamIDs);

exportResult.ReportData = result;
exportResult.ReportData = result;
}

executionInfo = service.GetExecutionInfo();
}
catch (Exception ex)
{
Expand Down
5 changes: 5 additions & 0 deletions AlanJuden.MvcReportViewer/ReportViewerModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ public class ReportViewerModel
/// </summary>
public bool AjaxLoadInitialReport { get; set; }

/// <summary>
/// Setting this to 'true' enables the paging control and renders a single page at a time. Setting this to 'false' removes the paging control and shows all pages at once.
/// </summary>
public bool EnablePaging { get; set; }

public ReportViewerModel()
{
this.Parameters = new Dictionary<string, string[]>();
Expand Down

0 comments on commit 7b20393

Please sign in to comment.