Click here to Skip to main content
15,948,339 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Here is what I am running into. I have a page and I attach an iframe to the page. The iframe calls a generic ashx. Then it should delete the iframe when it is done.

JavaScript
function ExportExcel() {    
        // Create an IFRAME.
        var iframe = document.createElement("iframe");      
       
        // Point the IFRAME to GenerateFile
        iframe.src = "../ASPX/ExportExcel.ashx";

        // This makes the IFRAME invisible to the user.
        iframe.style.display = "none";
        iframe.onload = function () {            
            this.parentNode.removeChild(this);
        };  

        // Add the IFRAME to the page.  This will trigger
        //   a request to GenerateFile now.
        document.body.appendChild(iframe);             
}


It calls this code in the ashx
VB
Public Class ExportExcel
    Implements System.Web.IHttpHandler
    Implements System.Web.SessionState.IRequiresSessionState

    Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest

        If context.Session.Item("logged_in") = "true" Then
            GetData()
        Else
            context.Response.ContentType = "text/plain"
            context.Response.Write("User Not Logged In")
        End If
    End Sub

    Public Sub GetData()
        Dim oData As New DataTable
        Dim strReportName As String = ""
        Dim oConnection As Insuraware.Utilities.IWDatabase = HttpContext.Current.Session.Item("DBConnection")
        Dim oRptTable As DataTable
        Dim strReportString As String
        Dim strReportType As String
        Dim oCriteria As Object = HttpContext.Current.Session.Item("CurrentReportCriteria")
        Dim strRptID As String = HttpContext.Current.Session.Item("CurrentReportID")

        oRptTable = oConnection.RunQuerySQL(" SELECT a.Source, a.FullName, b.Description from tbl_RptDetail a INNER JOIN tbl_RptMethod b ON a.MethodID = b.ID where a.ID = ? ", strRptID)

        strReportString = oRptTable.Rows(0).Item("Source")
        strReportType = oRptTable.Rows(0).Item("Description")
        strReportName = oRptTable.Rows(0).Item("FullName")
        Dim strParamArray As New Queue

        If strReportType = "ADOSTOREDPROC" Then
            For i As Integer = 0 To oCriteria.Length - 1
                strParamArray.Enqueue(oCriteria(i)(1))
            Next
        Else
            Try
                For i As Integer = 0 To oCriteria.Length - 1
                    'Sub (i)(0) gives you the Replace String in <> you want to replace
                    'Sub (i)(1) gives you what to replace with
                    strReportString = strReportString.Replace("<" & oCriteria(i)(0) & ">", oCriteria(i)(1))
                    'For the Ones we stripped the @ off of on the frontend
                    strReportString = strReportString.Replace("<@" & oCriteria(i)(0) & ">", oCriteria(i)(1))
                Next
            Catch ex As Exception
                'Ignore it and try to run it
            End Try
        End If

        Try
            If strReportType = "ADOSTOREDPROC" Then
                If strParamArray.Count = 0 Then
                    oData = oConnection.RunQuerySP(strReportString).Tables(0)

                Else
                    oData = oConnection.RunQuerySP(strReportString, strParamArray).Tables(0)
                End If

            Else
                oData = oConnection.RunQuerySQL(strReportString)
            End If

            ExportToExcel(oData, strReportName)
        Catch ex As Exception
            HttpContext.Current.Response.ContentType = "text/plain"
            HttpContext.Current.Response.Write("Cannot Run Report: " & ex.Message)
        End Try

    End Sub  

    Public Sub ExportToExcel(ByVal dt As DataTable, ByVal strReportName As String)
        Dim GridView1 As New GridView()
        GridView1.DataSource = dt
        HttpContext.Current.Response.Buffer = True
        HttpContext.Current.Response.AddHeader("Content-Disposition", "inline;filename=" & strReportName & ".xls")
        HttpContext.Current.Response.Charset = ""
        HttpContext.Current.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
        For Each oCell As System.Web.UI.WebControls.TableCell In GridView1.HeaderRow.Cells
            oCell.Style.Add("background-color", "green")
        Next

        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
                For Each oCell As System.Web.UI.WebControls.TableCell In row.Cells
                    oCell.Style.Add("background-color", "#C2D69B")
                Next
            End If
        Next
        GridView1.RenderControl(hw)

        'style to format numbers to string
        Dim style As String = "<style>.textmode{mso-number-format:\@;}</style>"
        HttpContext.Current.Response.Write(style)
        HttpContext.Current.Response.Output.Write(sw.ToString())
        'HttpContext.Current.Response.Flush()
        'HttpContext.Current.Response.End()
    End Sub    

    ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property

End Class


ExportToExcel is what I am working with currently. It all works like it should but I am not hitting the javascript to delete the iframe when it is done and if I choose to open the document rather than save it it runs my ashx 3 more times (I would expect it wouldn't run it again). If I can I would like to just do a window.open then auto close the window when the file is downloaded, but I am not exactly sure on how to do that from within the ashx or the javascript.

Any help would be....helpful.
Posted

1 solution

Try below.
Make Content-Disposition as attachment.
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" & strReportName & ".xls")


Instead of using iframe use window.location.href.
function ExportExcel() {    
     window.location.href  = "../ASPX/ExportExcel.ashx";
 }


Hope it may help.
 
Share this answer
 
v2
Comments
Brad Starbuck West 26-Aug-13 11:44am    
That worked just like I wanted it to. I have a part 2 to the question. For long running queries I want to use

jQuery('#dlgReportResults').showLoading();
and
jQuery('#dlgReportResults').hideLoading();
from the jquery.showLoading.min.js addon.

Any suggestions here? I can do the showLoading, but how do I make the hideLoading part run when the response comes back?
Mahesh Bailwal 26-Aug-13 11:49am    
please post separate question so that you have better answer :)
Brad Starbuck West 26-Aug-13 11:55am    
Sorry about that. New to posting on here. I will create a new question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900