Link to home
Start Free TrialLog in
Avatar of triplebd69
triplebd69

asked on

Reload Web Form

I have web form where I am opening a pdf file on the client like so, and all is well.

string FilePath = @"\\Path\Report.pdf";
WebClient User = new WebClient();
HttpResponse response = HttpContext.Current.Response;
response.AddHeader("Content-Disposition", "attachment;filename=\"" + FilePath + "\"");
byte[] data = User.DownloadData(FilePath);
response.BinaryWrite(data);
response.Flush();
response.SuppressContent = true;

Now I need to refresh the the page, I tried Response.Redirect("page.aspx"), but that doesn't work.    
Can anyone help get the page to reload?

Thanks
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Now I need to refresh the the page, I tried Response.Redirect("page.aspx"), but that doesn't work.    
Can anyone help get the page to reload?

you want to do it via client side Javascript or via server side asp.net (C# or vb.net?) code behind?
Hey man,
I'm not sure what is the full scenario and when the pdf should be opened.
However, if you are executing the code on page load, then add a pdfFlag in the QueryString, which will indicate whether you are just loading the page normally or want to open a pdf. So, refreshing the page should be as simple as  Response.Redirect("page.aspx") without the flag QueryString.
Avatar of triplebd69
triplebd69

ASKER

M. Tariq,

It is not in the page load, it is in a private method, any ideas on that scenario?
It would be helpful if you demonstrate how you call the function and when (including the code snippet would be better and better).
Sorry about the formatting EE doesn't seem to like tabs.  I need to to a response redirect either at the bottom of btnPrint_Click or bottom of PrintReport,  At least that is my thought, I am open to other ideas.


protected void Page_Load(object sender, EventArgs e)
{
try
{
if (!IsPostBack)
{
FillDropDowns();
}
if (txtBOmNumber.Text != null && ddlSupplier.SelectedIndex > 0 && ddlCarrier.SelectedIndex > 0)
{
btnPrint.Enabled = true;
}
}
catch (Exception)
{
throw;
}
}
protected void btnPrint_Click(object sender, EventArgs e)
{
try
{
int bol = Convert.ToInt32(txtBOmNumber.Text);
int supplier = Convert.ToInt32(ddlSupplier.SelectedValue);
int carrier = Convert.ToInt32(ddlCarrier.SelectedValue);
PrintReport(bol, supplier, carrier);
Either here Response.Redirect(“default.aspx”);
}
catch (Exception)
{
}
}
private void PrintReport(int bol, int supplier, int carrier)
{
try
{
cry.Load(Server.MapPath("~/Reports/CrystalReport1.rpt"));
SqlDataAdapter da = new SqlDataAdapter("PrintBOL", con);
da.SelectCommand.CommandType = CommandType.StoredProcedure;
da.SelectCommand.Parameters.Add("@BOL", SqlDbType.Int).Value = bol;
da.SelectCommand.Parameters.Add("@Supplier", SqlDbType.Int).Value = supplier;
da.SelectCommand.Parameters.Add("@Carrier", SqlDbType.Int).Value = carrier;

var dt = new DataTable();
da.Fill(dt);
cry.SetDataSource(dt);

ExportOptions CrExportOptions;
DiskFileDestinationOptions CrDiskFileDestinationOptions = new DiskFileDestinationOptions();
PdfRtfWordFormatOptions CrFormatTypeOptions = new PdfRtfWordFormatOptions();
CrDiskFileDestinationOptions.DiskFileName = @"\\pri-fs01\vol1\Test\BOLReport.pdf";
CrExportOptions = cry.ExportOptions;
{
CrExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
CrExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;
CrExportOptions.DestinationOptions = CrDiskFileDestinationOptions;
CrExportOptions.FormatOptions = CrFormatTypeOptions;
}
cry.PrintOptions.NoPrinter = true;
cry.Export();

string FilePath = @"\\pri-fs01\vol1\Test\BOLReport.pdf";
WebClient User = new WebClient();
HttpResponse response = HttpContext.Current.Response;
response.AddHeader("Content-Disposition", "attachment;filename=\"" + FilePath + "\"");
byte[] data = User.DownloadData(FilePath);
response.BinaryWrite(data);
response.Flush();
response.SuppressContent = true;                
Or here Response.Redirect(“default.aspx”);
}
catch (Exception)
{
throw;
}
}
recently i'm doing the similar thing to enable my portal to download some files.

you may do that using client side download with this jQuery plugin

https://github.com/johnculviner/jquery.fileDownload

by using this plugin, you can try look for the callback status, and if it's success then do your redirection via javascript.

check the source codes and examples from link above.

hope that make sense.
This looks like what I need but I don't know jquery.
Below is my aspx page and the changes I made to the code behind, but it still doesn't work.

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    Set-Cookie: fileDownload=true; path=/pri-fs01/vol1/Test/BOLReport.pdf"
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script>
        $(function () {
            $(document).on("click", "a.fileDownloadSimpleRichExperience", function () {
                $.fileDownload($(this).attr('href'), {
                    preparingMessageHtml: "We are preparing your report, please wait...",
                    failMessageHtml: "There was a problem generating your report, please try again."
                });
                return false; //this is critical to stop the click event which will trigger a normal file download!
            });
        });
    </script>
</asp:Content>

protected void btnPrint_Click(object sender, EventArgs e)
      {
            try
            {
                  SaveData();
                  int bol = Convert.ToInt32(txtBOmNumber.Text);
                  int supplier = Convert.ToInt32(ddlSupplier.SelectedValue);
                  int carrier = Convert.ToInt32(ddlCarrier.SelectedValue);
                  PrintReport(bol, supplier, carrier);
                  HttpContext.Current.Response.SetCookie(new HttpCookie("fileDownload", "true") { Path = "/pri-fs01/vol1/Test/BOLReport.pdf" });
            }
            catch (Exception)
            {
                  
            }
      }
Man,
What I know is using [response.End()] will end the execution of the page life cycle, which you are not using, so I'm not sure why the next block of code doesn't execute. However, you can call the print function asynchronously via JavaScript and on success you can redirect to whatever page you want.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.