I understand there is no current support for directly creating a PDF from a PivotGrid.
I am attempting to use a third party pdf generator to produce a pdf. I am attempting to use the PivotGrid's RenderControl method to capture the PivotGrid's HTML and push that HTML to the third party pdf generator.
Here is the code I'm using to attempt to create a PDF from the PivotGrid:
Attempts to create a pdf fail on the call to RenderControl. The expand/collapse buttons are causing me trouble. I'm getting the error below:
Control 'ctl00_FeaturedContent_pivotGrid_ctl04_ctl00_RowExpandCollapseButton_0_0' of type 'Button' must be placed inside a form tag with runat=server.
Is there a different approach I can take? Or can I make the PivotGrid not create the expand/collapse buttons?
I am attempting to use a third party pdf generator to produce a pdf. I am attempting to use the PivotGrid's RenderControl method to capture the PivotGrid's HTML and push that HTML to the third party pdf generator.
Here is the code I'm using to attempt to create a PDF from the PivotGrid:
01.Protected Sub exportPDF_OnClick(ByVal sender As Object, ByVal e As EventArgs)02. Dim stringBuilder As New System.Text.StringBuilder03. Dim stringWriter As New System.IO.StringWriter(stringBuilder)04. Dim pivotGridHtml As String05. Dim htmlTextWriter As New HtmlTextWriter(stringWriter)06. 07. pivotGrid.RenderControl(htmlTextWriter)08. pivotGridHtml = stringBuilder.ToString()09. PdfGenerator.GetDefaultPdfGenerator().HTMLToPDF(pivotGridHtml)10.11.End SubAttempts to create a pdf fail on the call to RenderControl. The expand/collapse buttons are causing me trouble. I'm getting the error below:
Control 'ctl00_FeaturedContent_pivotGrid_ctl04_ctl00_RowExpandCollapseButton_0_0' of type 'Button' must be placed inside a form tag with runat=server.
Is there a different approach I can take? Or can I make the PivotGrid not create the expand/collapse buttons?
8 Answers, 1 is accepted
0
Aaron
Top achievements
Rank 1
answered on 28 Feb 2014, 06:51 PM
Here is the full source for the markup
and the codebeind:
<%@ Page Title="Home Page" Language="VB" MasterPageFile="~/Site.Master" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %><%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI, Version=2014.1.225.40, Culture=neutral, PublicKeyToken=121fae78165ba3d4" %><asp:Content runat="server" ID="FeaturedContent" ContentPlaceHolderID="FeaturedContent"> <style> .rpgRowHeaderField { white-space: nowrap; } </style> <div> <asp:Button runat="server" ID="exportGrid" Text="Export to Excel" OnClick="exportGrid_OnClick"/> <asp:Button runat="server" ID="exportPDF" Text="Export to PDF" OnClick="exportPDF_OnClick"/> </div> <asp:UpdatePanel runat="server" ID="updatePanel"> <ContentTemplate> <telerik:RadPivotGrid ID="pivotGrid" Height="600px" runat="server" OnNeedDataSource="inventoryReportPivotGrid_OnNeedDataSource" AllowSorting="True" AllowPaging="True" EnableConfigurationPanel="True" ShowColumnHeaderZone="False" ShowDataHeaderZone="False" ShowFilterHeaderZone="False" ShowRowHeaderZone="False" AggregatesPosition="Columns" AggregatesLevel="2" ErrorValue="" OnPivotGridCellExporting="inventoryReportPivotGrid_OnPivotGridCellExporting"> <ClientSettings EnableFieldsDragDrop="True"> <Scrolling AllowVerticalScroll="True"></Scrolling> </ClientSettings> <TotalsSettings GrandTotalsVisibility="None" ColumnGrandTotalsPosition="None" ColumnsSubTotalsPosition="None" RowsSubTotalsPosition="Last" RowGrandTotalsPosition="Last"> </TotalsSettings> <Fields> <telerik:PivotGridRowField DataField="State"></telerik:PivotGridRowField> <telerik:PivotGridRowField DataField="Inventory_Type"></telerik:PivotGridRowField> <telerik:PivotGridColumnField DataField="Product"></telerik:PivotGridColumnField> <telerik:PivotGridAggregateField DataField="UOM" Aggregate="Min" UniqueName="UOM"> </telerik:PivotGridAggregateField> <telerik:PivotGridAggregateField DataField="Quantity"></telerik:PivotGridAggregateField> <telerik:PivotGridAggregateField DataField="Sales" DataFormatString="{0:C}"></telerik:PivotGridAggregateField> </Fields> </telerik:RadPivotGrid> </ContentTemplate> </asp:UpdatePanel></asp:Content>and the codebeind:
001.Option Strict On002.Option Explicit On003. 004.Imports System.Data005.Imports System.Data.SqlClient006.Imports System.Drawing007.Imports Telerik.Web.UI008. 009.Partial Class _Default010. Inherits Page011. 012. Protected Sub inventoryReportPivotGrid_OnNeedDataSource(ByVal sender As Object, ByVal e As PivotGridNeedDataSourceEventArgs)013. pivotGrid.DataSource = GetPivotTableData()014. End Sub015. 016. Private Function GetPivotTableData() As DataTable017. Dim sqlCommand As SqlCommand018. Dim connectionString As String = "data source=<SERVER>;initial catalog=<DATABASE>;persist security info=True;packet size=4096;Connect Timeout=30;Max Pool Size=200;Integrated Security=SSPI;"019. Dim sql As XElement = <SQL>020. SELECT *021. FROM Data022. </SQL>023. 024. sqlCommand = New SqlCommand()025. 026. sqlCommand.CommandType = CommandType.Text027. sqlCommand.CommandText = sql.Value028. 029. Return Nterline.DataAccess.ExecuteDataTable(connectionString, sqlCommand)030. End Function031. 032. Protected Sub exportGrid_OnClick(ByVal sender As Object, ByVal e As EventArgs)033. pivotGrid.ExportSettings.IgnorePaging = True034. pivotGrid.ExportToExcel()035. 036. End Sub037. 038. Protected Sub inventoryReportPivotGrid_OnPivotGridCellExporting(ByVal sender As Object, ByVal e As PivotGridCellExportingArgs)039. Dim modelDataCell As PivotGridBaseModelCell = TryCast(e.PivotGridModelCell, PivotGridBaseModelCell)040. If modelDataCell IsNot Nothing Then041. AddStylesToDataCells(modelDataCell, e)042. End If043. 044. If modelDataCell.TableCellType = PivotGridTableCellType.RowHeaderCell Then045. AddStylesToRowHeaderCells(modelDataCell, e)046. End If047. 048. If modelDataCell.TableCellType = PivotGridTableCellType.ColumnHeaderCell Then049. AddStylesToColumnHeaderCells(modelDataCell, e)050. End If051. 052. If modelDataCell.IsGrandTotalCell Then053. e.ExportedCell.Style.BackColor = Color.FromArgb(128, 128, 128)054. e.ExportedCell.Style.Font.Bold = True055. End If056. 057. If IsTotalDataCell(modelDataCell) Then058. e.ExportedCell.Style.BackColor = Color.FromArgb(150, 150, 150)059. e.ExportedCell.Style.Font.Bold = True060. AddBorders(e)061. End If062. 063. If IsGrandTotalDataCell(modelDataCell) Then064. e.ExportedCell.Style.BackColor = Color.FromArgb(128, 128, 128)065. e.ExportedCell.Style.Font.Bold = True066. AddBorders(e)067. End If068. End Sub069. 070. Private Sub AddStylesToDataCells(modelDataCell As PivotGridBaseModelCell, e As PivotGridCellExportingArgs)071. If modelDataCell.Field.UniqueName = "Sales" Then072. e.ExportedCell.Format = "$0.0"073. End If074. End Sub075. 076. Private Sub AddStylesToColumnHeaderCells(modelDataCell As PivotGridBaseModelCell, e As PivotGridCellExportingArgs)077. If e.ExportedCell.Table.Columns(e.ExportedCell.ColIndex).Width = 0 Then078. e.ExportedCell.Table.Columns(e.ExportedCell.ColIndex).Width = 20.0079. End If080. 081. If modelDataCell.IsTotalCell Then082. e.ExportedCell.Style.BackColor = Color.FromArgb(150, 150, 150)083. e.ExportedCell.Style.Font.Bold = True084. Else085. e.ExportedCell.Style.BackColor = Color.FromArgb(192, 192, 192)086. End If087. AddBorders(e)088. End Sub089. 090. Private Sub AddStylesToRowHeaderCells(modelDataCell As PivotGridBaseModelCell, e As PivotGridCellExportingArgs)091. If e.ExportedCell.Table.Columns(e.ExportedCell.ColIndex).Width = 0 Then092. e.ExportedCell.Table.Columns(e.ExportedCell.ColIndex).Width = 11.0093. End If094. If modelDataCell.IsTotalCell Then095. e.ExportedCell.Style.BackColor = Color.FromArgb(150, 150, 150)096. e.ExportedCell.Style.Font.Bold = True097. Else098. e.ExportedCell.Style.BackColor = Color.FromArgb(192, 192, 192)099. End If100. 101. AddBorders(e)102. End Sub103. 104. Private Shared Sub AddBorders(e As PivotGridCellExportingArgs)105. e.ExportedCell.Style.BorderBottomColor = Color.FromArgb(128, 128, 128)106. e.ExportedCell.Style.BorderBottomWidth = New Unit(1)107. e.ExportedCell.Style.BorderBottomStyle = BorderStyle.Solid108. 109. e.ExportedCell.Style.BorderRightColor = Color.FromArgb(128, 128, 128)110. e.ExportedCell.Style.BorderRightWidth = New Unit(1)111. e.ExportedCell.Style.BorderRightStyle = BorderStyle.Solid112. 113. e.ExportedCell.Style.BorderLeftColor = Color.FromArgb(128, 128, 128)114. e.ExportedCell.Style.BorderLeftWidth = New Unit(1)115. e.ExportedCell.Style.BorderLeftStyle = BorderStyle.Solid116. 117. e.ExportedCell.Style.BorderTopColor = Color.FromArgb(128, 128, 128)118. e.ExportedCell.Style.BorderTopWidth = New Unit(1)119. e.ExportedCell.Style.BorderTopStyle = BorderStyle.Solid120. End Sub121. 122. Private Function IsTotalDataCell(modelDataCell As PivotGridBaseModelCell) As Boolean123. Return modelDataCell.TableCellType = PivotGridTableCellType.DataCell AndAlso (modelDataCell.CellType = PivotGridDataCellType.ColumnTotalDataCell OrElse modelDataCell.CellType = PivotGridDataCellType.RowTotalDataCell OrElse modelDataCell.CellType = PivotGridDataCellType.RowAndColumnTotal)124. End Function125. 126. Private Function IsGrandTotalDataCell(modelDataCell As PivotGridBaseModelCell) As Boolean127. Return modelDataCell.TableCellType = PivotGridTableCellType.DataCell AndAlso (modelDataCell.CellType = PivotGridDataCellType.ColumnGrandTotalDataCell OrElse modelDataCell.CellType = PivotGridDataCellType.ColumnGrandTotalRowTotal OrElse modelDataCell.CellType = PivotGridDataCellType.RowGrandTotalColumnTotal OrElse modelDataCell.CellType = PivotGridDataCellType.RowGrandTotalDataCell OrElse modelDataCell.CellType = PivotGridDataCellType.RowAndColumnGrandTotal)128. End Function129. 130. Protected Sub exportPDF_OnClick(ByVal sender As Object, ByVal e As EventArgs)131. Dim stringBuilder As New System.Text.StringBuilder132. Dim stringWriter As New System.IO.StringWriter(stringBuilder)133. Dim pivotGridHtml As String134. Dim htmlTextWriter As New HtmlTextWriter(stringWriter)135. 136. pivotGrid.RenderControl(htmlTextWriter)137. pivotGridHtml = stringBuilder.ToString()138. PdfGenerator.GetDefaultPdfGenerator().HTMLToPDF(pivotGridHtml)139. End Sub140. 141.End Class0
Hi Aaron,
Make sure you are calling the RenderControl of the PivotGrid on Render event handler. If you called it earlier you will receive a multiple exceptions. Please check out the following code snippet.
Regards,
Kostadin
Telerik
Make sure you are calling the RenderControl of the PivotGrid on Render event handler. If you called it earlier you will receive a multiple exceptions. Please check out the following code snippet.
protected override void Render(HtmlTextWriter writer){ StringBuilder sb = new StringBuilder(); StringWriter sw = new StringWriter(sb); HtmlTextWriter htw = new HtmlTextWriter(sw); RadPivotGrid1.RenderControl(htw); string htmlvalue = sb.ToString(); base.Render(writer);}Regards,
Kostadin
Telerik
DevCraft Q1'14 is here! Join the free online conference to see how this release solves your top-5 .NET challenges. Reserve your seat now!
0
Aaron
Top achievements
Rank 1
answered on 05 Mar 2014, 05:40 PM
Your suggestion of calling the pivot grid's RenderControl method in the Render event handler produces the same error I was seeing in the export button's click event.
Control 'ctl00_FeaturedContent_pivotGrid_ctl01_ctl01_rc_Product_Column_LinkButton_Product_Column' of type 'LinkButton' must be placed inside a form tag with runat=server.
The difference is that now the error occurs when the page loads instead of when the "Export to PDF" button is clicked.
Also, the code snippet you provided is doing basically the same thing my exportPDF_OnClick event handler was doing except it does not appear to do anything with the html contained in the string variable, htmlvalue.
Control 'ctl00_FeaturedContent_pivotGrid_ctl01_ctl01_rc_Product_Column_LinkButton_Product_Column' of type 'LinkButton' must be placed inside a form tag with runat=server.
The difference is that now the error occurs when the page loads instead of when the "Export to PDF" button is clicked.
Also, the code snippet you provided is doing basically the same thing my exportPDF_OnClick event handler was doing except it does not appear to do anything with the html contained in the string variable, htmlvalue.
0
Hello Aaron,
In order to overcome this exception you have to override the VerifyRenderingInServerForm method.
Regards,
Kostadin
Telerik
In order to overcome this exception you have to override the VerifyRenderingInServerForm method.
public override void VerifyRenderingInServerForm(Control control){}Regards,
Kostadin
Telerik
DevCraft Q1'14 is here! Watch the online conference to see how this release solves your top-5 .NET challenges. Watch on demand now.
0
Kalyani
Top achievements
Rank 1
answered on 13 Mar 2014, 09:36 PM
Did this work.. I have this in my code
this is in my aspx file.
<asp:Button ID= "Button1" runat ="server" OnClick="exportPDF_OnClick" Text ="PDF"/>
C# file
protected void exportPDF_OnClick(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
string pivotGridHtml;
HtmlTextWriter txtWriter = new HtmlTextWriter(sw);
RadPivotGrid1.RenderControl(txtWriter);
pivotGridHtml = sb.ToString();
lblTest.Text = sb.ToString();
}
public override void VerifyRenderingInServerForm(Control control)
{
}
Now I get this error -
Script control 'RadTreeViewOLAP' is not a registered script control. Script controls must be registered using RegisterScriptControl() before calling RegisterScriptDescriptors().
Parameter name: scriptControl
How do I fix this - Thanks
this is in my aspx file.
<asp:Button ID= "Button1" runat ="server" OnClick="exportPDF_OnClick" Text ="PDF"/>
C# file
protected void exportPDF_OnClick(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
string pivotGridHtml;
HtmlTextWriter txtWriter = new HtmlTextWriter(sw);
RadPivotGrid1.RenderControl(txtWriter);
pivotGridHtml = sb.ToString();
lblTest.Text = sb.ToString();
}
public override void VerifyRenderingInServerForm(Control control)
{
}
Now I get this error -
Script control 'RadTreeViewOLAP' is not a registered script control. Script controls must be registered using RegisterScriptControl() before calling RegisterScriptDescriptors().
Parameter name: scriptControl
How do I fix this - Thanks
0
Hello Kalyani,
Please make sure you are calling the RenderControl of the PivotGrid on Render event handler. My first reply demonstrates how to achieve that.
Regards,
Kostadin
Telerik
Please make sure you are calling the RenderControl of the PivotGrid on Render event handler. My first reply demonstrates how to achieve that.
Regards,
Kostadin
Telerik
DevCraft Q1'14 is here! Watch the online conference to see how this release solves your top-5 .NET challenges. Watch on demand now.
0
Kalyani
Top achievements
Rank 1
answered on 01 Apr 2014, 09:24 PM
Thank you so much. I did change it to render event - so the code I have is like this
public override void VerifyRenderingInServerForm(Control control)
{
}
protected override void Render(HtmlTextWriter writer)
{
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
HtmlTextWriter htw = new HtmlTextWriter(sw);
RadPivotGrid1.RenderControl(htw);
string htmlvalue = sb.ToString();
lbltest.Text = htmlvalue;
base.Render(writer);
}
But then the render method is called as soon as the page is loaded. I would like to get the same thing to work when I click a button. How do we do that. I am trying to use this code for getting a print preview. I have a button and when I click it I was hoping to get a screen shot of the page (essentially whatever is in htmlValue). So something like this -
protected void exportPDF_OnClick(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
string pivotGridHtml;
HtmlTextWriter txtWriter = new HtmlTextWriter(sw);
RadPivotGrid1.RenderControl(txtWriter);
pivotGridHtml = sb.ToString();
lblTest.Text = sb.ToString();
}
Can I achieve it?
Thank you
public override void VerifyRenderingInServerForm(Control control)
{
}
protected override void Render(HtmlTextWriter writer)
{
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
HtmlTextWriter htw = new HtmlTextWriter(sw);
RadPivotGrid1.RenderControl(htw);
string htmlvalue = sb.ToString();
lbltest.Text = htmlvalue;
base.Render(writer);
}
But then the render method is called as soon as the page is loaded. I would like to get the same thing to work when I click a button. How do we do that. I am trying to use this code for getting a print preview. I have a button and when I click it I was hoping to get a screen shot of the page (essentially whatever is in htmlValue). So something like this -
protected void exportPDF_OnClick(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
string pivotGridHtml;
HtmlTextWriter txtWriter = new HtmlTextWriter(sw);
RadPivotGrid1.RenderControl(txtWriter);
pivotGridHtml = sb.ToString();
lblTest.Text = sb.ToString();
}
Can I achieve it?
Thank you
0
Hello Kalyani,
A possible solution is to use a flag to distinguish whether the export command is fired. Please check out the following code snippet.
Regards,
Kostadin
Telerik
A possible solution is to use a flag to distinguish whether the export command is fired. Please check out the following code snippet.
bool isExport = false;protected void ExportButton_Click(object sender, EventArgs e){ isExport = true; RadPivotGrid1.ExportToExcel();}public override void VerifyRenderingInServerForm(Control control){}protected override void Render(HtmlTextWriter writer){ if (isExport) { StringBuilder sb = new StringBuilder(); StringWriter sw = new StringWriter(sb); HtmlTextWriter htw = new HtmlTextWriter(sw); RadPivotGrid1.RenderControl(htw); string htmlvalue = sb.ToString(); lbltest.Text = htmlvalue; base.Render(writer); }}Regards,
Kostadin
Telerik
Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.