VerifyRenderingInServerForm(System.Web.UI.Control),同时将网格视图导出为pdf

时间:2011-06-29 07:38:11

标签: c# asp.net export-to-pdf

我尝试将数据从gridview导出到pdf,正常.aspx它正在工作,但是当我在webusercontrl.ascx表单中尝试这个时,它显示错误,如

    VerifyRenderingInServerForm(System.Web.UI.Control)': no suitable method found to override....

如何在webusercontrol.ascx中导出数据请帮帮我... 这是我的代码

     using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
     using System.IO;
     using iTextSharp.text;
   using iTextSharp.text.html.simpleparser;
   using iTextSharp.text.pdf;

  namespace MortgageSaver
  {
      public partial class Paymentreportsviewcontrl : System.Web.UI.UserControl
       {
          MortgagesaverEntities mortgageentity = new MortgagesaverEntities();
          protected void Page_Load(object sender, EventArgs e)
          {
        if (!Page.IsPostBack)
        {
            if (Session["Username"] != null)
            {

                if (Session["Loanid"] != null)
                {
                    txtloanid.Text = Session["Loanid"].ToString();
                    grdviewPayments.Attributes.Add("bordercolor ", "#A4C2CE");
                   GetPaymentReport();
                }
            }
            else
            {
                Response.Redirect("AdminLogin.aspx");
            }



        }

    }
    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 { }

    }
   protected string Getammount(object MonthPayAmt)
    {
        if (MonthPayAmt != "")
        {
            return "$" + MonthPayAmt;
        }
        else
            return null;
    }
   protected void btnviewreport_Click(object sender, EventArgs e)
   {
       grdviewPayments.Attributes.Add("bordercolor ", "#A4C2CE");
       GetPaymentReport();

   }
   public override void VerifyRenderingInServerForm(grdviewPayments)
   {
       /* Verifies that the control is rendered */
   }
   protected void btnemail_Click(object sender, EventArgs e)
   {
       Response.ContentType = "application/pdf";
       Response.AddHeader("content-disposition", "attachment;filename=UserDetails.pdf");
       Response.Cache.SetCacheability(HttpCacheability.NoCache);
       StringWriter sw = new StringWriter();
       HtmlTextWriter hw = new HtmlTextWriter(sw);
       grdviewPayments.AllowPaging = true;
       grdviewPayments.DataBind();
       grdviewPayments.RenderControl(hw);
       grdviewPayments.HeaderRow.Style.Add("width", "15%");
       grdviewPayments.HeaderRow.Style.Add("font-size", "10px");
       grdviewPayments.Style.Add("text-decoration", "none");
       grdviewPayments.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
       grdviewPayments.Style.Add("font-size", "8px");
       StringReader sr = new StringReader(sw.ToString());
       Document pdfDoc = new Document(PageSize.A2, 7f, 7f, 7f, 0f);
       HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
       PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
       pdfDoc.Open();
       htmlparser.Parse(sr);
       pdfDoc.Close();
       Response.Write(pdfDoc);
       Response.End();
  }


}

}

3 个答案:

答案 0 :(得分:5)

如果您使用的是ascx文件,请将此代码添加到您的页面Default.aspx.cs。

example.ascx.cs页面上的代码不起作用。

答案 1 :(得分:2)

只需将此功能添加到您的代码中;

public override void VerifyRenderingInServerForm(Control control)
  {
    /* Confirms that an HtmlForm control is rendered for the specified ASP.NET
       server control at run time. */
  }

同时确保using System.Web.UI也添加了

答案 2 :(得分:2)

您只需在.aspx.cs中添加以下代码

public override void VerifyRenderingInServerForm(Control control){
/* Confirms that an HtmlForm control is rendered for the specified ASP.NET
   server control at run time. */}