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

Posted on 2011-10-16
Last Modified: 2012-06-27
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 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()

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

            If (File.Exists(strFilePath)) Then
            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)
            Dim sb As StringBuilder = New StringBuilder("")
            sb.Append("'" & 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

Question by:countrymeister
    LVL 28

    Expert Comment

    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.
    LVL 1

    Author Comment

    do you have any code snippet that I can take a look at.
    LVL 28

    Accepted Solution

    Put your button in the StaticContent section.

    <asp:ScriptManager ID="ScriptManager" 
                       runat="server" />
    <asp:UpdatePanel ID="UpdatePanel1" 
           <asp:ContentPlaceHolder ID="AJAXContent" runat="server">
    <asp:ContentPlaceHolder ID="StaticContent" runat="server">

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
    Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
    The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
    Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now