Click here to Skip to main content
13,198,467 members (68,879 online)
Click here to Skip to main content
Add your own
alternative version


5 bookmarked
Posted 24 Oct 2011

Parse out controls from your html page.

, 24 Oct 2011
Rate this:
Please Sign up or sign in to vote.
Parse out controls from your html page.
-Override Render on your page ( In this excel export example we do this in a base class )
-Go through all the controls with Findcontrols and find the control type your looking for
-Render specific controls to a list of strings
-render page to string
-remove specific controls html from page.

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Threading;
using System.Net;
using System.IO;
using System.Collections.Generic;
using System.Text;
/// <summary>
/// Summary description for ReportBase
/// </summary>
public class ReportBase : Page
    const string ExcelExport = "ExcelExport";
    public ReportBase()
        this.Load += new EventHandler(ReportBase_Load);
    void ReportBase_Load(object sender, EventArgs e)
        if (!Page.IsPostBack)
            Session.Add(ExcelExport, Request.QueryString[ExcelExport]);
    protected override void Render(HtmlTextWriter writer)
        if (Session[ExcelExport] != null && bool.Parse(Session[ExcelExport].ToString()))
            List<Control> flatList = Utilities.FindControls(this);
            List<string> removeList = new List<string>();
            foreach (Control item in flatList)
                if (item.GetType() == typeof(Image))
            StringBuilder html = null;
            using (System.IO.StringWriter stringWrite = new System.IO.StringWriter())
                using (HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite))
                    html = new StringBuilder(stringWrite.ToString());
            foreach (var item in removeList)
                html.Replace(item, String.Empty);
    public void Download(string text)
            HttpResponse response = Page.Response;
            // for excel
            //response.AddHeader("cache-control", "must-revalidate");
            //response.ContentType = "application/";
        catch (ThreadAbortException)
            //If the download link is pressed we will get a thread abort.
    public static string RenderControlToHtmlString(Control ctrl)
        StringBuilder sb = new StringBuilder();
        StringWriter tw = new StringWriter(sb);
        HtmlTextWriter hw = new HtmlTextWriter(tw);
        return sb.ToString();


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior)
Canada Canada
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralReason for my vote of 5 Thanks for the nice Code & idea. Pin
Member 432084425-Oct-11 10:32
memberMember 432084425-Oct-11 10:32 
GeneralReason for my vote of 4 Cool, interesting stuff. Commenting... Pin
BrianBissell25-Oct-11 9:35
memberBrianBissell25-Oct-11 9:35 
Reason for my vote of 4
Cool, interesting stuff.

Commenting the code or breaking it up a little with screenshots would certainly add more value.

Thanks for sharing!

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171020.1 | Last Updated 24 Oct 2011
Article Copyright 2011 by rj45
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid