Export to Excel

P

Paul Zanbaka

I have this code below it works fine when exporting to excel.
However 2 things:
1-It does prompt me to save to file to excel (just displays excel in
browser)
2-It aonly works if AllowSorting="false' in <asp:GridView > tag

Can anyone help!
 
P

Paul Zanbaka

Sorry Here is the code below
Partial Class Excel

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)



Dim frm As HtmlForm = New HtmlForm()

Me.Controls.Add(frm)

frm.Controls.Add(GridView1)



frm.RenderControl(hw)



Response.Write(tw.ToString())

Response.End()



End Sub



End Class


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



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">



<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>Untitled Page</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:SipAppConnectionString %>"

SelectCommand="SELECT [DateImaged], [DeviceType] FROM [AllDevices]">

</asp:SqlDataSource>



<asp:GridView ID="GridView1" runat="server" DataSourceID="dsSearch"

BackColor="Silver" EmptyDataText="Sorry, your search has come up empty!">

</asp:GridView>

</div>

</form>

Code Behind Page
 
R

rbutare

I downloaded the code sample and it gives me the following error when
data is returned

RegisterForEventValidation can only be called during Render();
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException:
RegisterForEventValidation can only be called during Render();

Source Error:


Line 15: Me.Controls.Add(frm)
Line 16: frm.Controls.Add(GridView1)
Line 17: frm.RenderControl(hw)
Line 18: Response.Write(tw.ToString())
Line 19: Response.End()


Source File: C:\Documents and Settings\rbutare\My Documents\Visual
Studio 2005\WebSites\diversified\Excel.aspx.vb Line: 17


It works when no data is in grid? Any ideas what will be causing this
problem...

Thanks,

Rick
 
R

rbutare

I downloaded the code sample and it gives me the following error when
data is returned

RegisterForEventValidation can only be called during Render();
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException:
RegisterForEventValidation can only be called during Render();

Source Error:


Line 15: Me.Controls.Add(frm)
Line 16: frm.Controls.Add(GridView1)
Line 17: frm.RenderControl(hw)
Line 18: Response.Write(tw.ToString())
Line 19: Response.End()


Source File: C:\Documents and Settings\rbutare\My Documents\Visual
Studio 2005\WebSites\diversified\Excel.aspx.vb Line: 17


It works when no data is in grid? Any ideas what will be causing this
problem...

Thanks,

Rick
 
R

rbutare

I downloaded the code sample and it gives me the following error when
data is returned

RegisterForEventValidation can only be called during Render();
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException:
RegisterForEventValidation can only be called during Render();

Source Error:


Line 15: Me.Controls.Add(frm)
Line 16: frm.Controls.Add(GridView1)
Line 17: frm.RenderControl(hw)
Line 18: Response.Write(tw.ToString())
Line 19: Response.End()


Source File: C:\Documents and Settings\rbutare\My Documents\Visual
Studio 2005\WebSites\diversified\Excel.aspx.vb Line: 17


It works when no data is in grid? Any ideas what will be causing this
problem...

Thanks,

Rick
 
R

rbutare

I downloaded the code sample and it gives me the following error when
data is returned

RegisterForEventValidation can only be called during Render();
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException:
RegisterForEventValidation can only be called during Render();

Source Error:


Line 15: Me.Controls.Add(frm)
Line 16: frm.Controls.Add(GridView1)
Line 17: frm.RenderControl(hw)
Line 18: Response.Write(tw.ToString())
Line 19: Response.End()


Source File: C:\Documents and Settings\rbutare\My Documents\Visual
Studio 2005\WebSites\diversified\Excel.aspx.vb Line: 17


It works when no data is in grid? Any ideas what will be causing this
problem...

Thanks,

Rick
 
R

rbutare

I downloaded the code sample and it gives me the following error when
data is returned

RegisterForEventValidation can only be called during Render();
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException:
RegisterForEventValidation can only be called during Render();

Source Error:


Line 15: Me.Controls.Add(frm)
Line 16: frm.Controls.Add(GridView1)
Line 17: frm.RenderControl(hw)
Line 18: Response.Write(tw.ToString())
Line 19: Response.End()


Source File: C:\Documents and Settings\rbutare\My Documents\Visual
Studio 2005\WebSites\diversified\Excel.aspx.vb Line: 17


It works when no data is in grid? Any ideas what will be causing this
problem...

Thanks,

Rick
 
Joined
Oct 21, 2008
Messages
1
Reaction score
0
RegisterForEventValidation can only be called during Render();

Rick, I am having this same issue. Did you ever figure out how to fix it?

Nic
 
Joined
Mar 2, 2009
Messages
1
Reaction score
0
The problem is cause by Excel working with True/False values what i did was format the access True/False field in the query. basically Format(test.client, 'Yes/No') as [Existing Customer]
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
anuragag27

Latest Threads

Top