Please, help me resolve this error.

drewgeezmoe

Member
Joined
Feb 8, 2007
Messages
9
Programming Experience
Beginner
I have this code to export datas in the gridview to excel.
There is always an error when I click the button to export it
in excel.

It always point out to my dg.RenderControl(oHtmlTextWriter)
and it has this message:

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

Here is my codebehind:

VB.NET:
Imports System.Data.Sql
Imports System.Data.SqlClient
Partial Public Class _Default
    Inherits System.Web.UI.Page


    Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim conn As SqlConnection = New SqlConnection("data source=CDCSQL;initial=aT;Pwd=a;User ID=sa")
            Dim cmd As SqlCommand = New SqlCommand("Select ProductName, countryCode from tbl_Product", conn)
            Dim da As SqlDataAdapter = New SqlDataAdapter(cmd)
            Dim ds As DataSet = New DataSet
            da.Fill(ds)
            dg.DataSource = ds.Tables(0)
            dg.DataBind()
        End If
    End Sub


    Private Sub ClearControls(ByVal control As Control)
        Dim i As Integer = control.Controls.Count - 1
        While i >= 0
            ClearControls(control.Controls(i))
            System.Math.Max(System.Threading.Interlocked.Decrement(i), i + 1)
        End While
        If Not (TypeOf control Is TableCell) Then
            If Not (control.GetType.GetProperty("SelectedItem") Is Nothing) Then
                Dim literal As LiteralControl = New LiteralControl
                control.Parent.Controls.Add(literal)
                Try
                    literal.Text = CType(control.GetType.GetProperty("SelectedItem").GetValue(control, Nothing), String)
                Catch
                End Try
                control.Parent.Controls.Remove(control)
            Else
                If Not (control.GetType.GetProperty("Text") Is Nothing) Then
                    Dim literal As LiteralControl = New LiteralControl
                    control.Parent.Controls.Add(literal)
                    literal.Text = CType(control.GetType.GetProperty("Text").GetValue(control, Nothing), String)
                    control.Parent.Controls.Remove(control)
                End If
            End If
        End If
        Return
    End Sub




    Protected Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Response.Clear()
        Response.Buffer = True
        Response.ContentType = "application/vnd.ms-excel"
        Response.Charset = ""
        Me.EnableViewState = False
        Dim oStringWriter As System.IO.StringWriter = New System.IO.StringWriter
        Dim oHtmlTextWriter As System.Web.UI.HtmlTextWriter = New System.Web.UI.HtmlTextWriter(oStringWriter)
        Me.ClearControls(dg)
        dg.RenderControl(oHtmlTextWriter)
        Response.Write(oStringWriter.ToString)
        Response.End()
    End Sub

    Protected Sub dg_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dg.SelectedIndexChanged

    End Sub
End Class
and here is my aspx code
VB.NET:
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="WebApplication4._Default" %>

<!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:Panel ID="Panel1" runat="server" Height="470px" Width="507px">
            <asp:Button ID="Button1" runat="server" Style="left: 34px; position: static; top: 451px"
                Text="Button" />
            <asp:GridView ID="dg" runat="server" Height="366px" Style="position: static"
                Width="507px">
            </asp:GridView>
        </asp:Panel>
    
    </div>
    </form>
</body>
</html>
Thank you in advance for the help
 
I got it thank you

I just put this code as the last function in the codebehind using vb

VB.NET:
[SIZE=2]Public Overrides Sub VerifyRenderingInServerForm(ByVal control As System.Web.UI.Control) [/SIZE]
[SIZE=2]End Sub [/SIZE]
 
Back
Top