Cita:
Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
Imports System.Collections
Imports System.Text
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports iTextSharp.text.html.simpleparser
Partial Class ExportAll
Inherits System.Web.UI.Page
Private con As New SqlConnection(ConfigurationManager.ConnectionStrin gs("conString").ToString())
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
BindGridData()
End If
End Sub
Private Sub BindGridData()
Dim da As New SqlDataAdapter("select CategoryID,CategoryName,Description from categories", con)
Dim dt As New DataTable
da.Fill(dt)
If dt.Rows.Count > 0 Then
GridView1.DataSource = dt
GridView1.DataBind()
Else
dt.Rows.Add(dt.NewRow())
GridView1.DataSource = dt
GridView1.DataBind()
Dim columnCount As Integer = GridView1.Rows(0).Cells.Count
GridView1.Rows(0).Cells.Clear()
GridView1.Rows(0).Cells.Add(New TableCell())
GridView1.Rows(0).Cells(0).ColumnSpan = columnCount
GridView1.Rows(0).Cells(0).Text = "<font color=Red><b><center>No Data Found !</center></b></font>"
End If
End Sub
Public Overrides Sub VerifyRenderingInServerForm(ByVal control As System.Web.UI.Control)
'MyBase.VerifyRenderingInServerForm(control)
End Sub
Private Sub SaveCheckedValues()
Dim userdetails As New ArrayList
Dim index As Integer = -1
For Each gvrow As GridViewRow In GridView1.Rows
index = CInt(GridView1.DataKeys(gvrow.RowIndex).Value)
Dim result As Boolean = DirectCast(gvrow.FindControl("chkSelect"), CheckBox).Checked
If ViewState("CHECKED_ITEMS") IsNot Nothing Then
userdetails = DirectCast(ViewState("CHECKED_ITEMS"), ArrayList)
End If
If result Then
If Not userdetails.Contains(index) Then
userdetails.Add(index)
End If
Else
userdetails.Remove(index)
End If
Next
If userdetails IsNot Nothing AndAlso userdetails.Count > 0 Then
ViewState("CHECKED_ITEMS") = userdetails
End If
End Sub
Private Sub PopulateCheckedValues()
Dim userdetails As ArrayList = DirectCast(ViewState("CHECKED_ITEMS"), ArrayList)
If userdetails IsNot Nothing AndAlso userdetails.Count > 0 Then
For Each gvrow As GridViewRow In GridView1.Rows
Dim index As Integer = CInt(GridView1.DataKeys(gvrow.RowIndex).Value)
If userdetails.Contains(index) Then
Dim myCheckBox As CheckBox = DirectCast(gvrow.FindControl("chkSelect"), CheckBox)
myCheckBox.Checked = True
End If
Next
End If
End Sub
Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging
SaveCheckedValues()
GridView1.PageIndex = e.NewPageIndex
End Sub
Protected Sub btnPDF_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPDF.Click
Try
SaveCheckedValues()
Response.Clear()
Response.Buffer = True
Response.AddHeader("content-disposition", String.Format("attachment;filename=StudentDetail’s .pdf"))
Response.Cache.SetCacheability(HttpCacheability.No Cache)
Response.Charset = ""
Response.ContentType = "application/pdf"
Dim sw As New StringWriter()
Dim hw As New HtmlTextWriter(sw)
GridView1.AllowPaging = False
BindGridData()
GridView1.HeaderRow.Style.Add("background-color", "#FFFFFF")
GridView1.HeaderRow.Cells(0).Visible = False
For Each cell As TableCell In GridView1.HeaderRow.Cells
cell.BackColor = GridView1.HeaderStyle.BackColor
Next
If ViewState("CHECKED_ITEMS") IsNot Nothing Then
Dim CheckBoxArray As ArrayList = DirectCast(ViewState("CHECKED_ITEMS"), ArrayList)
Dim rowIdx As Integer = 0
For i As Integer = 0 To GridView1.Rows.Count - 1
Dim row As GridViewRow = GridView1.Rows(i)
row.Visible = False
Dim index As Integer = CInt(GridView1.DataKeys(row.RowIndex).Value)
If CheckBoxArray.Contains(index) Then
row.Visible = True
row.Cells(0).Visible = False
End If
rowIdx += 1
Next
End If
For Each row As GridViewRow In GridView1.Rows
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"
Dim controls As New List(Of Control)()
For Each control As Control In cell.Controls
controls.Add(control)
Next
For Each control As Control In controls
Select Case (control.[GetType]().Name)
Case "CheckBox"
cell.Controls.Add(New Literal() With { _
.Text = TryCast(control, CheckBox).Text _
})
cell.Visible = False
Exit Select
End Select
cell.Controls.Remove(control)
Next
Next
Next
GridView1.RenderControl(hw)
Dim style As String = "<style> .textmode { } </style>"
Response.Write(style)
Dim sr As New StringReader(sw.ToString())
Dim PDF As New Document(PageSize.A4, 10.0F, 10.0F, 100.0F, 10.0F)
Dim htmlparser As New HTMLWorker(PDF)
PdfWriter.GetInstance(PDF, Response.OutputStream)
PDF.Open()
htmlparser.Parse(sr)
PDF.Close()
Response.Output.Write(sw.ToString())
Response.[End]()
Catch ex As Exception
End Try
End Sub
End Class
Imports System.Data.SqlClient
Imports System.IO
Imports System.Collections
Imports System.Text
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports iTextSharp.text.html.simpleparser
Partial Class ExportAll
Inherits System.Web.UI.Page
Private con As New SqlConnection(ConfigurationManager.ConnectionStrin gs("conString").ToString())
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
BindGridData()
End If
End Sub
Private Sub BindGridData()
Dim da As New SqlDataAdapter("select CategoryID,CategoryName,Description from categories", con)
Dim dt As New DataTable
da.Fill(dt)
If dt.Rows.Count > 0 Then
GridView1.DataSource = dt
GridView1.DataBind()
Else
dt.Rows.Add(dt.NewRow())
GridView1.DataSource = dt
GridView1.DataBind()
Dim columnCount As Integer = GridView1.Rows(0).Cells.Count
GridView1.Rows(0).Cells.Clear()
GridView1.Rows(0).Cells.Add(New TableCell())
GridView1.Rows(0).Cells(0).ColumnSpan = columnCount
GridView1.Rows(0).Cells(0).Text = "<font color=Red><b><center>No Data Found !</center></b></font>"
End If
End Sub
Public Overrides Sub VerifyRenderingInServerForm(ByVal control As System.Web.UI.Control)
'MyBase.VerifyRenderingInServerForm(control)
End Sub
Private Sub SaveCheckedValues()
Dim userdetails As New ArrayList
Dim index As Integer = -1
For Each gvrow As GridViewRow In GridView1.Rows
index = CInt(GridView1.DataKeys(gvrow.RowIndex).Value)
Dim result As Boolean = DirectCast(gvrow.FindControl("chkSelect"), CheckBox).Checked
If ViewState("CHECKED_ITEMS") IsNot Nothing Then
userdetails = DirectCast(ViewState("CHECKED_ITEMS"), ArrayList)
End If
If result Then
If Not userdetails.Contains(index) Then
userdetails.Add(index)
End If
Else
userdetails.Remove(index)
End If
Next
If userdetails IsNot Nothing AndAlso userdetails.Count > 0 Then
ViewState("CHECKED_ITEMS") = userdetails
End If
End Sub
Private Sub PopulateCheckedValues()
Dim userdetails As ArrayList = DirectCast(ViewState("CHECKED_ITEMS"), ArrayList)
If userdetails IsNot Nothing AndAlso userdetails.Count > 0 Then
For Each gvrow As GridViewRow In GridView1.Rows
Dim index As Integer = CInt(GridView1.DataKeys(gvrow.RowIndex).Value)
If userdetails.Contains(index) Then
Dim myCheckBox As CheckBox = DirectCast(gvrow.FindControl("chkSelect"), CheckBox)
myCheckBox.Checked = True
End If
Next
End If
End Sub
Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging
SaveCheckedValues()
GridView1.PageIndex = e.NewPageIndex
End Sub
Protected Sub btnPDF_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPDF.Click
Try
SaveCheckedValues()
Response.Clear()
Response.Buffer = True
Response.AddHeader("content-disposition", String.Format("attachment;filename=StudentDetail’s .pdf"))
Response.Cache.SetCacheability(HttpCacheability.No Cache)
Response.Charset = ""
Response.ContentType = "application/pdf"
Dim sw As New StringWriter()
Dim hw As New HtmlTextWriter(sw)
GridView1.AllowPaging = False
BindGridData()
GridView1.HeaderRow.Style.Add("background-color", "#FFFFFF")
GridView1.HeaderRow.Cells(0).Visible = False
For Each cell As TableCell In GridView1.HeaderRow.Cells
cell.BackColor = GridView1.HeaderStyle.BackColor
Next
If ViewState("CHECKED_ITEMS") IsNot Nothing Then
Dim CheckBoxArray As ArrayList = DirectCast(ViewState("CHECKED_ITEMS"), ArrayList)
Dim rowIdx As Integer = 0
For i As Integer = 0 To GridView1.Rows.Count - 1
Dim row As GridViewRow = GridView1.Rows(i)
row.Visible = False
Dim index As Integer = CInt(GridView1.DataKeys(row.RowIndex).Value)
If CheckBoxArray.Contains(index) Then
row.Visible = True
row.Cells(0).Visible = False
End If
rowIdx += 1
Next
End If
For Each row As GridViewRow In GridView1.Rows
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"
Dim controls As New List(Of Control)()
For Each control As Control In cell.Controls
controls.Add(control)
Next
For Each control As Control In controls
Select Case (control.[GetType]().Name)
Case "CheckBox"
cell.Controls.Add(New Literal() With { _
.Text = TryCast(control, CheckBox).Text _
})
cell.Visible = False
Exit Select
End Select
cell.Controls.Remove(control)
Next
Next
Next
GridView1.RenderControl(hw)
Dim style As String = "<style> .textmode { } </style>"
Response.Write(style)
Dim sr As New StringReader(sw.ToString())
Dim PDF As New Document(PageSize.A4, 10.0F, 10.0F, 100.0F, 10.0F)
Dim htmlparser As New HTMLWorker(PDF)
PdfWriter.GetInstance(PDF, Response.OutputStream)
PDF.Open()
htmlparser.Parse(sr)
PDF.Close()
Response.Output.Write(sw.ToString())
Response.[End]()
Catch ex As Exception
End Try
End Sub
End Class