我有一个 UserControl ,我正在尝试将我的GridView表导出为PDF。我能够很好地制作CSV文件,但在尝试制作PDF时出错。 (我正在使用iTextSharp库)这是我得到的错误:
“格式为'GridView'的控件'DoNotAddList_1_GridView1'必须放在带有runat = server的表单标记内。”
我的html中有一个表单标记,我尝试将其添加到ascx文件中但没有任何效果。我发现很多人都收到此错误,大多数人都建议将其添加到ascx.cs文件的代码中:
public override void VerifyRenderingInServerForm(Control control) { }
但是这给了我这个错误“找不到合适的方法来覆盖......”
我不知道该怎么办。我一直无法找到解决方案。请帮忙!!
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;
public partial class UserControls_DoNotAddListControl : System.Web.UI.UserControl
{
protected void PDF_Click(object sender, EventArgs e)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition",
"attachment;filename=GridViewExport.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.AllowPaging = false;
GridView1.DataBind();
GridView1.RenderControl(hw);
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();
}
protected void CSV_Click(object sender, EventArgs e)
{
Response.ClearContent();
Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "BloombergDaily.csv"));
Response.ContentType = "application/text";
GridView1.AllowPaging = false;
GridView1.DataBind();
StringBuilder strbldr = new StringBuilder();
for (int i = 0; i < GridView1.Columns.Count; i++)
{
strbldr.Append(GridView1.Columns[i].HeaderText + ',');
}
strbldr.Append("\n");
for (int j = 0; j < GridView1.Rows.Count; j++)
{
for (int k = 0; k < GridView1.Columns.Count; k++)
{
strbldr.Append(GridView1.Rows[j].Cells[k].Text + ',');
}
strbldr.Append("\n");
}
Response.Write(strbldr.ToString());
Response.End();
}
}
这是我在aspx文件中的内容:
<form id="form1" runat="server" action="/">
<div id="donotadd" class="tableWrapper">
<div id="tabs1">
<p>
<div id="tabs-1" >
<div id="DoNotAddListMacro" class="dataTable table_Center">
<umbraco:Macro ID="Macro1" Alias="DoNotAddListControl" runat="server"></umbraco:Macro>
</div>
</div>
</p>
</div>
</div>
</form>
答案 0 :(得分:1)
前段时间我遇到了同样的情况。我看到你已经覆盖了以下方法。但是你把这个方法放在你的用户控件中了吗?那将会给你&#34;找不到合适的方法来覆盖&#34;。需要在.aspx.cs文件中重写此方法,您将删除用户控件。
具有ExportPdf()方法
的UserControl背后的代码 protected void btnExportPdf_Click(object sender, EventArgs e)
{
System.Threading.Thread.Sleep(1000);
ExportPdf();
}
protected void ExportPdf()
{
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter hw = new HtmlTextWriter(sw))
{
//To export all pages
gvCharges.AllowPaging = false;
this.BindRepeater();
gvCharges.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A2, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(pdfDoc);
Response.End();
}
}
}
这里是.ascx文件的来源
<asp:Button ID="btnExportPdf" runat="server" Text="Export to pdf" Visible = "false"
onclick="btnExportPdf_Click" CssClass="btn btn-primary" />
这里是.aspx的源代码,我把usercontrol放到了
<div class="form-section-body">
<table style="width: 100%">
<tr>
<td colspan="2" style="padding: 20px 5px 5px 5px;">
<uc2:PosTrans ID="PosTrans1" runat="server" />
</td>
</tr>
</table>
</div>
这里是.aspx.cs背后的代码,我将用户控件放到了
namespace DFM.Dashboard
{
public partial class Dashboard : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}
}
}
答案 1 :(得分:1)
以下是我找到的解决方案:
protected void BtnPDF_Click(object sender, EventArgs e)
{
GridView gv = new GridView();
gv.DataSource = SqlDataSource1;
gv.DataBind();
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
gv.RenderControl(htw);
var mem = new MemoryStream();
Document document = new Document(PageSize.LETTER, 50, 50, 50, 50);
PdfWriter.GetInstance(document,mem);
document.Open();
iTextSharp.text.html.simpleparser.HTMLWorker hw = new iTextSharp.text.html.simpleparser.HTMLWorker(document);
hw.Parse(new StringReader(sw.ToString()));
document.Close();
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment; filename=" + DateTime.Now);
Response.BinaryWrite(mem.ToArray());
Response.End();
Response.Flush();
Response.Clear();
}
答案 2 :(得分:0)
我知道这可能是左边的字段,但你是否将用户控件放在带有runat =“server”属性的表单标签中?
<form runat="server">
<myprefix:MyControl runat="server" />
</form>
答案 3 :(得分:0)
尝试这个。相应的地方控制类型
public override void VerifyRenderingInServerForm(Control control)
{
Macro grid = control as Macro ;
if (grid != null && grid.ID == "Macro1")
return;
else
base.VerifyRenderingInServerForm(control);
}
<强>更新强> 查看以下链接以获取完整示例