Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2004

    Question Unanswered: 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.Buffer = True
    Response.ContentType = "application/"
    '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)
    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
    End If


    End Sub
    Last edited by kscheidel; 09-09-06 at 09:07.

  2. #2
    Join Date
    Mar 2004

    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.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)

    End Sub

    Public Overrides Sub VerifyRenderingInServerForm(ByVal control As System.Web.UI.Control)
    End Sub
    Last edited by kscheidel; 09-09-06 at 09:19.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts