如何将选定的 gridview 详细信息转换为 pdf?出现错误?

发布于 2024-11-28 01:01:00 字数 3460 浏览 0 评论 0原文

代码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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沉溺在你眼里的海 2024-12-05 01:01:00

也许您还需要合并一个 StringBuilder ?

StringBuilder sb = new StringBuilder();
StringWriter tw = new StringWriter(sb);
HtmlTextWriter hw = new HtmlTextWriter(tw);
grdviewPayments.RenderControl(hw);
StringReader sr = new StringReader(hw.ToString());

我猜测是因为我在本页上读到的内容:
http://blogs.x2line.com/al/articles/859.aspx

Maybe you need to incorporate a StringBuilder as well?

StringBuilder sb = new StringBuilder();
StringWriter tw = new StringWriter(sb);
HtmlTextWriter hw = new HtmlTextWriter(tw);
grdviewPayments.RenderControl(hw);
StringReader sr = new StringReader(hw.ToString());

I'm guessing because of what I read on this page:
http://blogs.x2line.com/al/articles/859.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文