[英]Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server.?
[英]Control 'MainContent_GridView1' of type 'GridView' must be placed inside a form tag with runat=server
我已經將iTextSharp添加到“項目> ...添加引用”,並將它們添加到了使用:
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;
我在“ GridView1.RenderControl(hw);”行得到錯誤代碼。
如何通過Gridview和HtmlTextWriter添加runat =“ server”:
Control 'MainContent_GridView1' of type 'GridView' must be placed inside a form tag with runat=server.
Button1_Click代碼:
protected void Button1_Click(Object sender,System.EventArgs e)
{
string connStr = ConfigurationManager.ConnectionStrings["PDFMDF"].ConnectionString;
DataSet ds = new DataSet();
try
{
string cmdStr = "SELECT * FROM [GridviewTable];";
using (SqlConnection conn = new SqlConnection(connStr))
{
using (SqlCommand cmd = new SqlCommand(cmdStr, conn))
{
conn.Open();
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
conn.Close();
cmd.Dispose();
conn.Dispose();
}
}
}
catch (Exception ex)
{
TextBox1.Text = ex.ToString();
}
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=this.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.AllowPaging = false;
GridView1.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document PDFdoc = new Document(PageSize.A4, 10.0F, 10.0F, 10.0F, 0.0F);
iTextSharp.text.html.simpleparser.HTMLWorker htmlparser = new iTextSharp.text.html.simpleparser.HTMLWorker(PDFdoc);
PdfWriter.GetInstance(PDFdoc, Response.OutputStream);
PDFdoc.Open();
htmlparser.Parse(sr);
PDFdoc.Close();
Response.Write(PDFdoc);
Response.End();
}
您可以在標記代碼中執行此操作。 例如
<asp:GridView ID="GridView1" runat="server">
在我的標記中,已經准備好代碼runat =“ server”將此代碼添加到您的源代碼中
public override void VerifyRenderingInServerForm(Control control)
{
return;
}
它解決了問題
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.