Asp.net VB.Net-通过排序将GridView导出到Excel
我的GridView已将Asp.net VB.Net-通过排序将GridView导出到Excel,asp.net,vb.net,gridview,webforms,Asp.net,Vb.net,Gridview,Webforms,我的GridView已将AllowSorting设置为True,现在我想添加一个导出到Excel函数 但是我得到了错误信息 RegisterForEventValidation can only be called during Render(); 只有将AllowSorting设置为False才能继续下载 我考虑使用代码将AllowSorting更改为False,导出到Excel,然后将其设置回True: Sub export() Response.Clear() Re
AllowSorting
设置为True
,现在我想添加一个导出到Excel函数
但是我得到了错误信息
RegisterForEventValidation can only be called during Render();
只有将AllowSorting
设置为False
才能继续下载
我考虑使用代码将AllowSorting
更改为False
,导出到Excel,然后将其设置回True
:
Sub export()
Response.Clear()
Response.Buffer = True
Response.AddHeader("content-disposition", "attachment;filename= Summary.xls")
Response.Charset = ""
Response.ContentType = "application/vnd.ms-excel"
Using sw As New StringWriter()
Dim hw As New HtmlTextWriter(sw)
'To Export all pages
GridView1.AllowPaging = False
GridView1.AllowSorting = False
GridView1.HeaderRow.BackColor = Color.White
For Each cell As TableCell In GridView1.HeaderRow.Cells
cell.BackColor = GridView1.HeaderStyle.BackColor
Next
For Each row As GridViewRow In GridView1.Rows
row.BackColor = Color.White
For Each cell As TableCell In row.Cells
If row.RowIndex Mod 2 = 0 Then
cell.BackColor = GridView1.AlternatingRowStyle.BackColor
Else
cell.BackColor = GridView1.RowStyle.BackColor
End If
cell.CssClass = "textmode"
Next
Next
GridView1.RenderControl(hw) 'source error for RegisterForEventValidation
'style to format numbers to string
Dim style As String = "<style> .textmode { } </style>"
Response.Write(style)
Response.Output.Write(sw.ToString())
Response.Flush()
Response.[End]()
GridView1.AllowSorting = True 'revert back to original state
End Using
End Sub
Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)
End Sub
子导出()
答复:Clear()
Response.Buffer=True
AddHeader(“内容处置”、“附件;文件名=Summary.xls”)
Response.Charset=“”
Response.ContentType=“应用程序/vnd.ms excel”
将sw用作新的StringWriter()
Dim hw作为新的HtmlTextWriter(软件)
'导出所有页面
GridView1.AllowPaging=False
GridView1.AllowSorting=False
GridView1.HeaderRow.BackColor=颜色.白色
将每个单元格作为GridView1.HeaderRow.Cells中的TableCell
cell.BackColor=GridView1.HeaderStyle.BackColor
下一个
作为GridView1.Rows中的GridViewRow的每一行
row.BackColor=Color.White
将每个单元格作为行中的TableCell。单元格
如果row.RowIndex Mod 2=0,则
cell.BackColor=GridView1.AlternatingRowStyle.BackColor
其他的
cell.BackColor=GridView1.RowStyle.BackColor
如果结束
cell.CssClass=“textmode”
下一个
下一个
GridView1.RenderControl(hw)注册表ForEventValidation的源错误
'将数字格式化为字符串的样式
Dim样式为字符串=“.textmode{}”
回应。写作(风格)
Response.Output.Write(sw.ToString())
Response.Flush()
答复:[完]()
GridView1.AllowSorting=True“还原回原始状态”
终端使用
端接头
Public Overrides Sub-VerifyRenderingInServerForm(ByVal控件作为控件)
端接头
但问题仍然存在。除了创建另一个不可见的GridView并从中导出外,我最初的想法是否可行?确保在页面加载事件中,控件的填充被包装在一个If IsPostBack=False然后是“code here to load grid etc”中。因此,在其他回发中,网格不会重新加载。