enrique_aeo
asked on
export to excel from datadrid - you can only call RegisterForEventValidation for render ()
i am using this code for export to excel from datagrid
Partial Class exportToExcel
Inherits System.Web.UI.Page
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
'// Disable paging
GridView1.AllowPaging = False
'// exluded columns arraylist
Dim defaultExcludedColumns As New ArrayList()
'// Always exclude these columns
defaultExcludedColumns.Add ("")
'// Send to base Excel export method
ExportGridView(GridView1, "Excel-Name", defaultExcludedColumns)
'// Rebind with paging enabled
GridView1.AllowPaging = True
End Sub
'/// <summary>
'/// Export GridView data to Excel.
'/// </summary>
'/// <param name="grdView">GridView control to export.</param>
'/// <param name="filename">Filename of excel spreadsheet.</param>
'/// <param name="excludedColumnList"> ArrayList of columns to exlude.</param>
Sub ExportGridView(ByVal grdView As GridView, ByVal filename As String, ByVal excludedColumnList As ArrayList)
'// Clear response content & headers
Response.Clear()
Response.ClearContent()
Response.ClearHeaders()
'// Add header
Response.AddHeader("conten t-disposit ion", "attachment;filename=" + filename + ".xls")
Response.Charset = String.Empty
Response.Cache.SetCacheabi lity(Syste m.Web.Http Cacheabili ty.Public)
Response.ContentType = "application/vnd.xls"
'// Create stringWriter
Dim stringWrite As New System.IO.StringWriter()
'// Create HtmlTextWriter
Dim htmlWrite As New HtmlTextWriter(stringWrite )
'// Remove controls from Column Headers
If (grdView.HeaderRow IsNot Nothing And grdView.HeaderRow.Cells IsNot Nothing) Then
Dim ct As Integer
For ct = 0 To grdView.HeaderRow.Cells.Co unt - 1 Step ct + 1
' Save initial text if found
Dim headerText As String = grdView.HeaderRow.Cells(ct ).Text
' Check for controls in header
If grdView.HeaderRow.Cells(ct ).HasContr ols() Then
' Check for link button
If grdView.HeaderRow.Cells(ct ).Controls (0).GetTyp e().ToStri ng() = "System.Web.UI.WebControls .DataContr olLinkButt on" Then
' link button found, get text
headerText = (CType(grdView.HeaderRow.C ells(ct).C ontrols(0) , LinkButton)).Text
End If
' Remove controls from header
grdView.HeaderRow.Cells(ct ).Controls .Clear()
End If
' Reassign header text
grdView.HeaderRow.Cells(ct ).Text = headerText
Next
End If
'// Remove footer
If (grdView.FooterRow IsNot DBNull.Value) Then
grdView.FooterRow.Visible = False
End If
'// Remove unwanted columns (header text listed in removeColumnList arraylist)
Dim field As DataControlField
For Each field In grdView.Columns
If (excludedColumnList.Contai ns(field.H eaderText) ) Then
field.Visible = False
End If
Next
'// Call gridview's renderControl
grdView.RenderControl(html Write)
'// Write Response to browser
Response.Write(stringWrite .ToString( ))
Response.End()
End Sub
Public Overrides Sub VerifyRenderingInServerFor m(ByVal control As System.Web.UI.Control)
'MyBase.VerifyRenderingInS erverForm( control)
End Sub
End Class
BUT I HAVE THIS ERROR:
you can only call RegisterForEventValidation for render ()
view attached please
i am working in vs2005 and vb.net
exportarERROR.jpg
Partial Class exportToExcel
Inherits System.Web.UI.Page
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
'// Disable paging
GridView1.AllowPaging = False
'// exluded columns arraylist
Dim defaultExcludedColumns As New ArrayList()
'// Always exclude these columns
defaultExcludedColumns.Add
'// Send to base Excel export method
ExportGridView(GridView1, "Excel-Name", defaultExcludedColumns)
'// Rebind with paging enabled
GridView1.AllowPaging = True
End Sub
'/// <summary>
'/// Export GridView data to Excel.
'/// </summary>
'/// <param name="grdView">GridView control to export.</param>
'/// <param name="filename">Filename of excel spreadsheet.</param>
'/// <param name="excludedColumnList">
Sub ExportGridView(ByVal grdView As GridView, ByVal filename As String, ByVal excludedColumnList As ArrayList)
'// Clear response content & headers
Response.Clear()
Response.ClearContent()
Response.ClearHeaders()
'// Add header
Response.AddHeader("conten
Response.Charset = String.Empty
Response.Cache.SetCacheabi
Response.ContentType = "application/vnd.xls"
'// Create stringWriter
Dim stringWrite As New System.IO.StringWriter()
'// Create HtmlTextWriter
Dim htmlWrite As New HtmlTextWriter(stringWrite
'// Remove controls from Column Headers
If (grdView.HeaderRow IsNot Nothing And grdView.HeaderRow.Cells IsNot Nothing) Then
Dim ct As Integer
For ct = 0 To grdView.HeaderRow.Cells.Co
' Save initial text if found
Dim headerText As String = grdView.HeaderRow.Cells(ct
' Check for controls in header
If grdView.HeaderRow.Cells(ct
' Check for link button
If grdView.HeaderRow.Cells(ct
' link button found, get text
headerText = (CType(grdView.HeaderRow.C
End If
' Remove controls from header
grdView.HeaderRow.Cells(ct
End If
' Reassign header text
grdView.HeaderRow.Cells(ct
Next
End If
'// Remove footer
If (grdView.FooterRow IsNot DBNull.Value) Then
grdView.FooterRow.Visible = False
End If
'// Remove unwanted columns (header text listed in removeColumnList arraylist)
Dim field As DataControlField
For Each field In grdView.Columns
If (excludedColumnList.Contai
field.Visible = False
End If
Next
'// Call gridview's renderControl
grdView.RenderControl(html
'// Write Response to browser
Response.Write(stringWrite
Response.End()
End Sub
Public Overrides Sub VerifyRenderingInServerFor
'MyBase.VerifyRenderingInS
End Sub
End Class
BUT I HAVE THIS ERROR:
you can only call RegisterForEventValidation
view attached please
i am working in vs2005 and vb.net
exportarERROR.jpg
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.