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

Export to excel

When I do an export to excel it's fine....unless the gridview has a field that is a checkbox....then i get this error

Control 'GridView1_ctl18_ctl01' of type 'CheckBox' must be placed inside a form tag with runat=server.

The column in question, IsCenter, is a bit datatype....could that be the issue?

If so is there a way to fix this using sql or is there a way in the code?

Thx
Protected Sub btnExcel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExcel.Click
 
        Dim hf As New HtmlForm()
        Dim GridView3 As GridView = GridView1
        hf.Controls.Add(GridView3)
 
        Response.Clear()
        Response.Buffer = True
        Response.ContentType = "application/vnd.ms-excel"
        Response.Charset = ""
        EnableViewState = False
        Dim stwWriter As New System.IO.StringWriter
        Dim htwHtmlTextWriter As New System.Web.UI.HtmlTextWriter(stwWriter)
        GridView1.RenderControl(htwHtmlTextWriter)
 
        Response.Write(stwWriter.ToString())
        Response.End()
 
    End Sub

Open in new window

0
bdb12
Asked:
bdb12
  • 6
  • 6
1 Solution
 
GreymanMSCCommented:
Render the Form which contains the GridView to the TextWriter, not the GridView directly.
Protected Sub btnExcel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExcel.Click
 
        Dim hf As New HtmlForm()
        Dim GridView3 As GridView = GridView1
        hf.Controls.Add(GridView3)
 
        Response.Clear()
        Response.Buffer = True
        Response.ContentType = "application/vnd.ms-excel"
        Response.Charset = ""
        EnableViewState = False
        Dim stwWriter As New System.IO.StringWriter
        Dim htwHtmlTextWriter As New System.Web.UI.HtmlTextWriter(stwWriter)
        hf.RenderControl(htwHtmlTextWriter)
 
        Response.Write(stwWriter.ToString())
        Response.End()
 
    End Sub

Open in new window

0
 
GreymanMSCCommented:
PS: Line 14 holds the change.
0
 
bdb12Author Commented:
thx for your reply. I put that change in and get the following error

"HtmlForm cannot render without a reference to the Page instance.  Make sure your form has been added to the control tree."
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
GreymanMSCCommented:
Right, you've got to add the Form to the Page before you try to use Response.  Missed that.  Add the following at line 6:
   Me.Page.Controls.Add(hf)

Open in new window

0
 
bdb12Author Commented:
yeah I tried doing that as well, but then it gives this error

RegisterForEventValidation can only be called during Render();

on line 14

 hf.RenderControl(htwHtmlTextWriter)

Any ideas?
0
 
GreymanMSCCommented:
Rendering...  

Ah.  You are trying to rerender the page in a Button click event.  That won't work, you need to have such code in the Page_Load event.   I suggest you place the code, and the GridView, in the Page_Load event of a seperate page, then use the button on the original page to redirect to it.

0
 
bdb12Author Commented:
Ok thx I'll try it out and get back to you
0
 
GreymanMSCCommented:
Got to go now.  I'll check your progress tomorrow.
0
 
bdb12Author Commented:
Hmmm Im struggling to get it to work....

The button click function now redirects to a new page

   Protected Sub btnExcel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExcel.Click

        Response.Redirect("Default2.aspx")

    End Sub

On the new page there is a gridviewthat is the same as the original....

The page load event on the new page is below.


The thing is the
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim hf As New HtmlForm()
        Dim GridView3 As GridView = GridView1
        hf.Controls.Add(GridView3)
        Me.Page.Controls.Add(hf)
 
        Response.Clear()
        Response.Buffer = True
        Response.ContentType = "application/vnd.ms-excel"
        Response.Charset = ""
        EnableViewState = False
        Dim stwWriter As New System.IO.StringWriter
        Dim htwHtmlTextWriter As New System.Web.UI.HtmlTextWriter(stwWriter)
        hf.RenderControl(htwHtmlTextWriter)
 
        Response.Write(stwWriter.ToString())
        Response.End()
    End Sub

Open in new window

0
 
bdb12Author Commented:
Sorry....the thing is that it crashes on line 14 again, with the same error

RegisterForEventValidation can only be called during Render();

Thx for helping me out
0
 
GreymanMSCCommented:
I have nearly identical code that works fine so... I am at a loss.
Imports SysData = System.Data
Imports SysWebUI = System.Web.UI
Imports SysWebCon = System.Web.UI.WebControls
 
Partial Class Reports_ExportToExcel
    Inherits SysWebUI.Page
 
    Private WithEvents GV As New SysWebCon.GridView
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim oTab As SysData.DataTable = Session.Item("ExportToExcelTable")
 
        GV.DataSource = oTab
        GV.AutoGenerateColumns = True
        GV.DataBind()
 
        Dim tw As New System.IO.StringWriter()
        Dim hw As New SysWebUI.HtmlTextWriter(tw)
        Dim frm As HtmlForm = New HtmlForm
        Controls.Add(frm)
        frm.Controls.Add(GV)
        frm.RenderControl(hw)
 
        Response.Clear()
        Response.AddHeader("Content-Disposition", "inline;filename=Report.xls")
        Response.ContentType = "application/vnd.ms-excel"
        Response.Charset = ""
        Dim s As String = tw.ToString()
        If s.Contains("<table") Then
            s = s.Substring(s.IndexOf("<table"), s.IndexOf("</table>") - s.IndexOf("<table") + 8)
        End If
        Response.Write(s)
        Response.End()
    End Sub
 
End Class

Open in new window

0
 
bdb12Author Commented:
Got it!....thx for walking me through
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

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