locked
Response.Redirect Handler Running Again on PostBack RRS feed

  • Question

  • User-1091580438 posted

    Hi,

    I have documents (TXT, DOC, etc.) stored in SQL 2008 FileStream.

    I have a web form with a GridView that displays all the documents in the database.  The GridView has both a Select and Delete button.  When the user clicks on the Select button i call the GridView SelectedIndexChanged event which in turn calls a Generic Handler.

            protected void gvCaseDocuments_SelectedIndexChanged(object sender, EventArgs e)
            {
                 Response.Redirect("~/Handlers/DocumentHandler.ashx?docid=" + Convert.ToString(gvCaseDocuments.SelectedValue) + "&docNm=" + Convert.ToString(gvCaseDocuments.SelectedRow.Cells[2].Text));
            }

    The Generic Handler then downloads the file from the SQL Database

            public void ProcessRequest(HttpContext context)
            {
               
                string docId = context.Request["docId"] == null ? string.Empty : context.Request["docId"].ToString();
                string docNm = context.Request["docNm"] == null ? string.Empty : context.Request["docNm"].ToString();

                if (docNm == null)
                {
                    docNm = "UnknownFileName";
                }

                UMSClass UMS = new UMSClass();

                byte[] data = UMS.DowloadDocument(docId);

                context.Response.AddHeader("Content-disposition", "attachment; filename=" + docNm);

                context.Response.ContentType = "application/octet-stream";
                context.Response.BinaryWrite(data);
            }

    This all works fine until an Ajax PostBack occurs in which case the OnSelectedIndexChanged event for the GridView fires again causing the last document that was view to be downloaded for a second time.

    If i remove the Response.Redirect within the OnSelectedIndexChanged event then the event is no longer called when the next Ajax PostBack occurs.

    Can anyone assist with this?

    Thursday, December 29, 2011 12:44 PM

Answers

  • User-158764254 posted

    instead of a "select" button, why not just use a hyperlinkfield in the grid with a navigateurl that points to the handler? 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, December 29, 2011 7:14 PM

All replies

  • User-158764254 posted

    instead of a "select" button, why not just use a hyperlinkfield in the grid with a navigateurl that points to the handler? 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, December 29, 2011 7:14 PM
  • User-1091580438 posted

    Hey,

    Thanks for your suggestion. :-)

    I've created a HyperLinkField and dynamically set the NavigateUrl using the OnRowDataBound Event and this has corrected the issue.

                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    DataRowView rowView = (DataRowView)e.Row.DataItem;

                    string docId = rowView["DocumentID"].ToString();
                    string docNm = rowView["FileName"].ToString();

                    HyperLink myLink = (HyperLink)e.Row.Cells[0].Controls[0];

                    myLink.NavigateUrl = "~/Handlers/DocumentHandler.ashx?docid=" + docId + "&docNm=" + docNm;
                }

    Regards

    Kev

    Friday, December 30, 2011 2:35 PM