Search code examples
javascriptasp.netexcelgridviewwebmethod

HttpContext.Current.Response.End() skip the execution


I am exporting my gridview to excel sheet through JavaScript using webmethod. Since all the rows in the gridview contains template controls like label,anchor,etc., which are created at runtime through JavaScript not at design, I use to pass all the rows by array to web method. To export I have created a table and map the array returned by the JavaScript function. But the problem is while debugging, the execution is skipped on httpContext.Current.Response.End and also I couldn't export the grid. I don't know why its happening. below is my code of web method

<WebMethod(EnableSession:=True, transactionOption:=EnterpriseServices.TransactionOption.RequiresNew)> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Xml, usehttpget:=True)> _
Public Shared Sub XlExport(ByVal fileName As String, ByVal row As Object()())
    HttpContext.Current.Response.Clear()
    HttpContext.Current.Response.AddHeader("content-disposition", String.Format("attachment; filename={0}", fileName))
    HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"
    Dim sw As StringWriter = New StringWriter
    Dim htw As HtmlTextWriter = New HtmlTextWriter(sw)
    Dim table As Table = New Table
    table.GridLines = GridLines.Both
    For i As Integer = 0 To row.Length - 1
        Dim dr As New TableRow
        For j As Integer = 0 To row(i).Length - 1
            Dim dc As New TableCell
            Dim lt As New LiteralControl
            lt.Text = row(i)(j)
            dc.Controls.Add(lt)
            dr.Cells.Add(dc)
        Next
        table.Rows.Add(dr)
    Next
    table.RenderControl(htw)
    HttpContext.Current.Response.Write(sw.ToString)
    HttpContext.Current.Response.End()
End Sub

Solution

  • I tried in my own. i stored all the rows in a hidden field. and on server side i converted them into array then table. now i use the above code as a normal function not webmethod

    sub download_click()
        Dim str_arr As Object()
        Dim str_arr1 As Object()
        str_arr = hdn_gvcheck.Value.Split(",")
        str_arr1 = hdn_headerdiv.Value.Split(",")
        Dim len As Integer = (str_arr.Length - 2) / 36
        Dim twoDarr As Object()() = New Object(len - 1)() {}
        len = (str_arr1.Length - 2) / 36
        Dim headArr As Object()() = New Object(len - 1)() {}
        Dim ind As Integer = 0
        Dim start As Integer = 1
        Dim fnsh As Integer = 36
        headArr(ind) = New Object(35) {}
    l1:
        twoDarr(ind) = New Object(35) {}
    
        Dim col As Integer = 0
        For i As Integer = start To fnsh
            twoDarr(ind)(col) = str_arr(i)
            If ind = 0 Then
                headArr(ind)(col) = str_arr1(i)
            End If
    
            col += 1
        Next
        If ind < 7 Then
            ind += 1
            start = fnsh + 1
            fnsh = start + 35
            GoTo l1
        End If
        doExport("position.xls", twoDarr, headArr, gvcheck)
    End Sub
    
    Sub doExport(ByVal fileName As String, ByVal row As Object()(), ByVal head As Object()(), ByVal gv As GridView)
        HttpContext.Current.Response.Clear()
        HttpContext.Current.Response.Buffer = True
        HttpContext.Current.Response.AddHeader("content-disposition", String.Format("attachment; filename={0}", fileName))
        HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"
        HttpContext.Current.Response.Charset = ""
        Dim table As Table = New Table
        table.GridLines = gv.GridLines 'GridLines.Both
        'For i As Integer = 0 To head.Length - 1
        Dim HDR As New TableRow
        For j As Integer = 0 To head(0).Length - 1
            Dim dc As New TableCell
            dc.Style("font-weight") = "bold"
            Dim lt As New LiteralControl(head(0)(j))
            'lt.Text = row(i)(j)
            dc.Controls.Add(lt)
            HDR.Cells.Add(dc)
        Next
        table.Rows.Add(HDR)
        'Next
        For i As Integer = 0 To row.Length - 1
            Dim dr As New TableRow
            For j As Integer = 0 To row(i).Length - 1
                Dim dc As New TableCell
                Dim lt As New LiteralControl(row(i)(j))
                'lt.Text = row(i)(j)
                dc.Controls.Add(lt)
                dr.Cells.Add(dc)
            Next
            table.Rows.Add(dr)
        Next
        Dim sw As StringWriter = New StringWriter
        Dim htw As HtmlTextWriter = New HtmlTextWriter(sw)
        table.RenderControl(htw)
        HttpContext.Current.Response.Write(sw.ToString) 'sw.ToString
        HttpContext.Current.Response.End()
    End Sub