Link to home
Start Free TrialLog in
Avatar of kdeutsch
kdeutschFlag for United States of America

asked on

Export gridview with hiddenfield

I am trying to export a girdview with a hiddenfield that is embedded intot a templatefield to check agianst its values, but for some reason it will not let me add it to my control remove group so that it exports .  My Page is a sub page of a master site so all the form tags are on the master not on this page.

Error
Control 'HFLateMob_0' of type 'HiddenField' must be placed inside a form tag with runat=server.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: Control 'HFLateMob_0' of type 'HiddenField' must be placed inside a form tag with runat=server.

Source Error:


Line 236:
Line 237:                ' render the table into the htmlwriter
Line 238:                table.RenderControl(htw)
Line 239:                HttpContext.Current.Response.Write(style)
Line 240:                HttpContext.Current.Response.Write(sw.ToString())
 




Protected Sub lnkExport_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkExport.Click
        Export("myExcelFIle.xls", Me.myGridView)
    End Sub

    Public Shared Sub Export(ByVal fileName As String, ByVal gv As GridView)
        Dim style As String = "<style> .text { mso-number-format:\@; } </style> "
        HttpContext.Current.Response.Clear()
        HttpContext.Current.Response.AddHeader("content-disposition", String.Format("attachment; filename={0}", fileName))
        HttpContext.Current.Response.ContentType = "application/ms-excel"

        Using sw As New StringWriter()
            Using htw As New HtmlTextWriter(sw)
                ' Create a form to contain the grid
                Dim table As New Table()

                ' add the header row to the table
                If gv.HeaderRow IsNot Nothing Then
                    PrepareControlForExport(gv.HeaderRow)
                    table.Rows.Add(gv.HeaderRow)
                End If

                ' add each of the data rows to the table
                For Each row As GridViewRow In gv.Rows
                    PrepareControlForExport(row)
                    table.Rows.Add(row)
                Next

                ' add the footer row to the table
                If gv.FooterRow IsNot Nothing Then
                    PrepareControlForExport(gv.FooterRow)
                    table.Rows.Add(gv.FooterRow)
                End If

                ' render the table into the htmlwriter
                table.RenderControl(htw)
                HttpContext.Current.Response.Write(style)
                HttpContext.Current.Response.Write(sw.ToString())
                HttpContext.Current.Response.[End]()
            End Using
        End Using
    End Sub

    Private Shared Sub PrepareControlForExport(ByVal control As Control)
        For i As Integer = 0 To control.Controls.Count - 1
            Dim current As Control = control.Controls(i)
            If TypeOf current Is Label Then
                control.Controls.Remove(current)
                control.Controls.AddAt(i, New LiteralControl(TryCast(current, Label).Text))
            End If
            If TypeOf current Is LinkButton Then
                control.Controls.Remove(current)
                control.Controls.AddAt(i, New LiteralControl(TryCast(current, LinkButton).Text))
            ElseIf TypeOf current Is ImageButton Then
                control.Controls.Remove(current)
                control.Controls.AddAt(i, New LiteralControl(TryCast(current, ImageButton).AlternateText))
            ElseIf TypeOf current Is HyperLink Then
                control.Controls.Remove(current)
                control.Controls.AddAt(i, New LiteralControl(TryCast(current, HyperLink).Text))
            ElseIf TypeOf current Is DropDownList Then
                control.Controls.Remove(current)
                control.Controls.AddAt(i, New LiteralControl(TryCast(current, DropDownList).SelectedItem.Text))
            ElseIf TypeOf current Is CheckBox Then
                control.Controls.Remove(current)
                control.Controls.AddAt(i, New LiteralControl(If(TryCast(current, CheckBox).Checked, "True", "False")))
                'ElseIf TypeOf current Is HiddenField Then
                '    control.Controls.Remove(current)
                '    control.Controls.AddAt(i, New LiteralControl(If(TryCast(current, HiddenField).Value.Remove))
            End If

            If current.HasControls() Then
                PrepareControlForExport(current)
            End If
        Next
    End Sub

Open in new window

Avatar of Tom Beck
Tom Beck
Flag of United States of America image

Just to verify your situation...

>>all the form tags are on the master not on this page

..AND the content page is being pasted to a position in the markup that is inside the form tag on the master page
..AND the form tag has a runat="server" attribute.
Avatar of kdeutsch

ASKER

tommyBoy:
That is correct, it is just confounding how many problems I am running into with this.
ASKER CERTIFIED SOLUTION
Avatar of kdeutsch
kdeutsch
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
No response and no other offered solutions, eventually did the hard way.