[英]Control 'testGridview' of type 'SPGridView' must be placed inside a form tag with runat=server
I have a spgridview and want to export its content to pdf.我有一个 spgridview,想将其内容导出为 pdf。 I am using itextsharp for the same.我也在使用 itextsharp。 But error is encountered at rendercontrol call.但是在渲染控制调用时遇到错误。 I have already tried the solutions mentioned in the below thread.我已经尝试了以下线程中提到的解决方案。 Question Also I have tried using 问题我也试过使用
public override void VerifyRenderingInServerForm(Control control)
{
return;
}
but it doesnot work beacuse I am writing the code in usercontrol.但它不起作用,因为我在用户控件中编写代码。 Here is my ascx code.这是我的 ascx 代码。
<SharePoint:SPGridView ID="domainGridview" AllowPaging="false" runat="server" AutoGenerateColumns="False"
ShowHeader="true" ShowHeaderWhenEmpty="true" AllowSorting="True" GridLines="None" EnabledEventValidation="false">
<AlternatingRowStyle CssClass="ms-alternating" />
<Columns>
<SharePoint:SPBoundField DataField = "SiteName" HeaderText = "Team Site Name" SortExpression = "SiteName"></SharePoint:SPBoundField>
<asp:BoundField DataField = "SiteUrl" HtmlEncode="false" HeaderText = "Team Site URL" SortExpression = "SiteUrl" ></asp:BoundField>
</Columns>
</SharePoint:SPGridView>
<asp:Button ID="btnExport" runat="server" Text="Export" OnClick="btnExport_Click" />
This is my usercontrol.ascx.cs code这是我的 usercontrol.ascx.cs 代码
protected void btnExport_Click(object sender, EventArgs e)
{
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
UserControl myControl = (UserControl)LoadControl("DomainUserWebPartUserControl.ascx");
domainGridview.RenderControl(hw);
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition",
"attachment;filename=DataTable.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}
This is the exception i am getting这是我得到的例外
Control 'ctl00_m_g_70a6af81_243a_45cf_a88e_3760d013ca12_ctl00_domainGridview' of type 'SPGridView' must be placed inside a form tag with runat=server. 'SPGridView' 类型的控件 'ctl00_m_g_70a6af81_243a_45cf_a88e_3760d013ca12_ctl00_domainGridview' 必须放置在带有 runat=server 的表单标签内。
Finally I got my solution.最后我得到了我的解决方案。 I am using a different approach alltogether.我正在使用不同的方法。
protected void btnExport_Click(object sender, EventArgs e)
{
dtUIExport = GetData();
//Creating iTextSharp Table from the DataTable data
PdfPTable pdfTable = new PdfPTable(dtUIExport.Columns.Count - 1);
pdfTable.DefaultCell.BorderWidth = 2;
int[] widths = new int[dtUIExport.Columns.Count - 1];
//setting font
BaseFont bf = BaseFont.CreateFont(Environment.GetEnvironmentVariable("windir") + @"\fonts\ARIALUNI.TTF", BaseFont.IDENTITY_H, true);
//setting width
for (int x = 0; x < domainGridview.Columns.Count -1; x++)
{
widths[x] = (int)domainGridview.Columns[x].HeaderText.Length + 100;
}
pdfTable.SetWidths(widths);
//setting headers
for (int i = 0; i < dtUIExport.Columns.Count; i++)
{
if (i == 1)
{ continue; }
iTextSharp.text.Font font = new iTextSharp.text.Font(bf, 10, iTextSharp.text.Font.NORMAL);
font.Color = new BaseColor(domainGridview.HeaderStyle.ForeColor);
string cellText = Server.HtmlDecode(dtUIExport.Columns[i].ToString());
iTextSharp.text.pdf.PdfPCell cell = new PdfPCell(new Phrase(12, cellText, font));
pdfTable.AddCell(cell);
}
//copying the data to pdftable
for (int i = 0; i < dtUIExport.Rows.Count; i++)
{
for (int j = 0; j < dtUIExport.Columns.Count; j++)
{
if (j == 1)
{ continue; }
string cellText = Server.HtmlDecode(dtUIExport.Rows[i][j].ToString());
iTextSharp.text.Font font = new iTextSharp.text.Font(bf, 10, iTextSharp.text.Font.NORMAL);
font.Color = new BaseColor(domainGridview.RowStyle.ForeColor);
iTextSharp.text.pdf.PdfPCell cell = new iTextSharp.text.pdf.PdfPCell(new Phrase(12, cellText, font));
pdfTable.AddCell(cell);
}
}
//creating the PDF DOC
Document pdfDoc = new Document(PageSize.A2, 10f, 10f, 10f, 0f);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
pdfDoc.Add(pdfTable);
pdfDoc.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(pdfTable);
Response.End();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.