views:

245

answers:

4

Hi, I was wondering if there was a way to some how get the HTML output of a DataGrid. I want the raw HTML after the data has been bound to the grid. Is there some sort of overload for the render method I can use to accomplish this? Thanks.

A: 
protected internal override void Render(HtmlTextWriter writer)
{
     /// use HtmlTextWriter to customize your output
}
Chris Ballance
How would I use that to get the RAW html after the data has been binded to the grid?
Oliver S
Ok, create a writer object and pass it to this function:RenderContents(writer); then the contents of the writer has the output you want.
Chris Ballance
+1  A: 

Even if you did override the Render method and call the base Render method, the HTML would be in the stream.

Perhaps the Control Adapter architecture may help whatever you're trying to accomplish?

BC
Also see this link: http://msdn.microsoft.com/en-us/magazine/cc163543.aspx
Joel Coehoorn
+6  A: 
var outputBuffer = new StringBuilder();
using (var writer = new HtmlTextWriter(new StringWriter(outputBuffer)))
{
    yourDataGrid.RenderControl(writer);
}
outputBuffer.ToString();
bdukes
+1  A: 

You could use this approach in your class (derived from DataGrid):

protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);
    base.Render(hw);

    string html = ProcessHtml(sw.ToString());

    writer.Writer(html);
}
M4N