Fog Creek Software
Discussion Board

Welcome! and rules

Joel on Software

rendering datagrid to HTMLTextWriter

OK, this is getting to be annoying ... I'm trying to store the html text of a datagrid into a session variable to be used by another page.  (This page pulls up the text, sets Response.ContentType = "application/", and displays the results as an Excel page - that part works fine.)  The idea is to keep from requerying the server when there's a perfectly good datagrid already created, formatted, sorted, and waiting to be used.

Rendering the datagrid text to HTMLTextWriter works beautifully with your plain vanilla datagrid, but crashes if the datagrid has sort or command buttons.
The error is "Control 'dgParts__ctl1__ctl0' of type 'DataGridLinkButton' must be placed inside a form tag with runat=server."  The datagrid on the .aspx page is definitely in a form tag; it's just that when the control is being rendered to the writer that's not coming through somehow.  I've seen several posts about this problem but none on how to fix it short of looping through the datagrid and removing any server-side objects.  I don't want to do that because a) the datagrid should also appear normally on the page and b) it's a pain.  Seems like there might be a way to add form tags to the HTMLWriter and then insert the datagrid code?

Dim oStringWriter As New System.IO.StringWriter
Dim oHtmlTextWriter As New System.Web.UI.HtmlTextWriter(oStringWriter)
dim oDgrid as DataGrid

Session("xltext") = oStringWriter.ToString()

Any suggestions are welcome - I code in VB but can deal with C#.  Thanks!

Friday, December 10, 2004

You could try overriding Page.VerifyRenderingInServerForm.

Saturday, December 11, 2004

*  Recent Topics

*  Fog Creek Home