Gridview render control error in content page

  • Thread starter Thread starter Julia B
  • Start date Start date
J

Julia B

Hi all,

Am trying to render a gridview control in anASP.Net (2.0) app. The gridview
is in a content page which inherits from a master, which I believe is the
problem.

When I attempt to render the control I get the error Gridview must be placed
inside a form tag with runat=server, which I don't think is possible because
it's a content form.

I've tried the solution offered for forms which is to override the content
form's VerifyRenderingInServerForm event but that doesn't work. This is the
only recommended solution I can find online.

Does anyone have any ideas?

Thanks
Julia
 
Sorry Mark, I'm fairly new to this and I'm not necessarily up on the correct
terminology. I've actually trying to export the gridview to excel as follows:

Response.AddHeader("content-disposition",
"attachment;filename=FirestormAUCalSearchResults.xls")

'Set MIME type to word
Response.ContentType = "application/vnd.ms-excel"

'Remove the charset from the Content-Type header.
Response.Charset = ""

' Prepare to export the DataGrid
Dim strw As New System.IO.StringWriter
Dim htmlw As New System.Web.UI.HtmlTextWriter(strw)

strw.GetStringBuilder.Append("<B>")
strw.GetStringBuilder.Append("<U>")
strw.GetStringBuilder.Append("<Font Face='Verdana' Size ='2'>")
strw.WriteLine(Me.SearchLabel.Text)
strw.GetStringBuilder.Append("</U>")
strw.GetStringBuilder.Append("<br>")
strw.WriteLine("Search date: " + Today.ToString)
strw.GetStringBuilder.Append("<br>")
strw.GetStringBuilder.Append("<br>")
strw.GetStringBuilder.Append("</B>")
strw.GetStringBuilder.Append("</Font>")
strw.GetStringBuilder.Append("<table>")
strw.GetStringBuilder.Append("<tr>")
strw.GetStringBuilder.Append("<td colspan=7 align=left")
strw.GetStringBuilder.Append("</td>")
strw.GetStringBuilder.Append("</tr>")

'Set the cell style for all cells with the "text" attribute
strw.GetStringBuilder.Append("<style>.text { mso-number-format:\@; }
</style>")

'render the datagrid
Me.SearchGrid.RenderControl(htmlw)

' Finish the Excel spreadsheet and send the response
strw.GetStringBuilder.Append("</table>")
Response.Write(strw.ToString())
Response.End()

Julia
 
Hi all,

Am trying to render a gridview control in anASP.Net (2.0) app. The gridview
is in a content page which inherits from a master, which I believe is the
problem.

When I attempt to render the control I get the error Gridview must be placed
inside a form tag with runat=server, which I don't think is possible because
it's a content form.

I've tried the solution offered for forms which is to override the content
form's VerifyRenderingInServerForm event but that doesn't work. This is the
only recommended solution I can find online.

Does anyone have any ideas?

Thanks
Julia

Hi Julia

There should be no problem placing a GridView control in a content
page.

Make sure that the ContentPlaceHolder element (destined to contain the
GridView in the content page) is nested inside the Form element of
the Master Page.

HTH
 
'render the datagrid
Me.SearchGrid.RenderControl(htmlw)

All ASP.NET webcontrols check to make sure that they are being
rendered inside a form. It looks like you're trying to export some
data to Excel - but I don't think the approach you're taking will
work, as when you call RenderControl on the GridView, you're clearly
not rendering into a form.

Take a look at this page:

http://mattberseth.com/blog/2007/04/export_gridview_to_excel_1.html

There is a solution, based on a gridview, for exporting to excel - he
binds to the GridView, but then outputs a regular html table, which
Excel is capable of opening.

GSEJ
 
Sorry Stan, my posting was not explicit enough. I was trying to render the
grid to excel (probably the wrong terminology). Anyway, I've now solved the
problem by doing it in a brand new page.

Julia
 
Thanks Gareth

I tried the solution suggested but couldn't get it to work.

I've now fixed it by creating a brand new webform (not content page) and
putting the gridview on that and exporting the data to excel when the page is
opened (my content form only exported to excel when a user decided to press a
button). That originally gave me an extra error about not being able to
perform this operation during render, but I added EnableEventValidation
="false" to the page tags and that seems to have fixed it.

Julia
 
HtmlForm form = new HtmlForm();
string attachment = "attachment; filename=Employee.xls";


Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", attachment);

Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "";
this.EnableViewState = false;
System.IO.StringWriter oStringWriter = new
System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new
System.Web.UI.HtmlTextWriter(oStringWriter);
form.Controls.Add(gridPersons);
//this.ClearControls(gridPersons);
this.Controls.Add(form);
form.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();


try this code
 
Back
Top