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

R

Rez

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.
 
B

bruce barker

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

-- bruce (sqlwork.com)


| 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.
 
A

arbabi

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,832
Latest member
GlennSmall

Latest Threads

Top