Reload calling page in ASP.NET 2.0

  • Thread starter Thread starter J055
  • Start date Start date
J

J055

Hi

I have a list on LinkButton controls on a page. When the link is clicked the
LinkButton.Command event does a Server.Transfer to a page which writes
binary to the HTTP output stream, e.g. a word document, pdf etc.

Response.BinaryWrite(buffer);
Response.End();

I would like to reload the page with the LinkButtons after this event to
reflect changes which are made after the download of the file.

I understand that some client script may be needed but I'm not sure how to
implement this or if something can be added to the LinkButton.OnClientClick
property.

I'm guessing that maybe I need to do a postback and then write some client
script to open the download page? What's the neatest solution please?

Many thanks
Andrew
 
Hi Andrew,

When the LinkButton is clicked, in its Command event, you don't use
Server.Transfer; instead, you use ClientScript.RegisterStartupScript to
register a script to open the download page in a new window:

void Link1_Command(object sender, CommandEventArgs e)
{
ClientScript.RegisterStartupScript(GetType(), "download",
"window.open('Download.aspx', '_blank')", true);
}


#open Method (window)
http://msdn2.microsoft.com/en-us/library/ms536651.aspx


Hope this helps.


Regards,
Walter Wang (wawang@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
I think your idea will be the easiest: on click of the link button on the
server side, do all the necessary updates and spit out JavaScript code to
open the binary streaming page. This will let the download start in another
page but at the same time refresh the current page.
 
Hi

Thanks for the advise. I need to include some querystring parameters. The
window.open method doesn't seem to support a querystring, e.g.

onclick="window.open('Download.aspx?ID=1234');"

I guess i need a javascript function? How would I add the function to the
aspx page?

Thanks
Andrew
 
Hi Andrew,

The window.open function can open a URL with querystring; your code looks
fine, I'm not sure what's the issue you're having?

To add a javascript function in the page, you can use
ClientScript.RegisterClientScriptBlock or
ClientScript.RegisterClientScriptBlock.


Regards,
Walter Wang (wawang@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi Walter

There's a problem with the VS web server and IE7. It works ok in Firefox
using the local server. I get this message in IE when clicking the link with
the querystring.

"Internet Explorer cannot download Download.aspx from localhost. Internet
Explorer was not able to open this internet site. The requested site is
either unavailable or cannot be found. Please try again later."

I've tested it using an IIS web server and it works ok. The only problem for
me is that it opens a blank window as well as a file download pop up window.
Do you know if I can stop the new window displaying?

Thanks again
Andrew
 
Hi Andrew,

Please try following two WebForm:

Default2.aspx:

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

protected void Page_Load(object sender, EventArgs e)
{
Link1.Command += new CommandEventHandler(Link1_Command);
}

void Link1_Command(object sender, CommandEventArgs e)
{
ClientScript.RegisterStartupScript(GetType(), "download",
"window.open('Download2.aspx?id=123', '_blank')", true);
}

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:LinkButton ID="Link1" runat="server"
Text="Download"></asp:LinkButton>
</div>
</form>
</body>
</html>


Download2.aspx:

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

protected void Page_Load(object sender, EventArgs e)
{
string id = Request.QueryString["id"];
DownloadFile(new System.IO.FileInfo(@"c:\1.bin"));
}

void DownloadFile(System.IO.FileInfo fi)
{
Response.AddHeader("Content-Disposition", "attachment; filename=" +
fi.Name);
Response.AddHeader("Content-Length", fi.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.WriteFile(fi.FullName);
Response.End();
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>

</div>
</form>
</body>
</html>



In Download2.aspx, you should be able to get the querystring id and use
this to download different file.

Also, I didn't see the empty window left open when the file is downloaded
on IE7.


Regards,
Walter Wang (wawang@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi Andrew,

I'm wondering if you have tried the code in my last reply. Please feel free
to let me know if there's anything else I can help.


Regards,
Walter Wang (wawang@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top