Generate Excel and then redirect to another page

Discussion in 'ASP .Net' started by Guillermo D. Polonsky, Jul 11, 2006.

  1. Hi, i'm exporting gridview data to excel file, and after that (after
    the save as dialog pops up on client machine) i need the page to be
    redirected to home page. Tha problem it's that the export rutine ends
    with response.end, and anything else after that it's ignored, and if i
    put the redirect command in the finally it says that http headers had
    already been sent to the client, so that's not working eather. Ideas?
    This is my code.

    protected void Page_Load(object sender, EventArgs e)
    {
    Session["endWait"] = "SI";
    long id = 0;

    CERTIFICADO oCertificado = new CERTIFICADO();
    DataTable dt;

    try
    {
    string s =
    System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern;
    DateTime fecha =
    System.DateTime.Parse(Request["FechaVencimiento"].ToString(),
    System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat);
    dt = oCertificado.GenerarCertificados(Request["Letra"],
    Request["Cantidad"], oCertificado.GetSerie(),
    Request["FechaVencimiento"], Session["id_usuario"].ToString(), ref id);

    Response.Clear();
    Response.AddHeader("content-disposition",
    "attachment;filename=Certificados" +
    System.DateTime.Today.ToString(s).Replace('/', '-') + "-" +
    System.DateTime.Now.Hour + "_" + System.DateTime.Now.Minute + "_" +
    System.DateTime.Now.Second + ".xls");

    Response.ContentType = "application/vnd.xls";

    StringWriter sw = new StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(sw);
    CERTIFICADO c = new CERTIFICADO();
    GridView1.DataSource = dt;
    GridView1.DataBind();
    GridView1.RenderControl(htw);
    Response.Write(sw.ToString());
    Response.End();
    }
    catch (Exception ex)
    {
    ExceptionManager.Publish(ex);
    }
    finally
    {
    oCertificado = null;
    }
    }

    public override void VerifyRenderingInServerForm(Control control)
    {
    }
     
    Guillermo D. Polonsky, Jul 11, 2006
    #1
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.