• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 392
  • Last Modified:

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

0
countrymeister
Asked:
countrymeister
  • 2
1 Solution
 
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
 
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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now