• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 766
  • Last Modified:

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

0
kdeutsch
Asked:
kdeutsch
  • 3
1 Solution
 
Tom BeckCommented:
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.
0
 
kdeutschAuthor Commented:
tommyBoy:
That is correct, it is just confounding how many problems I am running into with this.
0
 
kdeutschAuthor Commented:
Solution to problem was to send datagrid to antoher page with form tags and put export on this page without an update panel
0
 
kdeutschAuthor Commented:
No response and no other offered solutions, eventually did the hard way.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now