Exporting to Excel via a Modal Dialog with <base target="_self"> in header

Discussion in 'ASP .Net' started by Rez, Feb 10, 2005.

  1. Rez

    Rez Guest

    Hi Guys,

    I have a problem with exporting to excel from a modal dialog box.

    Basically, i have an ASPX page that gathers user criteria, and passes
    it to a dialogbox (showModalDialog), There is a grid on the dialog
    that will be populated based on the criteria. Since i am doing some
    postback on the dialog i have the following tag in the header:
    <base target="_self">

    Now, I want to export the grid to excel, for doing that i have used
    the following code:

    private void btnExcel_Click(object sender, System.EventArgs e)
    {
    Response.ClearContent();
    Response.ClearHeaders();
    Response.Buffer = true;
    Response.ContentType = "application/vnd.ms-excel";
    Response.Charset = "";
    EnableViewState = false;

    StringWriter sw = new StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(sw);

    GridResult.RenderControl(htw);

    Response.Write(sw.ToString());
    Response.End();
    }

    The above code working fine when i don't have the base tag, but with
    the base tag, it won't work, and i kind of understand why it doesn't,
    but i couldn't find a work-around. Any help/advice would be greatfully
    appreciated.
    One idea can be this:
    Is there any way that i can prevent the tag to be rendered on
    btnExcel_Click?
    I have tried to override the render method of the tag (by using
    SetRenderMethodDelegate), but no success.
    Rez, Feb 10, 2005
    #1
    1. Advertising

  2. Rez

    bruce barker Guest

    you can use a nonmodal dialog, or in client script change the target on the
    form object.

    -- bruce (sqlwork.com)


    "Rez" <> wrote in message
    news:...
    | Hi Guys,
    |
    | I have a problem with exporting to excel from a modal dialog box.
    |
    | Basically, i have an ASPX page that gathers user criteria, and passes
    | it to a dialogbox (showModalDialog), There is a grid on the dialog
    | that will be populated based on the criteria. Since i am doing some
    | postback on the dialog i have the following tag in the header:
    | <base target="_self">
    |
    | Now, I want to export the grid to excel, for doing that i have used
    | the following code:
    |
    | private void btnExcel_Click(object sender, System.EventArgs e)
    | {
    | Response.ClearContent();
    | Response.ClearHeaders();
    | Response.Buffer = true;
    | Response.ContentType = "application/vnd.ms-excel";
    | Response.Charset = "";
    | EnableViewState = false;
    |
    | StringWriter sw = new StringWriter();
    | HtmlTextWriter htw = new HtmlTextWriter(sw);
    |
    | GridResult.RenderControl(htw);
    |
    | Response.Write(sw.ToString());
    | Response.End();
    | }
    |
    | The above code working fine when i don't have the base tag, but with
    | the base tag, it won't work, and i kind of understand why it doesn't,
    | but i couldn't find a work-around. Any help/advice would be greatfully
    | appreciated.
    | One idea can be this:
    | Is there any way that i can prevent the tag to be rendered on
    | btnExcel_Click?
    | I have tried to override the render method of the tag (by using
    | SetRenderMethodDelegate), but no success.
    bruce barker, Feb 10, 2005
    #2
    1. Advertising

  3. Rez

    Guest

    Bruce,
    Thanks for the reply, but my design is based on using modal dialogs and
    at this stage i can't change it to modeless or simply open another
    window.

    i found a solution though:
    for exporting to excel instead of using MIME i will use client-side
    script, it will keep the look and feel of the grid as well. here it is:


    function excelExport()
    {
    var gridTable = document.getElementById('<%= GridResult.ClientID
    %>');

    if (gridTable == null) return;

    var shtml = gridTable.outerHTML;

    var excelApp,excelBook;
    excelApp = new ActiveXObject("Excel.Application");
    excelBook = excelApp.Workbooks.Add();

    excelBook.HTMLProject.HTMLProjectItems(1).Text = shtml;
    excelBook.HTMLProject.RefreshDocument();

    excelApp.Workbooks(1).ActiveSheet.Hyperlinks.Delete();
    excelApp.Workbooks(1).ActiveSheet.Cells.Borders.LineStyle = 0;

    excelApp.ActiveWindow.DisplayGridlines = true;
    excelApp.Visible = true;
    excelApp.UserControl = true;
    }

    thanks.
    , Feb 10, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Koen
    Replies:
    2
    Views:
    1,328
    Scott G.
    Jun 28, 2004
  2. =?Utf-8?B?bWdkZXY=?=

    ASP.NET 2.0 <base target=_self />

    =?Utf-8?B?bWdkZXY=?=, Apr 16, 2007, in forum: ASP .Net
    Replies:
    1
    Views:
    1,052
    =?Utf-8?B?bWdkZXY=?=
    Apr 17, 2007
  3. Joe Kovac
    Replies:
    5
    Views:
    1,483
    Mark Rae
    May 4, 2007
  4. stephen
    Replies:
    2
    Views:
    1,491
    Mark Stevens
    Jan 6, 2010
  5. J Kester
    Replies:
    0
    Views:
    280
    J Kester
    Dec 23, 2003
Loading...

Share This Page