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