2010-02-25 83 views
1

我有一个gridvidew(GV2)。我希望用户能够将此GridView的内容导出到Excel电子表格以供离线处理。将gridview内容导出为ex​​cel电子表格

这里是我的子程序:

Protected Sub ExcelButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ExcelButton.Click 
     Response.ContentType = "application/vnd.ms-excel" 
     Response.Charset = "" 
     Me.EnableViewState = False 
     Dim stringWriter As New System.IO.StringWriter() 
     Dim htmlWriter As New System.Web.UI.HtmlTextWriter(stringWriter) 
     GV2.RenderControl(htmlWriter) 
     Response.Write(stringWriter.ToString()) 
     Response.End() 
    End Sub 

在点击ExcelButton我得到的错误消息:类型的GridView'的

控制“GV2”必须放在一个表单标签内有RUNAT =服务器。

控制GV2其实里面:

<form id="form1" runat="server"></form> 

回答

2

有一个 'story' 这背后的错误消息。我会直接找到其中一个解决方案。

一下添加到ASPX文件的代码隐藏:

Public Overrides Sub VerifyRenderingInServerForm(control As Control) 

End Sub 

此处了解详情:

How to export GridView to Word using ASP.NET 2.0 and VB
CodeSnip: Exporting GridView to Excel

+0

感谢您的帮助。我添加了上面的代码,现在得到:RegisterForEventValidation只能在Render()中调用; – Phil 2010-02-25 10:56:25

+0

我通过关闭事件验证来解决这个问题.....通过在@page指令中添加EnableEventValidation =“false”...这是否存在潜在的危险? – Phil 2010-02-25 11:07:42

+0

@Phil,我假设你只关闭该特定页面的事件验证,则需要确保该页面的回发事件不会被劫持以执行非法或不需要的操作。 – 2010-02-25 11:52:09