Introduction
When you try to use RenderConrol Method of ASP.Net 1.1 DataGrid doesn't seem to work for GridView.Using RenderControlMethod of DataGrid to render HTML content throws a HTTPException "System.Web.HttpException: Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server" ,Which is already inside the form with runat=server
Background
In most of the cases we need to process with HTML content of the DataGrid rendered like Export to Excel and using AJAX to bind the Grid.So we try to use RenderControl method of DataGrid to get the HTML content and write it to the output stream.
The above case works fine when we render grid which has no controls in it, in other way it is simply loaded with boundcolumns and no template columns.
Microsoft has accepted it as bug in .Net 1.1 and you can see the post in
https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=118285
I have posted a workaround here.
Solution
Every aspx page has HTML Form control with runat=server and all the controls placed inside the form tag. The concept is to add the DataGrid control to the form manually at the codebehind which is already added to the Form, you can verify this by using
Form1.FindControl("GridView1");
Before using above code you have to declare form instance as.
protected System.Web.UI.HtmlControls.HtmlForm Form1; (Form1 - Form id)
The above code returns the gridview control which means it is already inside a Form with runat= server.But if you try to use the RenderControl Method of gridview as below
System.Text.StringBuilder sb = new System.Text.StringBuilder();
HtmlTextWriter htWriter = new HtmlTextWriter(new System.IO.StringWriter(sb));
GridView1.RenderControl(htWriter);
You will get below exception
Error message:Control 'GridView1' of type 'GridView' must be placed inside a
form tag with runat=server.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more
information about the error and where it originated in the code.
Exception Details: System.Web.HttpException: Control 'GridView1' of type
'GridView' must be placed inside a form tag with runat=server.
To fix this issue use below block of code which adds GridView control to Form and use RenderControl method of Form instead of Grid.This would include "<form>" tags to the HTMLWriter.The next step is to remove unwanted tags which are added when calling the RenderControl method of the Form.
Form1.Controls.Clear();
Form1.Controls.Add(GridView1);
System.Text.StringBuilder sb = new System.Text.StringBuilder();
HtmlTextWriter htWriter = new HtmlTextWriter(new System.IO.StringWriter(sb));
Form1.RenderControl(htWriter);
string strRenderedHTML = sb.ToString();
int startindex = strRenderedHTML .IndexOf("<table");
int endindex = strRenderedHTML .IndexOf("</table>");
int length = (endindex-startindex)+8;
strRenderedHTML = strRenderedHTML .Substring(startindex,length);
Use the above code to remove "<form>" and viewstate "<input>" tags that got rendered with Grid HTML content .You would get the exact HTML Content that GridView renders