【问题标题】:Getting the HTML content of the current page with databound controls使用数据绑定控件获取当前页面的 HTML 内容
【发布时间】:2013-04-17 22:23:22
【问题描述】:

我正在尝试创建一个将其所在页面转换为 PDF 的控件。

protected void ConvertPageToPDF_click(object sender, EventArgs e)
{
    string pageHtml;
    byte[] pdfBytes;

    string url = HttpContext.Current.Request.Url.AbsoluteUri;

    // get the HTML for the entire page into pageHtml
    HtmlTextWriter hw = new HtmlTextWriter(new StringWriter());
    this.Page.RenderControl(hw);
    pageHtml = hw.InnerWriter.ToString();

    // send pageHtml to a library for conversion
    // send the PDF to the user
}

这部分有效。在我的页面上,我有几个转发器;他们的内容不会出现在pageHtml 中。关于为什么会这样的任何想法?我应该如何解决这个问题?

【问题讨论】:

标签: c# asp.net repeater


【解决方案1】:

事实证明,我必须在数据绑定到控件后渲染控件。我的 click 方法只是添加了一个事件处理程序:

protected void ConvertRepeaterToPDF_click(object sender, EventArgs e)
{
    // handle the PreRender complete method
    Page.PreRenderComplete += new EventHandler(Page_OnPreRenderComplete);
}

然后在 Page_OnPreRenderComplete 中我可以像上面一样使用 RenderControl。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-07
    • 1970-01-01
    • 1970-01-01
    • 2011-04-05
    相关资源
    最近更新 更多