如何将选定的 gridview 详细信息转换为 pdf?出现错误?
代码1:
private void GetPaymentReport()
{
try
{
string todate = txttodate.Text;
string[] todatesplt = todate.Split('-');
int todatemnth = Convert.ToInt32(todatesplt[0]);
int todatedate = Convert.ToInt32(todatesplt[1]);
int todateyr = Convert.ToInt32(todatesplt[2]);
string fromdate = txtfromdate.Text;
string[] frmdatesplt = fromdate.Split('-');
int frmdatemnth = Convert.ToInt32(frmdatesplt[0]);
int frmdatedate = Convert.ToInt32(frmdatesplt[1]);
int frmdateyr = Convert.ToInt32(frmdatesplt[2]);
DateTime start = new DateTime(frmdateyr, frmdatemnth, frmdatedate);
DateTime end = new DateTime(todateyr, todatemnth, todatedate);
int loanpaymentid = Convert.ToInt32(Session["Loanid"].ToString());
var query = from p in mortgageentity.Payments
join D in mortgageentity.Debit_Method on p.Debit_Method_ID equals D.Debit_Method_ID
join pt in mortgageentity.Payment_Type on p.Payment_Type_ID equals pt.Payment_Type_ID
where (p.Client_Pmt_Date >= start) && (p.Client_Pmt_Date <= end) && (p.Loan_ID == loanpaymentid)orderby p.Client_Pmt_Date descending
select new
{
p.Pmt_ID,
p.Loan_ID,
p.Client_Pmt_Date,
p.MtgSvr_Pmt_Start_Date2,
D.Debit_Method_Desc,
p.Total_Debit_Amt,
p.CreditAmt,
p.LenderAmt,
pt.Payment_Type_Desc,
p.Return_Code,
p.Returned_Date
};
grdviewPayments.DataSource = query.ToList();
grdviewPayments.DataBind();
}
catch { }
}
代码2:
protected void btnemail_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);
grdviewPayments.AllowPaging = false;
grdviewPayments.DataBind();
grdviewPayments.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();
}
如上所述,我有code1从gridview fromat中的数据库获取数据,code2将gridview数据转换为pdf格式。但我的问题是收到错误,如“控制'ctl00_ContentPlaceHolder1_TabContainer1_TabPanel1_Paymentreport_grdviewPayments'类型'GridView'必须是放置在带有 runat=server 的表单标签内。”在代码2位置:grdviewPayments.RenderControl(hw);
请帮我。 问候。
Code 1:
private void GetPaymentReport()
{
try
{
string todate = txttodate.Text;
string[] todatesplt = todate.Split('-');
int todatemnth = Convert.ToInt32(todatesplt[0]);
int todatedate = Convert.ToInt32(todatesplt[1]);
int todateyr = Convert.ToInt32(todatesplt[2]);
string fromdate = txtfromdate.Text;
string[] frmdatesplt = fromdate.Split('-');
int frmdatemnth = Convert.ToInt32(frmdatesplt[0]);
int frmdatedate = Convert.ToInt32(frmdatesplt[1]);
int frmdateyr = Convert.ToInt32(frmdatesplt[2]);
DateTime start = new DateTime(frmdateyr, frmdatemnth, frmdatedate);
DateTime end = new DateTime(todateyr, todatemnth, todatedate);
int loanpaymentid = Convert.ToInt32(Session["Loanid"].ToString());
var query = from p in mortgageentity.Payments
join D in mortgageentity.Debit_Method on p.Debit_Method_ID equals D.Debit_Method_ID
join pt in mortgageentity.Payment_Type on p.Payment_Type_ID equals pt.Payment_Type_ID
where (p.Client_Pmt_Date >= start) && (p.Client_Pmt_Date <= end) && (p.Loan_ID == loanpaymentid)orderby p.Client_Pmt_Date descending
select new
{
p.Pmt_ID,
p.Loan_ID,
p.Client_Pmt_Date,
p.MtgSvr_Pmt_Start_Date2,
D.Debit_Method_Desc,
p.Total_Debit_Amt,
p.CreditAmt,
p.LenderAmt,
pt.Payment_Type_Desc,
p.Return_Code,
p.Returned_Date
};
grdviewPayments.DataSource = query.ToList();
grdviewPayments.DataBind();
}
catch { }
}
code 2:
protected void btnemail_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);
grdviewPayments.AllowPaging = false;
grdviewPayments.DataBind();
grdviewPayments.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();
}
as above i have code1 to get the data from database in gridview fromat and code2 to conver the gridview data to pdf format.but my prolem is getting error like "Control 'ctl00_ContentPlaceHolder1_TabContainer1_TabPanel1_Paymentreport_grdviewPayments' of type 'GridView' must be placed inside a form tag with runat=server." at code2 position : grdviewPayments.RenderControl(hw);
please help me.
regards.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许您还需要合并一个 StringBuilder ?
我猜测是因为我在本页上读到的内容:
http://blogs.x2line.com/al/articles/859.aspx
Maybe you need to incorporate a StringBuilder as well?
I'm guessing because of what I read on this page:
http://blogs.x2line.com/al/articles/859.aspx