How to handle AJAXed pages and Response.Write or Registerstartupscript to open Excel data

I have an ASP.NET application which uses master pages and AJAX.
I am trying to export some datagrid to excel.

Noticed that when I use Respose,Write I get the error

Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be

paresed. Common causes for this error are when response is modified by calls to Response.Write(),response

filters, HttpModules, or server trace is enabled."

How can I avoid this error, and open the excel worksheet.
I have also looked into Excel Interop dll's and Using registerstartupscript

The RegisterStartup script gives me an error, in the window open - stating Access is denied.
The user is granted full access to the folder where the file is written, and the file is created successfully

But the window.open code errors out stating access is denied.
Should excel be installed on the web server?, there is no ms-excel currently installed.
Protected Sub ExportToExcel()
        Try

            Dim strFileName As String = String.Empty
            Dim strFilePath As String = String.Empty
            strFilePath = Server.MapPath("~/Excel/") + "ExcelFileName" + ".xls"

            If (File.Exists(strFilePath)) Then
                File.Delete(strFilePath)
            End If

            Dim oStringWriter As System.IO.StringWriter = New StringWriter()
            Dim oHtmlTextWriter As System.Web.UI.HtmlTextWriter = New HtmlTextWriter(oStringWriter)
            Dim objStreamWriter As StreamWriter
            Dim strHeader As String = "XYZ Report"
            Dim strHeader1 As String = "StartDate: " & txtStartDate.Text & "  EndDate: " & txtEndDate.Text
            objStreamWriter = File.AppendText(strFilePath)
            dgCriticalResults.RenderControl(oHtmlTextWriter)
            objStreamWriter.WriteLine(strHeader)
            objStreamWriter.WriteLine(strHeader1)
            objStreamWriter.WriteLine(oStringWriter.ToString())
            objStreamWriter.Close()
            Dim sb As StringBuilder = New StringBuilder("")
            sb.Append("window.open('" & strFilePath & "','mywindow','width=400,height=400,resizable=1,top=50,left=50,scrollbars=1');")

            
                        ScriptManager.RegisterClientScriptBlock(Me.Page, Me.Page.GetType(), Me.ClientID, sb.ToString(), True)
        Catch ex As Exception

            'Handle Exception
            Dim strError As String = ex.Message()
                       Exit Sub

        End Try
    End Sub

Open in new window

LVL 1
countrymeisterAsked:
Who is Participating?
 
strickddCommented:
Put your button in the StaticContent section.

MasterPage:
<asp:ScriptManager ID="ScriptManager" 
                   runat="server" />
<asp:UpdatePanel ID="UpdatePanel1" 
                 UpdateMode="Conditional"
                 runat="server">
    <ContentTemplate>
       <asp:ContentPlaceHolder ID="AJAXContent" runat="server">
            </asp:ContentPlaceHolder>
    </ContentTemplate>
</asp:UpdatePanel>

<asp:ContentPlaceHolder ID="StaticContent" runat="server">
            </asp:ContentPlaceHolder>

Open in new window

0
 
strickddCommented:
This is one of the drawbacks of using update panels. What you need to do is any button that triggers the Excel portion needs to be OUTSIDE the update panel. I typically have two content sections in my masterpage, one inside the update panel and one outside.
0
 
countrymeisterAuthor Commented:
strickdd:
do you have any code snippet that I can take a look at.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.