将html页面存储到字符串中 [英] store a html page in to a string

查看:188
本文介绍了将html页面存储到字符串中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

我有一个.aspx页面.现在页面加载时,我必须将该页面的html部分转换为.pdf格式.为此,我必须将整个html页面存储到一个字符串中,以便将该字符串传递给我的pdf转换器函数.该html页面包含gridview和除图像以外的其他数据.现在我不明白如何将.aspx页面的html部分存储到字符串中.

请帮助我

迫切
提前thanxz

hi all

i have an .aspx page. now on page load i have to convert html part of that page in to .pdf format . for that i have to store whole html page in to a string so that i pass that string to my pdf converter function. this html page contain gridview and other data except images .now i didnt understand how to store html part of .aspx page in to a string .

plz help me

its urgent
thanxz in advance

推荐答案


您可以使用以下方法.这将适用于所有aspx页面(包括任何asp.net控件)
Hi,
you can use the following method. This will work for all aspx pages(which includes any asp.net controls)
private string GetHTMLContent()
        {
            StringWriter sw = new StringWriter();
            HtmlTextWriter w = new HtmlTextWriter(sw);
            //divHTMLWrapper is a div, which we set to runat 'server' and in this DIV the whole controls include
            divHTMLWrapper.RenderControl(w);
            string htmlString = sw.GetStringBuilder().ToString();
        }



//您必须在代码文件中声明此函数



// You must declare this function in the code file

public override void VerifyRenderingInServerForm(Control control)
    {
    }



aspx页面将如下所示.您的aspx控件应位于"divHTMLWrapper"中



The aspx page will look like this. Your aspx controls should be in "divHTMLWrapper"

<form id="form1" runat="server">
<pre lang="xml">
    <div id="divHTMLWrapper" runat="server">
    <div  style="width: 100%; background-position: center center;">
    <div style="margin:auto; width:280px">
    <img runat="server" src="images/logo_new.jpg" style="text-align:center" /></div>
    </div>

</div
</form>

</div
</form>




这并不难.您需要做的就是使用流读取器或任何文件读取机制,打开aspx文件,将其读取到字符串变量中,然后将其传递到所需的任何位置.

代码示例如下:

StreamReader sr =新的StreamReader("FILE PATH");
字符串strText = sr.ReadToEnd();
sr.Close();
Hi,

That isn''t hard to do. All you need to do is using stream reader or any file reading mechanism, open the aspx file, read it into a string variable and pass it wherever you want.

Code sample is as below:

StreamReader sr = new StreamReader("FILE PATH");
string strText = sr.ReadToEnd();
sr.Close();


这篇关于将html页面存储到字符串中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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