這問題困擾了我大概幾分鐘.....google後馬上迎刃而解
我頁面中有自動產生的 GridView n 個....我要按個按鈕轉成 excel
然後他給我出現 "必須置於有 runat=server 的表單標記之中"的錯誤訊息....><
解決方式如最下方藍字所示
程式片斷.......................
Protected Sub ButtonClick(ByVal sender As Object, ByVal e As System.EventArgs)
Dim name As String = "attachment;filename=test.xls"
Response.Clear()
Response.AddHeader("content-disposition", name)
Response.Charset = "UTF-8"
Response.ContentType = "application/vnd.xls"
Dim stringWrite As System.IO.StringWriter = New System.IO.StringWriter
Dim htmlWrite As System.Web.UI.HtmlTextWriter = New HtmlTextWriter(stringWrite)
For i As Integer = 1 To GD_Count - 1
Dim gd As GridView = Me.Page.FindControl("gd" & i)
gd.RenderControl(htmlWrite)
Next
Response.Write(stringWrite.ToString)
Response.End()
End Sub
'以下不加會出現 " 必須置於有 runat=server 的表單標記之中"的錯誤訊息
Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)
''覆寫,不執行 MyBase.VerifyRenderingInServerForm 方法,解決執行 RenderControl 產生的錯誤
End Sub
原來是因為 VerifyRenderingInServerForm 在檢查 runat="server"
這邊寫的很詳細
資料來源 : http://www.dotblogs.com.tw/jeff377/archive/2008/05/16/4006.aspx
留言列表