Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Export to Excel - ASP.net 2.0

Reply
Thread Tools

Export to Excel - ASP.net 2.0

 
 
Not Me
Guest
Posts: n/a
 
      11-15-2005
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
 
Reply With Quote
 
 
 
 
Cowboy \(Gregory A. Beamer\)
Guest
Posts: n/a
 
      11-15-2005
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" <(E-Mail Removed)> wrote in message
news:dlcius$ejv$(E-Mail Removed)...
> 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



 
Reply With Quote
 
 
 
 
Not Me
Guest
Posts: n/a
 
      11-15-2005
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
 
Reply With Quote
 
PL
Guest
Posts: n/a
 
      11-15-2005
> 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.


 
Reply With Quote
 
Not Me
Guest
Posts: n/a
 
      11-16-2005
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

 
Reply With Quote
 
=?Utf-8?B?SkdvcmRvbg==?=
Guest
Posts: n/a
 
      11-16-2005
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
>
>

 
Reply With Quote
 
=?Utf-8?B?SkdvcmRvbg==?=
Guest
Posts: n/a
 
      11-16-2005
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
>
>

 
Reply With Quote
 
Not Me
Guest
Posts: n/a
 
      11-17-2005
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


 
Reply With Quote
 
=?Utf-8?B?QyBPJydCcmllbg==?=
Guest
Posts: n/a
 
      11-29-2005
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
>

 
Reply With Quote
 
=?Utf-8?B?UGF1bCBaYW5iYWth?=
Guest
Posts: n/a
 
      01-26-2006
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
> >

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Export to Excel (Default File Type - Excel) =?Utf-8?B?SGVtYW50IFNpcGFoaW1hbGFuaQ==?= ASP .Net 15 05-21-2009 12:01 PM
Problem with Excel reports ::::Excel 2003 Migration To Excel 2007 =?Utf-8?B?c2hhc2hhbmsga3Vsa2Fybmk=?= ASP .Net 15 10-24-2007 01:34 PM
Export to excel in asp.net using excel template Grey ASP .Net 4 10-17-2007 08:08 AM
using Microsoft Excel image for Export to Excel button =?Utf-8?B?U3JpZGhhcg==?= ASP .Net 0 12-09-2005 08:58 PM
Invalid export DLL or export format =?Utf-8?B?RGF2aWQgVmFsbGU=?= ASP .Net 0 10-29-2003 11:46 AM



Advertisments