Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
1.89/5 (2 votes)
After downloading image I want the control to not leave my page. For simplicity think it as a textbox on page. If user enters 1 then a particular image is downloaded and textbox should be cleared.

protected void btnDownload_Click(object sender, EventArgs e)
{
Response.Redirect("~/Survey/one.png");
textbox.Text=String.Empty;
}

But after that control does not stay on my page so code preceding it means textbox text can't be empty.
Posted
Comments
F-ES Sitecore 13-Nov-15 10:17am    
If you are redirecting to an image then none of your controls are there anymore. Is there something about your solution you're not saying such as the request to your redirect page happening in a new tab etc?

This is a similar issue that will at least explain what you can't do what it is you're trying to do

http://www.codeproject.com/Answers/1054231/cant-change-label-text-when-use-Response-TransmitF
Afzaal Ahmad Zeeshan 14-Nov-15 3:49am    
Well, you have just redirected the user from that page, the text box no longer matters. Does it?

Response.Redirect() is provided to redirect the users from one page to another. Remove it, and download the image on the same page, by transmitting the file only.
Member 12133729 14-Nov-15 3:57am    
I have used Response.Transmit also. But problem still persists.In my Click code I written as:

Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ClearContent();
Response.Clear();
Response.ContentType = "application/msi";
Response.AppendHeader("Content-Disposition", "attachment; filename=Survey.apk ");
Response.TransmitFile("~/Survey/one.png");
Response.End();
textbox.Text=String.Empty;



Also when I move textbox.Text=String.Empty; before Response.End, it doesn't help.

You can use client-side code to achieve this. With jQuery for example.

Check out this post : http://stackoverflow.com/questions/4545311/download-a-file-by-jquery-ajax

There is a jQuery plugin that makes the download very easy:

$.fileDownload('some/file.pdf')
    .done(function () { alert('File download a success!'); })
    .fail(function () { alert('File download failed!'); });


Then you can clear the textbox

$('#textboxId').val("");


All this happens on the client side so the user stays on the same page
 
Share this answer
 
v3
I used
Page.ClientScript.RegisterStartupScript(this.GetType(), "","window.open('Survey/One.png', '_self', '', false)");

inside my Asp.Net code and I am able to achieve what I want to. But now I am facing problem like when user clicks on back and forth button of browser it downloaded again and again. Please help.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900