渲染用户控件并调用它们的Page_Load [英] Render User Controls and call their Page_Load

查看:84
本文介绍了渲染用户控件并调用它们的Page_Load的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面code将无法工作,因为对照组(第1页,第2页,第3页)要求他们的Page_Load事件被调用。

The following code will not work because the controls (page1, page2, page3) require that their "Page_Load" event gets called.

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

public partial class utilities_getPDF : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // add user controls to the page
        AddContentControl("controls/page1");
        AddContentControl("controls/page2");
        AddContentControl("controls/page3");

        // set up the response to download the rendered PDF...
        Response.ContentType = "application/pdf";
        Response.ContentEncoding = System.Text.Encoding.UTF8;
        Response.AddHeader("Content-Disposition", "attachment;filename=FileName.pdf");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);

        // get the rendered HTML of the page
        System.IO.StringWriter stringWrite = new StringWriter();
        System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
        this.Render(htmlWrite);
        StringReader reader = new StringReader(stringWrite.ToString());

        // write the PDF to the OutputStream...
        Document doc = new Document(PageSize.A4);
        HTMLWorker parser = new HTMLWorker(doc);
        PdfWriter.GetInstance(doc, Response.OutputStream);
        doc.Open();
        parser.Parse(reader);
        doc.Close();

    }
    //  the parts are probably irrelevant to the question...
    private const string contentPage = "~/includes/{0}.ascx";
    private void AddContentControl(string page)
    {
        content.Controls.Add(myLoadControl(page));
    }
    private Control myLoadControl(string page)
    {
        return TemplateControl.LoadControl(string.Format(contentPage, page));
    }
}

所以我的问题是:

So my question is:

我如何能得到控制HTML?

推荐答案

首先,为您的动态添加控件参与页面生命周期则需要在页面preINIT事件创建。看到 ASP.NET页生命周期

First of all, for your dynamically added controls to participate in the page life cycle you need to create them in page PreInit event. see ASP.NET Page Life Cycle

然后得到完整的HTML,确保所有页面事件进行了处理,你重写页面的Render方法如下:

Then to get complete HTML, making sure all page events were processed you override page Render method as follows:

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

public partial class utilities_getPDF : System.Web.UI.Page 
{ 
    protected void Page_PreInit(object sender, EventArgs e) 
    { 
        // add user controls to the page 
        AddContentControl("controls/page1"); 
        AddContentControl("controls/page2"); 
        AddContentControl("controls/page3"); 

    } 
    //  the parts are probably irrelevant to the question... 
    private const string contentPage = "~/includes/{0}.ascx"; 
    private void AddContentControl(string page) 
    { 
        content.Controls.Add(myLoadControl(page)); 
    } 
    private Control myLoadControl(string page) 
    { 
        return TemplateControl.LoadControl(string.Format(contentPage, page)); 
    } 

    protected override void Render(HtmlTextWriter writer)
    {
        //ignore asp.net writer and use your own.
        //base.Render(writer);    
        // set up the response to download the rendered PDF... 
        Response.ContentType = "application/pdf"; 
        Response.ContentEncoding = System.Text.Encoding.UTF8; 
        Response.AddHeader("Content-Disposition", "attachment;filename=FileName.pdf"); 
        Response.Cache.SetCacheability(HttpCacheability.NoCache); 

        // get the rendered HTML of the page 
        System.IO.StringWriter stringWrite = new StringWriter(); 
        System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite); 
        base.Render(htmlWrite); 
        htmlWrite.Flush();
        htmlWrite.Close();
        StringReader reader = new StringReader(stringWrite.ToString()); 
        System.IO.MemoryStream stream = new MemoryStream();
        // write the PDF to the OutputStream... 
        Document doc = new Document(PageSize.A4); 
        HTMLWorker parser = new HTMLWorker(doc); 
        PdfWriter.GetInstance(doc, stream); 
        doc.Open(); 
        parser.Parse(reader); 
        doc.Close(); 

        //binary write your pdf file
        Response.Clear();
        Response.BinaryWrite(stream.GetBuffer());
        Response.End();

    }
} 

这篇关于渲染用户控件并调用它们的Page_Load的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆