Export to Excel - ASP.net 2.0

Discussion in 'ASP .Net' started by Not Me, Nov 15, 2005.

  1. Not Me

    Not Me Guest

    Hi,

    I'm trying to export from a gridview control, to an excel file using
    code intended for a datagrid control (it's all over the web, can post if
    requested)

    I get the error.

    Control 'gvSearch' of type 'GridView' must be placed inside a form tag
    with runat=server.

    This error is similar to that experienced when the datagrid had
    server-side controls such as edit buttons and the like, but my gridview
    is very plain, just displaying text fields (and it is of course inside a
    form tag with runat=server).

    Are there any tweaks that could be made to allow the code to work? or is
    a completely different method the only way?

    Cheers,
    Chris
     
    Not Me, Nov 15, 2005
    #1
    1. Advertising

  2. Look at the tags. Your's will look something like this:

    </form>
    <asp:GridView id="gvSearch" runat="server" />

    Just make sure you move the GridView tag up inside of the </form> tag. If
    there are other elements that are outside the tag, that also have to be
    above the GridView, pull them up as well.

    This normally happens most often when you throw something at the end of a
    page after you have hand edited the tags in HTML view at least once. It is
    easy to fix.

    --
    Gregory A. Beamer
    MVP; MCP: +I, SE, SD, DBA

    ***********************************************
    Think Outside the Box!
    ***********************************************
    "Not Me" <> wrote in message
    news:dlcius$ejv$...
    > Hi,
    >
    > I'm trying to export from a gridview control, to an excel file using code
    > intended for a datagrid control (it's all over the web, can post if
    > requested)
    >
    > I get the error.
    >
    > Control 'gvSearch' of type 'GridView' must be placed inside a form tag
    > with runat=server.
    >
    > This error is similar to that experienced when the datagrid had
    > server-side controls such as edit buttons and the like, but my gridview is
    > very plain, just displaying text fields (and it is of course inside a form
    > tag with runat=server).
    >
    > Are there any tweaks that could be made to allow the code to work? or is a
    > completely different method the only way?
    >
    > Cheers,
    > Chris
     
    Cowboy \(Gregory A. Beamer\), Nov 15, 2005
    #2
    1. Advertising

  3. Not Me

    Not Me Guest

    Cowboy (Gregory A. Beamer) wrote:
    > Look at the tags. Your's will look something like this:
    >
    > </form>
    > <asp:GridView id="gvSearch" runat="server" />
    >
    > Just make sure you move the GridView tag up inside of the </form> tag. If
    > there are other elements that are outside the tag, that also have to be
    > above the GridView, pull them up as well.


    Thanks, but that it not the case with my code, the gridview (in fact
    pretty much the whole page) is within the <form> tags.

    Any other ideas? I'm guessing from your response that there's no reason
    for this not to work under asp.net 2.0?

    Cheers,
    Chris
     
    Not Me, Nov 15, 2005
    #3
  4. Not Me

    PL Guest

    > Any other ideas? I'm guessing from your response that there's no reason for this not to work under asp.net 2.0?

    Make sure all tags are properly closed, validate the html.

    Strange results can happen if there are unclosed tags within the form ruant=server
    and inside the datagrid/gridview.

    PL.
     
    PL, Nov 15, 2005
    #4
  5. Not Me

    Not Me Guest

    PL wrote:
    >>Any other ideas? I'm guessing from your response that there's no reason for this not to work under asp.net 2.0?


    > Make sure all tags are properly closed, validate the html.
    >
    > Strange results can happen if there are unclosed tags within the form ruant=server
    > and inside the datagrid/gridview.


    Bizarrely I had a 'runat=server' in the <head> tags :) however removing
    that did not solve the problem, and I couldn't find any other issues.

    I apologise for the code-dump but if it helps this is the page causing
    issues...(bigsearchresults.aspx)


    **********************************************
    <%@ Page Language="VB" AutoEventWireup="false"
    CodeFile="BigSearchResults.aspx.vb" Inherits="BigSearchResults" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
    "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Results!</title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <asp:Button ID="btExport" runat="server" OnClick="btExport_Click"
    Text="Export" Style="position: relative; z-index: 100;" />

    <asp:SqlDataSource ID="dsSearch" runat="server"
    ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
    SelectCommand="aspBigSearch" SelectCommandType="StoredProcedure">

    <SelectParameters>
    <asp:SessionParameter SessionField="ac" Name="ac" Type="String" />
    <asp:SessionParameter SessionField="ad" Name="ad" Type="String" />
    <asp:SessionParameter SessionField="cd" Name="cd" Type="String" />
    <asp:SessionParameter SessionField="course" Name="course"
    Type="String" ConvertEmptyStringToNull="false" />
    <asp:SessionParameter SessionField="yearfrom" Name="gradyearfrom"
    Type="Int32" DefaultValue="0" />
    <asp:SessionParameter SessionField="yearto" Name="gradyearto"
    Type="Int32" DefaultValue="0" />
    </SelectParameters>
    </asp:SqlDataSource>

    <asp:GridView ID="gvSearch" runat="server" DataSourceID="dsSearch"
    BackColor="Silver" EmptyDataText="Sorry, your search has come up empty!"
    AllowSorting="false">
    </asp:GridView>
    </div>
    </form>
    </body>
    </html>
    *******************************************

    the codebehind bigsearchresults.vb is thus:

    *******************************************
    Partial Class BigSearchResults Inherits System.Web.UI.Page

    Protected Sub btExport_Click(ByVal sender As Object, ByVal e As
    System.EventArgs) Handles btExport.Click

    Response.ContentType = "application/vnd.ms-excel"
    Response.Charset = ""
    Me.EnableViewState = False

    Dim tw As New System.IO.StringWriter()
    Dim hw As New System.Web.UI.HtmlTextWriter(tw)

    gvSearch.RenderControl(hw)
    Response.Write(tw.ToString())
    Response.End()

    End Sub
    ****************************

    Really apprecaite any help!


    Cheers,
    Chris
     
    Not Me, Nov 16, 2005
    #5
  6. Alternatively, you could fake a form in the HtmlTextWriter, like this:

    Protected Sub btExport_Click(ByVal sender As Object, ByVal e As
    System.EventArgs) Handles btExport.Click

    Response.ContentType = "application/vnd.ms-excel"
    Response.Charset = ""
    Me.EnableViewState = False

    Dim tw As New System.IO.StringWriter()
    Dim hw As New System.Web.UI.HtmlTextWriter(tw)

    Dim frm As HtmlForm = new HtmlForm()
    Me.Controls.Add(frm)
    frm.Controls.Add(gvSearch)

    frm.RenderControl(hw)

    Response.Write(tw.ToString())
    Response.End()

    End Sub


    "Not Me" wrote:

    > PL wrote:
    > >>Any other ideas? I'm guessing from your response that there's no reason for this not to work under asp.net 2.0?

    >
    > > Make sure all tags are properly closed, validate the html.
    > >
    > > Strange results can happen if there are unclosed tags within the form ruant=server
    > > and inside the datagrid/gridview.

    >
    > Bizarrely I had a 'runat=server' in the <head> tags :) however removing
    > that did not solve the problem, and I couldn't find any other issues.
    >
    > I apologise for the code-dump but if it helps this is the page causing
    > issues...(bigsearchresults.aspx)
    >
    >
    > **********************************************
    > <%@ Page Language="VB" AutoEventWireup="false"
    > CodeFile="BigSearchResults.aspx.vb" Inherits="BigSearchResults" %>
    >
    > <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
    > "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    > <html xmlns="http://www.w3.org/1999/xhtml">
    > <head>
    > <title>Results!</title>
    > </head>
    > <body>
    > <form id="form1" runat="server">
    > <div>
    > <asp:Button ID="btExport" runat="server" OnClick="btExport_Click"
    > Text="Export" Style="position: relative; z-index: 100;" />
    >
    > <asp:SqlDataSource ID="dsSearch" runat="server"
    > ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
    > SelectCommand="aspBigSearch" SelectCommandType="StoredProcedure">
    >
    > <SelectParameters>
    > <asp:SessionParameter SessionField="ac" Name="ac" Type="String" />
    > <asp:SessionParameter SessionField="ad" Name="ad" Type="String" />
    > <asp:SessionParameter SessionField="cd" Name="cd" Type="String" />
    > <asp:SessionParameter SessionField="course" Name="course"
    > Type="String" ConvertEmptyStringToNull="false" />
    > <asp:SessionParameter SessionField="yearfrom" Name="gradyearfrom"
    > Type="Int32" DefaultValue="0" />
    > <asp:SessionParameter SessionField="yearto" Name="gradyearto"
    > Type="Int32" DefaultValue="0" />
    > </SelectParameters>
    > </asp:SqlDataSource>
    >
    > <asp:GridView ID="gvSearch" runat="server" DataSourceID="dsSearch"
    > BackColor="Silver" EmptyDataText="Sorry, your search has come up empty!"
    > AllowSorting="false">
    > </asp:GridView>
    > </div>
    > </form>
    > </body>
    > </html>
    > *******************************************
    >
    > the codebehind bigsearchresults.vb is thus:
    >
    > *******************************************
    > Partial Class BigSearchResults Inherits System.Web.UI.Page
    >
    > Protected Sub btExport_Click(ByVal sender As Object, ByVal e As
    > System.EventArgs) Handles btExport.Click
    >
    > Response.ContentType = "application/vnd.ms-excel"
    > Response.Charset = ""
    > Me.EnableViewState = False
    >
    > Dim tw As New System.IO.StringWriter()
    > Dim hw As New System.Web.UI.HtmlTextWriter(tw)
    >
    > gvSearch.RenderControl(hw)
    > Response.Write(tw.ToString())
    > Response.End()
    >
    > End Sub
    > ****************************
    >
    > Really apprecaite any help!
    >
    >
    > Cheers,
    > Chris
    >
    >
     
    =?Utf-8?B?SkdvcmRvbg==?=, Nov 16, 2005
    #6
  7. The gridview control is being asked to render itself inside an empty
    htmltextwriter. Thus it is not inside a <form runat="server"...> tag.

    Try changing your btExport method to this:

    Protected Sub btExport_Click(ByVal sender As Object, ByVal e As
    System.EventArgs) Handles btExport.Click
    Response.ContentType = "application/vnd.ms-excel"
    btExport.Visible = False
    End Sub




    "Not Me" wrote:

    > PL wrote:
    > >>Any other ideas? I'm guessing from your response that there's no reason for this not to work under asp.net 2.0?

    >
    > > Make sure all tags are properly closed, validate the html.
    > >
    > > Strange results can happen if there are unclosed tags within the form ruant=server
    > > and inside the datagrid/gridview.

    >
    > Bizarrely I had a 'runat=server' in the <head> tags :) however removing
    > that did not solve the problem, and I couldn't find any other issues.
    >
    > I apologise for the code-dump but if it helps this is the page causing
    > issues...(bigsearchresults.aspx)
    >
    >
    > **********************************************
    > <%@ Page Language="VB" AutoEventWireup="false"
    > CodeFile="BigSearchResults.aspx.vb" Inherits="BigSearchResults" %>
    >
    > <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
    > "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    > <html xmlns="http://www.w3.org/1999/xhtml">
    > <head>
    > <title>Results!</title>
    > </head>
    > <body>
    > <form id="form1" runat="server">
    > <div>
    > <asp:Button ID="btExport" runat="server" OnClick="btExport_Click"
    > Text="Export" Style="position: relative; z-index: 100;" />
    >
    > <asp:SqlDataSource ID="dsSearch" runat="server"
    > ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
    > SelectCommand="aspBigSearch" SelectCommandType="StoredProcedure">
    >
    > <SelectParameters>
    > <asp:SessionParameter SessionField="ac" Name="ac" Type="String" />
    > <asp:SessionParameter SessionField="ad" Name="ad" Type="String" />
    > <asp:SessionParameter SessionField="cd" Name="cd" Type="String" />
    > <asp:SessionParameter SessionField="course" Name="course"
    > Type="String" ConvertEmptyStringToNull="false" />
    > <asp:SessionParameter SessionField="yearfrom" Name="gradyearfrom"
    > Type="Int32" DefaultValue="0" />
    > <asp:SessionParameter SessionField="yearto" Name="gradyearto"
    > Type="Int32" DefaultValue="0" />
    > </SelectParameters>
    > </asp:SqlDataSource>
    >
    > <asp:GridView ID="gvSearch" runat="server" DataSourceID="dsSearch"
    > BackColor="Silver" EmptyDataText="Sorry, your search has come up empty!"
    > AllowSorting="false">
    > </asp:GridView>
    > </div>
    > </form>
    > </body>
    > </html>
    > *******************************************
    >
    > the codebehind bigsearchresults.vb is thus:
    >
    > *******************************************
    > Partial Class BigSearchResults Inherits System.Web.UI.Page
    >
    > Protected Sub btExport_Click(ByVal sender As Object, ByVal e As
    > System.EventArgs) Handles btExport.Click
    >
    > Response.ContentType = "application/vnd.ms-excel"
    > Response.Charset = ""
    > Me.EnableViewState = False
    >
    > Dim tw As New System.IO.StringWriter()
    > Dim hw As New System.Web.UI.HtmlTextWriter(tw)
    >
    > gvSearch.RenderControl(hw)
    > Response.Write(tw.ToString())
    > Response.End()
    >
    > End Sub
    > ****************************
    >
    > Really apprecaite any help!
    >
    >
    > Cheers,
    > Chris
    >
    >
     
    =?Utf-8?B?SkdvcmRvbg==?=, Nov 16, 2005
    #7
  8. Not Me

    Not Me Guest

    JGordon wrote:
    > "Not Me" wrote:
    >> Response.ContentType = "application/vnd.ms-excel"
    >> Response.Charset = ""
    >> Me.EnableViewState = False
    >>
    >> Dim tw As New System.IO.StringWriter()
    >> Dim hw As New System.Web.UI.HtmlTextWriter(tw)
    >>
    >> gvSearch.RenderControl(hw)
    >> Response.Write(tw.ToString())
    >> Response.End()


    > Alternatively, you could fake a form in the HtmlTextWriter, like this:
    > Dim tw As New System.IO.StringWriter()
    > Dim hw As New System.Web.UI.HtmlTextWriter(tw)
    > Dim frm As HtmlForm = new HtmlForm()
    > Me.Controls.Add(frm)
    > frm.Controls.Add(gvSearch)
    > frm.RenderControl(hw)
    > Response.Write(tw.ToString())
    > Response.End()


    Genius!

    That worked a treat, though I'm not sure why? - is it because I declare
    the gridView inside the .aspx page that means it's within the <form>
    tags, but things declared in the .vb page are outside of it? I'll have
    to keep that in mind for future use.

    Much appreciated, thank you!
    Chris
     
    Not Me, Nov 17, 2005
    #8
  9. I'm getting the same error and have tried all the suggestions from the
    replies to this but to no avail. Here is my situation.

    Gridview on page, page contains a master page. I have tried removing the
    MasterPage and the RenderControl still doesn't work.

    The following code
    Dim stringWrite As New System.IO.StringWriter
    Dim htmlWrite As New System.Web.UI.HtmlTextWriter(stringWrite)
    grdQueryResults.RenderControl(htmlWrite)

    will yield this error:
    Control 'grdQueryResults' of type 'GridView' must be placed inside a form
    tag with runat=server

    I DEFINATELY have the GridView in a properly formed <Form> tag.
    --------

    If I use the method described by faking the form tag I get this error:
    RegisterForEventValidation can only be called during Render();
    -----

    Also, if I use the method described on several other sites by adding an empty
    Public Overrides Sub VerifyRenderingInServerForm
    I get the same
    RegisterForEventValidation can only be called during Render();
    error

    Any thoughts on this as this is driving me CRAZY. The only alternative
    which I find not really acceptable is to use a DataGrid when exporting is
    required. The RenderControl method on the DataGrid works as expected.

    Thanks
    -Chris


    "Not Me" wrote:

    > Hi,
    >
    > I'm trying to export from a gridview control, to an excel file using
    > code intended for a datagrid control (it's all over the web, can post if
    > requested)
    >
    > I get the error.
    >
    > Control 'gvSearch' of type 'GridView' must be placed inside a form tag
    > with runat=server.
    >
    > This error is similar to that experienced when the datagrid had
    > server-side controls such as edit buttons and the like, but my gridview
    > is very plain, just displaying text fields (and it is of course inside a
    > form tag with runat=server).
    >
    > Are there any tweaks that could be made to allow the code to work? or is
    > a completely different method the only way?
    >
    > Cheers,
    > Chris
    >
     
    =?Utf-8?B?QyBPJydCcmllbg==?=, Nov 29, 2005
    #9
  10. C O Brien
    Try aremoving the AllowSorting attribute from the grid view tag--
    Paul Zanbaka
    Programmer Analyst
    www.mycodekb.com


    "C O''Brien" wrote:

    > I'm getting the same error and have tried all the suggestions from the
    > replies to this but to no avail. Here is my situation.
    >
    > Gridview on page, page contains a master page. I have tried removing the
    > MasterPage and the RenderControl still doesn't work.
    >
    > The following code
    > Dim stringWrite As New System.IO.StringWriter
    > Dim htmlWrite As New System.Web.UI.HtmlTextWriter(stringWrite)
    > grdQueryResults.RenderControl(htmlWrite)
    >
    > will yield this error:
    > Control 'grdQueryResults' of type 'GridView' must be placed inside a form
    > tag with runat=server
    >
    > I DEFINATELY have the GridView in a properly formed <Form> tag.
    > --------
    >
    > If I use the method described by faking the form tag I get this error:
    > RegisterForEventValidation can only be called during Render();
    > -----
    >
    > Also, if I use the method described on several other sites by adding an empty
    > Public Overrides Sub VerifyRenderingInServerForm
    > I get the same
    > RegisterForEventValidation can only be called during Render();
    > error
    >
    > Any thoughts on this as this is driving me CRAZY. The only alternative
    > which I find not really acceptable is to use a DataGrid when exporting is
    > required. The RenderControl method on the DataGrid works as expected.
    >
    > Thanks
    > -Chris
    >
    >
    > "Not Me" wrote:
    >
    > > Hi,
    > >
    > > I'm trying to export from a gridview control, to an excel file using
    > > code intended for a datagrid control (it's all over the web, can post if
    > > requested)
    > >
    > > I get the error.
    > >
    > > Control 'gvSearch' of type 'GridView' must be placed inside a form tag
    > > with runat=server.
    > >
    > > This error is similar to that experienced when the datagrid had
    > > server-side controls such as edit buttons and the like, but my gridview
    > > is very plain, just displaying text fields (and it is of course inside a
    > > form tag with runat=server).
    > >
    > > Are there any tweaks that could be made to allow the code to work? or is
    > > a completely different method the only way?
    > >
    > > Cheers,
    > > Chris
    > >
     
    =?Utf-8?B?UGF1bCBaYW5iYWth?=, Jan 26, 2006
    #10
  11. Not Me

    Guest

    I set allowsorting = false in my gridview and the export to excel works
    now, but I have to have sorting. Is there any other option?
     
    , Jan 31, 2006
    #11
  12. Not Me

    bene_1979 Guest

    wrote:
    > *I set allowsorting = false in my gridview and the export to excel
    > works
    > now, but I have to have sorting. Is there any other option? *



    Try to use 2 Grid views. One for the main page(with sorting enabled)
    and one for exporting to excel(without sorting).



    --
    bene_1979
    ------------------------------------------------------------------------
    Posted via http://www.codecomments.com
    ------------------------------------------------------------------------
     
    bene_1979, May 25, 2006
    #12
  13. Not Me

    AMVENIS

    Joined:
    Jun 6, 2007
    Messages:
    1
    Hi,

    I am also face the same problem... for this i just replace the Gridview with Datagrid...now it works fine... you also try the same...

    :tea: Cheers,
    Venis - MCP,MCAD

     
    Last edited: Jun 6, 2007
    AMVENIS, Jun 6, 2007
    #13
  14. Not Me

    darkwurm

    Joined:
    Jun 12, 2008
    Messages:
    1
    Adding the following code helped me


    Code:
       
    Public Overrides Sub VerifyRenderingInServerForm(ByVal control As System.Web.UI.Control)
    
        End Sub
    
    
     
    darkwurm, Jun 12, 2008
    #14
    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. Joy
    Replies:
    2
    Views:
    2,675
    Sabre
    Aug 22, 2003
  2. sanju
    Replies:
    2
    Views:
    6,285
    verda
    Feb 23, 2009
  3. =?Utf-8?B?U3JpZGhhcg==?=

    using Microsoft Excel image for Export to Excel button

    =?Utf-8?B?U3JpZGhhcg==?=, Dec 9, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    3,134
    =?Utf-8?B?U3JpZGhhcg==?=
    Dec 9, 2005
  4. =?Utf-8?B?SGVtYW50IFNpcGFoaW1hbGFuaQ==?=

    Export to Excel (Default File Type - Excel)

    =?Utf-8?B?SGVtYW50IFNpcGFoaW1hbGFuaQ==?=, Apr 24, 2006, in forum: ASP .Net
    Replies:
    15
    Views:
    17,579
    syed hassan
    May 21, 2009
  5. Grey
    Replies:
    4
    Views:
    2,041
    Mark Rae [MVP]
    Oct 17, 2007
Loading...

Share This Page