This is a migrated thread and some comments may be shown as answers.

Extracting HTML Markup From RadGrid

2 Answers 196 Views
Grid
This is a migrated thread and some comments may be shown as answers.
amonte
Top achievements
Rank 1
amonte asked on 22 Jul 2011, 05:47 AM
Hi,

I am attempting to extract the HTML markup from a RadGrid, but I am having a problem.  Specifically, when the

grd.RenderControl(htmlWriter)

 

line is executed, an error is being thrown stating that the GridLinkButtons must be placed within a form tag.  My goal is to extract the HTML markup of the header columns and grid data items only - if this approach won't work, then I will probably have to look into a technique that will iterate each row of the grid and manually write the HTML (I'd like to avoid this if at all possible).  Here is the code that I have at this point:

public static string ConvertRadGridToHTML(ref Telerik.Web.UI.RadGrid grd)
{
    var stringwriter = new StringWriter();
    var htmlWriter = new HtmlTextWriter(stringwriter);
    string result;
    try
        {
            htmlWriter.RenderBeginTag(HtmlTextWriterTag.Html);
            htmlWriter.RenderBeginTag(HtmlTextWriterTag.Body);
            grd.RenderControl(htmlWriter);
            htmlWriter.RenderEndTag();
            htmlWriter.RenderEndTag();
            htmlWriter.Flush();
            result = htmlWriter.ToString();
        }
    catch
        {
            result = String.Empty;
        }
    finally
        {
            htmlWriter.Close();
            stringwriter.Close();                   
        }
        return result;
}

2 Answers, 1 is accepted

Sort by
0
amonte
Top achievements
Rank 1
answered on 25 Jul 2011, 02:48 PM
I would appreciate some assistance with this if you have any suggestions.  Thanks!
0
Princy
Top achievements
Rank 2
answered on 07 Mar 2013, 05:27 AM
Hi,

Here is the sample code that I tried which worked as expected.
C#:
protected void Button2_Click(object sender, EventArgs e)
   {
      StringBuilder sb = new StringBuilder();
       StringWriter sw = new StringWriter(sb;
       HtmlTextWriter html= new HtmlTextWriter(sw);
       RadGrid1.RenderControl(html;
      StringWriter writer = new StringWriter();
       HtmlTextWriter TextWriter = new HtmlTextWriter(writer);
       RadGrid1.RenderControl(TextWriter);
       string htmlvalue = TextWriter.ToString();
}

Thanks,
Princy
Tags
Grid
Asked by
amonte
Top achievements
Rank 1
Answers by
amonte
Top achievements
Rank 1
Princy
Top achievements
Rank 2
Share this question
or