[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Export gridview with hiddenfield

Posted on 2011-10-17
4
Medium Priority
?
740 Views
Last Modified: 2012-05-12
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
Comment
Question by:kdeutsch
  • 3
4 Comments
 
LVL 38

Expert Comment

by:Tom Beck
ID: 36981247
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
 

Author Comment

by:kdeutsch
ID: 36981701
tommyBoy:
That is correct, it is just confounding how many problems I am running into with this.
0
 

Accepted Solution

by:
kdeutsch earned 0 total points
ID: 36987050
Solution to problem was to send datagrid to antoher page with form tags and put export on this page without an update panel
0
 

Author Closing Comment

by:kdeutsch
ID: 37013541
No response and no other offered solutions, eventually did the hard way.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

868 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question