If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ASP > GridView output to file

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-06-06, 01:43
kscheidel kscheidel is offline
Registered User
 
Join Date: Mar 2004
Posts: 5
Question GridView output to file

FOUND SOLUTION: See reply!

I am trying to learn ASP and have developed a page with a GridView with paging, theme and sorting turned off. Although it is within the form tag on the "default.aspx" page, I still get the following error from "default.aspx.vb" page:

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

Source Error:
Line 30: oHtmlTextWriter = New System.Web.UI.HtmlTextWriter (oStringWriter)
Line 31: Me.Campus.RenderControl(oHtmlTextWriter)
Line 32: Response.Write(oStringWriter.ToString())

My code contains snippets from the web (trying to save just the Grid control to excel) but I get the full page if I rem Line 31 and the above error if I don't.

If someone would be kind enough to explain why this doesn't work, I would be most grateful and I thank you in advance. MKS

-------- Code --------
Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim oStringWriter As System.IO.StringWriter
Dim oHtmlTextWriter As System.Web.UI.HtmlTextWriter

Response.Clear()
Response.Buffer = True
Response.ContentType = "application/vnd.ms-excel"
'Response.AddHeader("Content-Disposition", "attachment filename=""" & filename & """")
Response.ContentEncoding = System.Text.Encoding.ASCII
Response.Charset = ""
EnableViewState = False
oStringWriter = New System.IO.StringWriter()
oHtmlTextWriter = New System.Web.UI.HtmlTextWriter(oStringWriter)
Me.Campus.RenderControl(oHtmlTextWriter)
Response.Write(oStringWriter.ToString())
End Sub

[Note: This attempt to get past the control issue gives me a Page error. I had placed it beneath the above sub. Why doesn't this work?]

' Override the Render method to ensure that this control
' is nested in an HtmlForm server control, between a <form runat=server>
' opening tag and a </form> closing tag.
Protected Overrides Sub Render(ByVal writer As HtmlTextWriter)

' Ensure that the control is nested in a server form.
If Not (Page Is Nothing) Then
Page.VerifyRenderingInServerForm(Me)
End If

MyBase.Render(writer)

End Sub

Last edited by kscheidel; 09-09-06 at 08:07.
Reply With Quote
  #2 (permalink)  
Old 09-09-06, 08:09
kscheidel kscheidel is offline
Registered User
 
Join Date: Mar 2004
Posts: 5
Lightbulb Here's what works in VisWebDev

Following line should be on Web.config page and only if it contains no user input controls. Also, this WILL impact entire site. There is another means of bypassing but I've only seen it in C# and haven't had time to translate to VB.

<pages enableEventValidation ="false" ></pages>


Now for the aspx code:

Protected Sub PrintButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)

Dim sw As System.IO.StringWriter
Dim htw As System.Web.UI.HtmlTextWriter

Response.ClearContent()
Response.AddHeader("Content-Disposition", "attachment; filename=Filename.xls")
Response.Charset = ""
Response.ContentType = "application/vnd.xls"

sw = New System.IO.StringWriter
htw = New System.Web.UI.HtmlTextWriter(sw)

GridViewName.RenderControl(htw)
Response.Write(sw.ToString())
Response.End()
End Sub

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

Last edited by kscheidel; 09-09-06 at 08:19.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 On