|
.net
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Gridview render control error in content pageHi 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 "Julia B" <Jul***@discussions.microsoft.com> wrote in message For the record, content pages don't inherit from MasterPages - this is a news:709A6962-1BF0-4DF5-95B8-2E06B8E74D40@microsoft.com... > 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. common misconception. A MasterPage is a UserControl, just like any other. When a content page is requested, it loads its MasterPage (along with any other UserControl it uses) and constructs the HTML markup to be streamed down to the client browser, just like any other page. > When I attempt to render the control I get the error Gridview must be What precisely do you mean by "When I attempt to render the control"? How > placed > inside a form tag with runat=server, which I don't think is possible > because > it's a content form. precisely are *you* trying to render this GridView? Sounds very much like the content page / MasterPage relationship hasn't been set up properly... 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 Show quoteHide quote "Mark Rae [MVP]" wrote: > "Julia B" <Jul***@discussions.microsoft.com> wrote in message > news:709A6962-1BF0-4DF5-95B8-2E06B8E74D40@microsoft.com... > > > 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. > > For the record, content pages don't inherit from MasterPages - this is a > common misconception. A MasterPage is a UserControl, just like any other. > When a content page is requested, it loads its MasterPage (along with any > other UserControl it uses) and constructs the HTML markup to be streamed > down to the client browser, just like any other page. > > > 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. > > What precisely do you mean by "When I attempt to render the control"? How > precisely are *you* trying to render this GridView? > > Sounds very much like the content page / MasterPage relationship hasn't been > set up properly... > > > -- > Mark Rae > ASP.NET MVP > http://www.markrae.net > > On Mon, 6 Jul 2009 06:45:01 -0700, Julia B
<Jul***@discussions.microsoft.com> wrote: > 'render the datagrid All ASP.NET webcontrols check to make sure that they are being> Me.SearchGrid.RenderControl(htmlw) 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 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 Show quoteHide quote "gareth erskine-jones" wrote: > On Mon, 6 Jul 2009 06:45:01 -0700, Julia B > <Jul***@discussions.microsoft.com> wrote: > > > '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 > 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 On 6 July, 13:58, Julia B <Jul***@discussions.microsoft.com> wrote:
Show quoteHide quote > Hi all, Hi Julia> > 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 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 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 Show quoteHide quote "Stan" wrote: > On 6 July, 13:58, Julia B <Jul***@discussions.microsoft.com> wrote: > > 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 >
Other interesting topics
Advice required
Brwoser and screen width? xhtmlConformance mode="Strict" Entity Framework - Reassigning child entity's parent Accessing GridViewRow.DataItem outside of GridView databinding events Gridview binding twice Problems adding HyperLink controls through code. The page cannot be found error message CSS menu Custom PageHandler ??? |
|||||||||||||||||||||||