Gridview render control error in content page

Discussion in 'ASP .Net' started by Julia B, Jul 6, 2009.

  1. Julia B

    Julia B Guest

    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
     
    Julia B, Jul 6, 2009
    #1
    1. Advertising

  2. Julia B

    Julia B Guest

    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


    "Mark Rae [MVP]" wrote:

    > "Julia B" <> wrote in message
    > news:...
    >
    > > 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
    >
    >
     
    Julia B, Jul 6, 2009
    #2
    1. Advertising

  3. Julia B

    Stan Guest

    On 6 July, 13:58, Julia B <> 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
     
    Stan, Jul 7, 2009
    #3
  4. On Mon, 6 Jul 2009 06:45:01 -0700, Julia B
    <> 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
     
    gareth erskine-jones, Jul 7, 2009
    #4
  5. Julia B

    Julia B Guest

    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

    "Stan" wrote:

    > On 6 July, 13:58, Julia B <> 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
    >
     
    Julia B, Jul 7, 2009
    #5
  6. Julia B

    Julia B Guest

    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
    "gareth erskine-jones" wrote:

    > On Mon, 6 Jul 2009 06:45:01 -0700, Julia B
    > <> 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
    >
     
    Julia B, Jul 7, 2009
    #6
  7. 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
     
    sabeer ahamed, Jul 8, 2009
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lau Lei Cheong

    Page.Render do not render complete page

    Lau Lei Cheong, May 15, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    2,093
    Lau Lei Cheong
    May 15, 2004
  2. hazz
    Replies:
    6
    Views:
    49,793
    SkyUCHC
    Jun 9, 2010
  3. Dave L
    Replies:
    3
    Views:
    3,287
    Göran Andersson
    Mar 4, 2010
  4. Stephen Miller
    Replies:
    12
    Views:
    792
    Stephen Miller
    Jan 20, 2004
  5. Glenn Gillen
    Replies:
    0
    Views:
    288
    Glenn Gillen
    Nov 17, 2006
Loading...

Share This Page