Click here to Skip to main content
15,894,646 members
Articles / Web Development / ASP.NET

RenderControl doesn't work for GridView

Rate me:
Please Sign up or sign in to vote.
2.07/5 (5 votes)
31 Oct 2007CPOL2 min read 74.3K   9   5
The RenderConrol method of DataGrid doesn't seem to work for GridView. Using the RenderControl method of the DataGrid to render HTML content throws an HTTPException.

Introduction

When you try to use the RenderConrol method of ASP.NET 1.1 DataGrid, it doesn't seem to work for GridView. Using RenderControl method of DataGrid to render HTML content throws an 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 HTML content of a DataGrid rendered like Export to Excel and using AJAX to bind the grid. So we try to use the RenderControl method of the DataGrid to get the HTML content and write it to the output stream.

The above case works fine when we the rendered grid has no controls in it. I.e,, when it is simply loaded with bound columns and no template columns.

Microsoft has accepted it as bug in .NET 1.1, and you can see it here: https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=118285.

I have posted a workaround below.

Using the Code

Every ASPX page has an 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 in the code-behind which is already added to the form. You can verify this by using:

C#
//"Form1" - HTMLForm id and "GridView1" - Grid id
Form1.FindControl("GridView1");

Before using the above code, you have to declare the form instance as.

C#
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 the GridView as below:

C#
System.Text.StringBuilder sb = new System.Text.StringBuilder();
HtmlTextWriter htWriter = new HtmlTextWriter(new System.IO.StringWriter(sb));
GridView1.RenderControl(htWriter);

You will get this 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 the code below which adds the GridView control to the form and uses the RenderControl method of the form instead of that of the 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.

C#
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 get rendered with the grid HTML content. You would get the exact HTML content that GridView renders.

Points of Interest

The interesting thing about this issue is that I tried to find a workaround to fix this issue in the internet, but in vain. Another important factor is that the form instance has to be manually declared like:

C#
protected System.Web.UI.HtmlControls.HtmlForm Form1;
//(Form1 - Form id)

History

No history yet.

License

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


Written By
Software Developer TCS, India
India India
I am Rajesh Thomas. I’ve been into Microsoft .NET technologies since 2003 and my favorites are ASP.Net, C# and JavaScript.

I am a software developer in the .NET Development Platform in India. I started my career since mid 2003. Since then I have been working on .NET, C# and ASP.NET. I largely dedicate myself in understanding and applying the new technologies that bind with .Net Framework. In the recent years, the popularity and advantage of AJAX led me in creating an AJAX Datagrid which is in progress with much more features than normal one.

I am also passionate about JavaScript - I guess that arouse from the fact that I made much lines of JavaScript in the past two years.

I have invariably kept myself busy with side projects along the way that have served as means for continued education, fun and inspiration. My current projects include: ASPNetGrid – an ASP.Net 1.1 Data Grid which enables Column resizing, Header Freezing, and AJAX Data Grid - an asp.net AJAX Grid with lot of features to compete with the Windows.Net Grid.

I did my Bachelor of Engineering Degree in Information Technology.

Comments and Discussions

 
Questioncode on vb.net Pin
torvic02030-Dec-14 5:10
torvic02030-Dec-14 5:10 
QuestionYou are rock... Pin
Vishal Sanchihar 19811-Feb-12 22:28
Vishal Sanchihar 19811-Feb-12 22:28 
GeneralMy vote of 1 Pin
Brady Kelly11-Sep-09 1:47
Brady Kelly11-Sep-09 1:47 
GeneralEven more simple.... Pin
mmgoldstein23-Apr-08 11:19
mmgoldstein23-Apr-08 11:19 
GeneralSimpler way Pin
GilesHinton2-Apr-08 22:48
GilesHinton2-Apr-08 22:48 
There is an easier way to do this which doesn't involve having to hack the generated HTML.

1. Prevent the server checking for this error - we're not technically doing anything wrong; it may just be that the form hasn't been rendered yet; we can get around that by adding this override at the page level:

public override void VerifyRenderingInServerForm(Control control) {
// Confirms that an HtmlForm control is rendered for the
// specified ASP.NET server control at run time.
// No code required here.
}

2. Add your generated control to a PlaceHolder or other existing server tag BEFORE you use RenderControl to send it to the HtmlTextWriter instance.
3. Run the RenderControl method of the control to write its markup into the HtmlTextWriter.
4. Once this is done, just set the visible property of the control to false. It won't get rendered, BUT any events you had tied to it will still work.

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.