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

RadGrid to Html Conversion

4 Answers 306 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Muthiullah
Top achievements
Rank 1
Muthiullah asked on 06 Mar 2013, 02:07 PM
I need to know how to convert the Rad grid to html in order to add the Radgrid in emailbody.

4 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 07 Mar 2013, 05:22 AM
Hello,

protected void YourEventName(object sender, EventArgs e)
   {
       StringBuilder sb = new StringBuilder();
       StringWriter tw = new StringWriter(sb);
       HtmlTextWriter hw = new HtmlTextWriter(tw);
 
       RadGrid1.RenderControl(hw);
 
       sb.ToString(); // Acess your Html Code here
   }
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False" OnNeedDataSource="RadGrid1_NeedDataSource"
          AllowPaging="false" AllowFilteringByColumn="false" ShowFooter="true" AllowMultiRowSelection="true"
          AllowMultiRowEdit="true" PageSize="10" RegisterWithScriptManager="false"
       
       >
          <ExportSettings ExportOnlyData="true">
          </ExportSettings>
          <MasterTableView EditMode="EditForms" Name="Parent" DataKeyNames="ID" InsertItemPageIndexAction="ShowItemOnCurrentPage"
              ClientDataKeyNames="ID">
              <Columns>
                  <telerik:GridBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID">
                  </telerik:GridBoundColumn>
                  <telerik:GridBoundColumn DataField="Name" UniqueName="Name" HeaderText="Name">
                  </telerik:GridBoundColumn>
              </Columns>
          </MasterTableView>
      </telerik:RadGrid>
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
   {
       dynamic data = new[] {
           new { ID = 1, Name ="Name1",Comments=""},
           new { ID = 2, Name = "Name2",Comments=""},
           new { ID = 3, Name = "Name3",Comments="ABC"},
            new { ID = 4, Name = "Name4",Comments="XYZ"},
           new { ID = 5, Name = "Name5",Comments=""}
       };
 
       RadGrid1.DataSource = data;
 
   }


Thanks,
Jayesh Goyani
0
Shinu
Top achievements
Rank 2
answered on 07 Mar 2013, 05:22 AM
Hi,

Try the following code to achieve your scenario.
protected void Button2_Click(object sender, EventArgs e)
   {
      StringBuilder SB = new StringBuilder();
       StringWriter SW3 = new StringWriter(SB);
       HtmlTextWriter htmlTW = new HtmlTextWriter(SW3);
       RadGrid1.RenderControl(htmlTW);
      StringWriter oStringWriter = new StringWriter();
       HtmlTextWriter oHtmlTextWriter = new HtmlTextWriter(oStringWriter);
       RadGrid1.RenderControl(oHtmlTextWriter);
       string html = oHtmlTextWriter.ToString();
}

Thanks,
Shinu
0
Margo Noreen
Top achievements
Rank 1
Veteran
answered on 06 Jul 2020, 08:20 PM

Is there any updated solution to this?  When I try this code on a grid with a GridCheckBoxColumn, I get an exception:

 

[HttpException (0x80004005): Control 'ctl00_ContentPlaceHolder1_radGrid1_ctl00_ctl04_ctl00' of type 'CheckBox' must be placed inside a form tag with runat=server.]

 

The exception is also thrown when it's a TemplateColumn with an "asp:Checkbox" control in the ItemTemplate

0
Vessy
Telerik team
answered on 09 Jul 2020, 03:16 PM

Hi Margo,

When the Grid contains a server controls (in CheckBox column or a template column) you have to make sure that the RenderControl() call is made inside the override of the Render event.

For example:

        <telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False" OnNeedDataSource="RadGrid1_NeedDataSource"
            AllowPaging="false" AllowFilteringByColumn="false" ShowFooter="true" AllowMultiRowSelection="true"
            AllowMultiRowEdit="true" PageSize="10" RegisterWithScriptManager="false">
            <ExportSettings ExportOnlyData="true">
            </ExportSettings>
            <MasterTableView EditMode="EditForms" Name="Parent" DataKeyNames="ID" InsertItemPageIndexAction="ShowItemOnCurrentPage"
                ClientDataKeyNames="ID">
                <Columns>
                    <telerik:GridCheckBoxColumn UniqueName="CheckboxColumns"></telerik:GridCheckBoxColumn>
                    <telerik:GridBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="Name" UniqueName="Name" HeaderText="Name">
                    </telerik:GridBoundColumn>
                </Columns>
            </MasterTableView>
        </telerik:RadGrid>
        <telerik:RadButton ID="testButton" runat="server" Text="Generate" OnClick="testButton_Click"></telerik:RadButton>
        <asp:Label ID="resultPanel" runat="server"></asp:Label>

    bool isPrinting = false;

    protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        dynamic data = new[] {
           new { ID = 1, Name ="Name1",Comments=""},
           new { ID = 2, Name = "Name2",Comments=""},
           new { ID = 3, Name = "Name3",Comments="ABC"},
            new { ID = 4, Name = "Name4",Comments="XYZ"},
           new { ID = 5, Name = "Name5",Comments=""}
       };

        RadGrid1.DataSource = data;
    }

    protected void testButton_Click(object sender, EventArgs e)
    {
        isPrinting = true;
    }
    protected override void Render(HtmlTextWriter writer)
    {
        if (isPrinting)
        {
            isPrinting = false;
            StringBuilder sb = new StringBuilder();
            StringWriter tw = new StringWriter(sb);
            HtmlTextWriter hw = new HtmlTextWriter(tw);

            //test
            RadGrid1.RenderControl(hw);
            resultPanel.Text = sb.ToString();
        }
        base.Render(writer);
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
        if (!isPrinting)
            base.VerifyRenderingInServerForm(control);
    }

Regards,
Vessy
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
Tags
Grid
Asked by
Muthiullah
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Shinu
Top achievements
Rank 2
Margo Noreen
Top achievements
Rank 1
Veteran
Vessy
Telerik team
Share this question
or