i have an error at click button export to PDF, Export to DOC, Export to XLS
Server Error in '/' Application.
Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Web.HttpException: Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server.
Source Error:
Line 97: GridView1.AllowPaging = False
Line 98: GridView1.DataBind()
Line 99: GridView1.RenderControl(hw)
Line 100: Dim sr As New StringReader(sw.ToString())
Line 101: Dim pdfDoc As New Document(PageSize.A4, 10.0F, 10.0F, 10.0F, 0.0F)
|
<asp:GridView runat="server" ID="GridView1" AutoGenerateColumns="False"
DataKeyNames="KdKryawan" DataSourceID="SDSkaryawan">
<Columns>
<asp:BoundField DataField="KdKryawan" HeaderText="KdKryawan"
InsertVisible="False" ReadOnly="True" SortExpression="KdKryawan" />
<asp:BoundField DataField="NIKKaryawan" HeaderText="NIKKaryawan"
SortExpression="NIKKaryawan" />
<asp:BoundField DataField="NamaKaryawan" HeaderText="NamaKaryawan"
SortExpression="NamaKaryawan" />
<asp:BoundField DataField="NPWPKaryawan" HeaderText="NPWPKaryawan"
SortExpression="NPWPKaryawan" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SDSkaryawan" runat="server"
ConnectionString="<%$ ConnectionStrings:Conn %>"
SelectCommand="SELECT [KdKryawan], [NIKKaryawan], [NamaKaryawan], [NPWPKaryawan] FROM [Karyawan]">
</asp:SqlDataSource>
<asp:Button ID="eksword" runat="server" Text="Ekspor ke Word" />
<asp:Button ID="eksExcel" runat="server" Text="Ekspor ke Excel" />
<asp:Button ID="ekspdf" runat="server" Text="Ekspor ke PDF" />
<asp:Button ID="ekscsv" runat="server" Text="Ekspor ke CSV" />
Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports iTextSharp.text.html
Imports iTextSharp.text.html.simpleparser
Imports System.Text
Public Class Render
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Private Sub eksword_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles eksword.Click
ExportWord()
End Sub
Protected Sub ExportWord()
Response.Clear()
Response.Buffer = True
Response.AddHeader("content-disposition",
"attachment;filename=GridViewExport.doc")
Response.Charset = ""
Response.ContentType = "application/vnd.ms-word "
Dim sw As New StringWriter()
Dim hw As New HtmlTextWriter(sw)
GridView1.AllowPaging = False
GridView1.DataBind()
GridView1.RenderControl(hw)
Response.Output.Write(sw.ToString())
Response.Flush()
Response.End()
End Sub
Private Sub eksExcel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles eksExcel.Click
Response.Clear()
Response.Buffer = True
Response.AddHeader("content-disposition",
"attachment;filename=GridViewExport.xls")
Response.Charset = ""
Response.ContentType = "application/vnd.ms-excel"
Dim sw As New StringWriter()
Dim hw As New HtmlTextWriter(sw)
GridView1.AllowPaging = False
GridView1.DataBind()
'Change the Header Row back to white color
GridView1.HeaderRow.Style.Add("background-color", "#FFFFFF")
'Apply style to Individual Cells
GridView1.HeaderRow.Cells(0).Style.Add("background-color", "green")
GridView1.HeaderRow.Cells(1).Style.Add("background-color", "green")
GridView1.HeaderRow.Cells(2).Style.Add("background-color", "green")
GridView1.HeaderRow.Cells(3).Style.Add("background-color", "green")
For i As Integer = 0 To GridView1.Rows.Count - 1
Dim row As GridViewRow = GridView1.Rows(i)
'Change Color back to white
row.BackColor = System.Drawing.Color.White
'Apply text style to each Row
row.Attributes.Add("class", "textmode")
'Apply style to Individual Cells of Alternating Row
If i Mod 2 <> 0 Then
row.Cells(0).Style.Add("background-color", "#C2D69B")
row.Cells(1).Style.Add("background-color", "#C2D69B")
row.Cells(2).Style.Add("background-color", "#C2D69B")
row.Cells(3).Style.Add("background-color", "#C2D69B")
End If
Next
GridView1.RenderControl(hw)
'style to format numbers to string
Dim style As String = "<style>.textmode{mso-number-format:\@;}</style>"
Response.Write(style)
Response.Output.Write(sw.ToString())
Response.Flush()
Response.End()
End Sub
Private Sub ekspdf_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ekspdf.Click
Response.Clear()
Response.Buffer = True
Response.ContentType = "application/pdf"
Response.AddHeader("content-disposition",
"attachment;filename=GridViewExport.pdf")
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Dim sw As New StringWriter()
Dim hw As New HtmlTextWriter(sw)
GridView1.AllowPaging = False
GridView1.DataBind()
GridView1.RenderControl(hw)
Dim sr As New StringReader(sw.ToString())
Dim pdfDoc As New Document(PageSize.A4, 10.0F, 10.0F, 10.0F, 0.0F)
Dim htmlparser As New HTMLWorker(pdfDoc)
PdfWriter.GetInstance(pdfDoc, Response.OutputStream)
pdfDoc.Open()
htmlparser.Parse(sr)
pdfDoc.Close()
Response.Write(pdfDoc)
Response.End()
End Sub
Private Sub ekscsv_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ekscsv.Click
Response.Clear()
Response.Buffer = True
Response.AddHeader("content-disposition",
"attachment;filename=GridViewExport.csv")
Response.Charset = ""
Response.ContentType = "application/text"
GridView1.AllowPaging = False
GridView1.DataBind()
Dim sb As New StringBuilder()
For k As Integer = 0 To GridView1.Columns.Count - 1
'add separator
sb.Append(GridView1.Columns(k).HeaderText + ","c)
Next
'append new line
sb.Append(vbCr & vbLf)
For i As Integer = 0 To GridView1.Rows.Count - 1
For k As Integer = 0 To GridView1.Columns.Count - 1
'add separator
sb.Append(GridView1.Rows(i).Cells(k).Text + ","c)
Next
'append new line
sb.Append(vbCr & vbLf)
Next
Response.Output.Write(sb.ToString())
Response.Flush()
Response.End()
End Sub
End Class