Master Page Gridview Export Excel

M

Madison

Hello,

I try to export to Excel from gridview using framework2 with master page.
Here is my coding
Response.Clear()
Response.AddHeader("content-disposition",
"attachment;filename=foe.xls")
Response.ContentType = "application/vnd.xls"
Response.Charset = ""
Me.EnableViewState = False
Dim sw As New System.IO.StringWriter
Dim htmlWriter As New HtmlTextWriter(sw)
myGrid.RenderControl(htmlWriter)
Response.Write(htmlWriter.ToString)
Response.End()
When I ran it, I got the message said "Control 'ctl00_BodyContent_myGrid' of
type 'GridView' must be placed inside a form tag with runat=server". I have
tried many search but nothing it works yet.
Any solution to this message.
Thanks.
 
W

Walter Wang [MSFT]

Hi,

This error is because by default the Page class is checking if the control
is put into a server-side form before rendering it in its
VerifyRenderingInServerForm() method. The workaround is to derive from Page
class and override this method to bypass the checking:

#GridViewGuy
http://www.gridviewguy.com/ArticleDetails.aspx?articleID=86

Hope this helps.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
M

Madison

Hi Walter,

Thank you for your reply. I have tried calling

Public Overrides Sub VerifyRenderingInServerForm(ByVal control As
System.Web.UI.Control)
MyBase.VerifyRenderingInServerForm(control)
End Sub

The article did not mention where should this overrides call and what I have
to pass with parametere. I just have the code and tried to run, I still get
the message say

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

and the error point to MyBase.VerifyRenderingInServerForm(control)

Thank you for your advise.
 
W

Walter Wang [MSFT]

Hi Madison,

Sorry for not clarifying the code previously.

You need to create a custom class that inherits from System.Web.UI.Page and
override this method in this custom class. Then you change your WebForm to
inherit from this custom class instead of System.Web.UI.Page, you can do
this in the code-behind of your WebForm.

Hope this helps.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
M

Madison

Hi Walter,

Thank you for your reply.
I end up with create the second gridview on code-behind (the display
gridview needs sorting and paging) and found the error with my coding. Here
is the way I did

Dim ds As DataTable = ViewState("report")
Dim sw As New StringWriter
Dim htw As New HtmlTextWriter(sw)
Dim dg As New GridView
dg.DataSource = ds
dg.DataBind()
dg.RenderControl(htw)

Response.Write(sw.ToString)
Response.End()

I'm not sure that using viewstate to hold dataset vs call stored procedure
to retrieve new dataset which one better.
 
W

Walter Wang [MSFT]

Hi Madison,

Thanks for your update. Looks like I had previously misunderstood your
question.

For question about holding dataset in ViewState, usually this is a
tradeoff. I think you just need to compare both approaches' pro and con and
decide which one is better according to your specific configuration:

In ViewState:
Pro: avoid repeatedly querying database
Con: increase page size and may cause performance issue if your users are
on a slow connection

Querying database everytime:
Pro: Page size is reduced and loads faster
Con: database server may become a bottleneck if it's already on high-load


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
E

Ettore Cefalà

To get a real XML or XLSX export to Excel you should try
http://www.gridviewtoexcel.com; it works also with very large number of
records (even though you can perform large export in HTML from your
server, it's really painful to open them with Excel); it can compress
the exported files to be and send them via email.

Regards

Ettore
 

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

Top