Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports System.IO
Imports System.Text
Partial Class Account_CouponPrint
    Inherits CharterCMSPage

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If (Not Session("ctrl") Is Nothing) Then
            Dim ctrl As Control = CType(Session("ctrl"), Control)
            Dim queryString As String
            If Not Request.QueryString("Print") Is Nothing Then
                queryString = Request.QueryString("Print")
            End If
            PrintWebControl(ctrl, String.Empty, queryString)
            Session("ctrl") = Nothing
        End If
    End Sub


    Public Sub PrintWebControl(ByVal ctrl As Control, ByVal Script As String, ByVal queryString As String)

        Dim stringWrite As StringWriter = New StringWriter()
        Dim htmlWrite As System.Web.UI.HtmlTextWriter = New System.Web.UI.HtmlTextWriter(stringWrite)
        If TypeOf ctrl Is WebControl Then
            Dim w As Unit = New Unit(100, UnitType.Percentage)
            CType(ctrl, WebControl).Width = w
            If (queryString.ToUpper() = "STATUS") Then
                Dim plhCouponStatusAccountOverview As PlaceHolder
                Dim plhCouponStatusPrintIcon As PlaceHolder
                Dim plhCouponStatusGoBack As PlaceHolder
                Dim lblCloseCouponStatus As Label
                plhCouponStatusAccountOverview = ctrl.FindControl("plhCouponStatusAccountOverview")
                plhCouponStatusPrintIcon = ctrl.FindControl("plhCouponStatusPrintIcon")
                plhCouponStatusGoBack = ctrl.FindControl("plhCouponStatusGoBack")
                lblCloseCouponStatus = ctrl.FindControl("lblCloseCouponStatus")
                plhCouponStatusAccountOverview.Visible = False
                plhCouponStatusGoBack.Visible = False
                lblCloseCouponStatus.Visible = True
                plhCouponStatusPrintIcon.Visible = False
            ElseIf (queryString.ToUpper() = "HISTORY") Then
                Dim plhRedemptionHistoryAccountOverview As PlaceHolder
                Dim plhRedemtionHistoryPrintIcon As PlaceHolder
                Dim plhRedemptionHistoryGoBack As PlaceHolder
                Dim lblRedemptionHistoryClose As Label
                plhRedemptionHistoryAccountOverview = ctrl.FindControl("plhRedemptionHistoryAccountOverview")
                plhRedemtionHistoryPrintIcon = ctrl.FindControl("plhRedemtionHistoryPrintIcon")
                plhRedemptionHistoryGoBack = ctrl.FindControl("plhRedemptionHistoryGoBack")
                lblRedemptionHistoryClose = ctrl.FindControl("lblRedemptionHistoryClose")
                plhRedemptionHistoryAccountOverview.Visible = False
                plhRedemptionHistoryGoBack.Visible = False
                lblRedemptionHistoryClose.Visible = True
                plhRedemtionHistoryPrintIcon.Visible = False

            ElseIf (queryString.ToUpper() = "REDEEM") Then
                Dim plhRedeemAccountOverview As PlaceHolder
                Dim plhRedeemPrintIcon As PlaceHolder
                Dim plhRedeemGoBack As PlaceHolder
                Dim lblRedeemClose As Label
                plhRedeemAccountOverview = ctrl.FindControl("plhRedeemAccountOverview")
                plhRedeemPrintIcon = ctrl.FindControl("plhRedeemPrintIcon")
                plhRedeemGoBack = ctrl.FindControl("plhRedeemGoBack")
                lblRedeemClose = ctrl.FindControl("lblRedeemClose")
                plhRedeemAccountOverview.Visible = False
                plhRedeemGoBack.Visible = False
                lblRedeemClose.Visible = True
                plhRedeemPrintIcon.Visible = False

            End If
            

            frmPrint.Controls.Add(ctrl)
        End If

        'Page.EnableEventValidation = False
        If Script <> String.Empty Then
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "PrintJavaScript", Script)
        End If

        Page.DesignerInitialize()
        Page.RenderControl(htmlWrite)
        Dim strHTML As String = stringWrite.ToString()
        HttpContext.Current.Response.Clear()
        HttpContext.Current.Response.Write(strHTML)
        HttpContext.Current.Response.Write("<script>window.print();</script>")
        HttpContext.Current.Response.End()

        'Dim stringWrite As StringWriter = New StringWriter()
        'Dim htmlWrite As System.Web.UI.HtmlTextWriter = New System.Web.UI.HtmlTextWriter(stringWrite)
        'If TypeOf ctrl Is WebControl Then
        '    Dim w As Unit = New Unit(100, UnitType.Percentage)
        '    CType(ctrl, WebControl).Width = w
        'End If
        'Dim pg As Page = New Page()
        'pg.EnableEventValidation = False
        'If Script <> String.Empty Then
        '    pg.ClientScript.RegisterStartupScript(pg.GetType(), "PrintJavaScript", Script)
        'End If
        'Dim frm As HtmlForm = New HtmlForm()
        'pg.Controls.Add(frm)
        'frm.Attributes.Add("runat", "server")
        'frm.Controls.Add(ctrl)
        'pg.DesignerInitialize()
        'pg.RenderControl(htmlWrite)
        'Dim strHTML As String = stringWrite.ToString()
        'HttpContext.Current.Response.Clear()
        'HttpContext.Current.Response.Write(strHTML)
        'HttpContext.Current.Response.Write("<script>window.print();</script>")
        'HttpContext.Current.Response.End()
    End Sub

End Class
