More in this section

Forums / Developing with Sitefinity / Extract Html content of a webpage

Extract Html content of a webpage

2 posts, 0 answered
  1. Amaresh
    Amaresh avatar
    1 posts
    Registered:
    11 Jan 2012
    19 Jan 2012
    Link to this post
    HI.
    iam facing some issues while extracting html content.
    i have created a page which is inheriting a master page having 2 placeholders.
    1 placeholder i have image and text and in  2 placeholder i have usercontrol which has a button. on click of button iam extracting html content of image and text from 1placeholder
    this is the code iam using for extracting html

    protected void btn_ExtractHtml_Click(object sender, EventArgs e)

     {
    Control c = new Control();
    ContentPlaceHolder cp = (ContentPlaceHolder)this.Page.Master.FindControl("ContentPlaceHolder1");
    string strhtml = renderControl(cp);

    }

    public string renderControl(Control cntr)

    {
    StringBuilder sb = new StringBuilder();
    StringWriter sw = new StringWriter(sb);
    HtmlTextWriter hw = new HtmlTextWriter(sw);
    cntr.RenderControl(hw);
    return sb.ToString();

     }

     iam geeting only Tags without content as shown below

    <div class="sfimageWrp">

    </div><div class="sfContentBlock">

    </div>
    i need content of this tags can anyone help me on this.
    i have attached masterpage and webpage.

  2. Lubomir Velkov
    Lubomir Velkov avatar
    688 posts
    Registered:
    03 Nov 2014
    24 Jan 2012
    Link to this post
    Hello Amaresh,

    You can't get the rendered HTML source in the OnClick event of a Button control - it's too early in page lifecycle. What you can do is assign a code-behind class to a page as per this blog post -

    http://www.sitefinity.com/blogs/petermarinov/posts/11-12-27/sitefinity_and_the_asp_net_code-behind_model.aspx 

    and override the Render method of the page like this -

    protected override void Render(HtmlTextWriter writer)
    {
        var sb = new StringBuilder();
        var tw = new HtmlTextWriter(new System.IO.StringWriter(sb));
        base.Render(tw);
        string sContent = sb.ToString();
        writer.Write(sContent);
    }

    Regards,
    Lubomir Velkov
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
2 posts, 0 answered